You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2020/10/28 11:51:39 UTC

[GitHub] [flink] guoweiM opened a new pull request #13824: [FLINK-19736] Add the SinkTransformation

guoweiM opened a new pull request #13824:
URL: https://github.com/apache/flink/pull/13824


   <!--
   *Thank you very much for contributing to Apache Flink - we are happy that you want to help us improve Flink. To help the community review your contribution in the best possible way, please go through the checklist below, which will get the contribution into a shape in which it can be best reviewed.*
   
   *Please understand that we do not do this to make contributions to Flink a hassle. In order to uphold a high standard of quality for code contributions, while at the same time managing a large number of contributions, we need contributors to prepare the contributions well, and give reviewers enough contextual information for the review. Please also understand that contributions that do not follow this guide will take longer to review and thus typically be picked up with lower priority by the community.*
   
   ## Contribution Checklist
   
     - Make sure that the pull request corresponds to a [JIRA issue](https://issues.apache.org/jira/projects/FLINK/issues). Exceptions are made for typos in JavaDoc or documentation files, which need no JIRA issue.
     
     - Name the pull request in the form "[FLINK-XXXX] [component] Title of the pull request", where *FLINK-XXXX* should be replaced by the actual issue number. Skip *component* if you are unsure about which is the best component.
     Typo fixes that have no associated JIRA issue should be named following this pattern: `[hotfix] [docs] Fix typo in event time introduction` or `[hotfix] [javadocs] Expand JavaDoc for PuncuatedWatermarkGenerator`.
   
     - Fill out the template below to describe the changes contributed by the pull request. That will give reviewers the context they need to do the review.
     
     - Make sure that the change passes the automated tests, i.e., `mvn clean verify` passes. You can set up Azure Pipelines CI to do that following [this guide](https://cwiki.apache.org/confluence/display/FLINK/Azure+Pipelines#AzurePipelines-Tutorial:SettingupAzurePipelinesforaforkoftheFlinkrepository).
   
     - Each pull request should address only one issue, not mix up code from multiple issues.
     
     - Each commit in the pull request has a meaningful commit message (including the JIRA id)
   
     - Once all items of the checklist are addressed, remove the above text and this checklist, leaving only the filled out template below.
   
   
   **(The sections below can be removed for hotfixes of typos)**
   -->
   
   ## What is the purpose of the change
   
   Translate the new sink to the physical operator topology.
   
   
   
   
   ## Brief change log
   
   1. Introduce 'SinkTransformation' to represent the new sink api.
   2. Introduce 'SinkTransformationTranslator' that translates the 'SinkTransformation' to the corresponding runtime operators.
   3. Make DataStream Sdk support new sink api.
   
   ## Verifying this change
   
   
   
   This change added tests and can be verified as follows:
   
     - *Introduce `StreamSinkITCase` to test end to end result*
     - *Introduce `SinkTransformationTranslatorTest` to test build sink topology*
   
   ## Does this pull request potentially affect one of the following parts:
   
     - Dependencies (does it add or upgrade a dependency): (no)
     - The public API, i.e., is any changed class annotated with `@Public(Evolving)`: (no)
     - The serializers: (no)
     - The runtime per-record code paths (performance sensitive): (no)
     - Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn/Mesos, ZooKeeper: (no)
     - The S3 file system connector: (no)
   
   ## Documentation
   
     - Does this pull request introduce a new feature? (yes)
     - If yes, how is the feature documented? (JavaDocs )
   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13824: [FLINK-19736] Add the SinkTransformation

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13824:
URL: https://github.com/apache/flink/pull/13824#issuecomment-717895470


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "a10e3dcdaf7ff215ec455ce60ca7594651ad8d04",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8509",
       "triggerID" : "a10e3dcdaf7ff215ec455ce60ca7594651ad8d04",
       "triggerType" : "PUSH"
     }, {
       "hash" : "140a65e1ecfaf98837a4510c548cb8c14a9ed380",
       "status" : "FAILURE",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8617",
       "triggerID" : "140a65e1ecfaf98837a4510c548cb8c14a9ed380",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 140a65e1ecfaf98837a4510c548cb8c14a9ed380 Azure: [FAILURE](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8617) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] kl0u commented on pull request #13824: [FLINK-19736] Add the SinkTransformation

Posted by GitBox <gi...@apache.org>.
kl0u commented on pull request #13824:
URL: https://github.com/apache/flink/pull/13824#issuecomment-718710025


   > Given that now we have a transformation that is mapped to multiple operators, I would suggest to not reuse the `DataStreamSink` but create a new wrapper for the new `SinkTransformation` that will account for this difference.
   > 
   > In more detail, the new wrapper (I do not have a proposal for a name, for now let's call it `NewDataStreamSink`) could have different methods for setting the `uid` of the `writer`, the `committer` and the `globalCommitter` (`NewDataStreamSink withWriterUid(...)`, `NewDataStreamSink withCommiterUid(...)` and `NewDataStreamSink withGlobalCommitterUid(...)`), instead of trying to prepend or append things to the user-provided uid. I also propose to follow a builder pattern as you can see from the signatures above (the methods will return `this`), so that the user can write sth like:
   > 
   > ```
   > env.addSource()
   >       ...
   >       .addSink().withXXXUid(...)
   > ```
   > 
   > This will also allow us to have a validation step (if we want) that, for example, will check that if the `Sink` has a committer, then it has to also have a `committerUid`.
   > 
   > Then this `NewDataStreamSink` can forward all the details to the new `SinkTransformation` which will also have such methods, and from there, the translator will be able to set the `StreamNode` properties accordingly.
   > 
   > The same may make sense also for the `resource` settings but we can see later about that.
   > 
   > In general, I think that such a design that acknowledges that the new `Sink` does not fit in the old `Transformation` paradigm is more flexible and more future-proof.
   > 
   > What do you think @guoweiM and @aljoscha ?
   
   From an offline discussion with @guoweiM I think he is right to not go with my proposal above as it exposes more information about internal implementation than needed and this may tie our hands for the future.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] gaoyunhaii commented on a change in pull request #13824: [FLINK-19736] Add the SinkTransformation

Posted by GitBox <gi...@apache.org>.
gaoyunhaii commented on a change in pull request #13824:
URL: https://github.com/apache/flink/pull/13824#discussion_r514888912



##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/api/datastream/DataStreamSink.java
##########
@@ -33,19 +36,33 @@
 @Public
 public class DataStreamSink<T> {
 
-	private final LegacySinkTransformation<T> transformation;
+	private final PhysicalTransformation<T> transformation;
 
 	@SuppressWarnings("unchecked")
 	protected DataStreamSink(DataStream<T> inputStream, StreamSink<T> operator) {
-		this.transformation = new LegacySinkTransformation<T>(inputStream.getTransformation(), "Unnamed", operator, inputStream.getExecutionEnvironment().getParallelism());
+		this.transformation = (PhysicalTransformation<T>) new LegacySinkTransformation<>(inputStream.getTransformation(), "Unnamed", operator, inputStream.getExecutionEnvironment().getParallelism());

Review comment:
       nit: too long line

##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/translators/SinkTransformationTranslator.java
##########
@@ -0,0 +1,292 @@
+/*
+ * 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.runtime.translators;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.ExecutionConfig;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.connector.sink.Sink;
+import org.apache.flink.api.dag.Transformation;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.streaming.api.graph.StreamGraph;
+import org.apache.flink.streaming.api.graph.TransformationTranslator;
+import org.apache.flink.streaming.api.operators.ChainingStrategy;
+import org.apache.flink.streaming.api.operators.OneInputStreamOperatorFactory;
+import org.apache.flink.streaming.api.operators.StreamOperatorFactory;
+import org.apache.flink.streaming.api.transformations.SinkTransformation;
+import org.apache.flink.streaming.runtime.operators.sink.BatchCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.BatchGlobalCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.GlobalStreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatefulWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatelessWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.util.graph.StreamGraphUtils;
+import org.apache.flink.streaming.util.typeutils.CommittableTypeInformation;
+
+import java.lang.reflect.Type;
+import java.util.Collection;
+import java.util.Collections;
+
+import static org.apache.flink.api.java.typeutils.TypeExtractionUtils.typeToClass;
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * A {@link TransformationTranslator} for the {@link SinkTransformation}.
+ */
+@Internal
+public class SinkTransformationTranslator<InputT, CommT, WriterStateT, GlobalCommT> implements
+		TransformationTranslator<Object, SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT>> {
+
+	@Override
+	public Collection<Integer> translateForBatch(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+		final int parallelism = getParallelism(transformation, context);
+
+		int writerId = addWriter(
+				transformation,
+				parallelism, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new BatchCommitterOperatorFactory<>(transformation.getSink()),
+				parallelism, context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new BatchGlobalCommitterOperatorFactory<>(transformation.getSink()), context);
+		return Collections.emptyList();
+	}
+
+	@Override
+	public Collection<Integer> translateForStreaming(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+
+		final int parallelism = getParallelism(transformation, context);
+
+		int writerId = addWriter(
+				transformation,
+				parallelism, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new StreamingCommitterOperatorFactory<>(transformation.getSink()),
+				parallelism, context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new GlobalStreamingCommitterOperatorFactory<>(transformation.getSink()), context);
+
+		return Collections.emptyList();
+	}
+
+	/**
+	 * Add a sink writer node to the stream graph.
+	 *
+	 * @param sinkTransformation The transformation that the writer belongs to
+	 * @param parallelism The parallelism of the writer
+	 *
+	 * @return The stream node id of the writer
+	 */
+	private int addWriter(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			int parallelism,
+			Context context) {
+		final boolean hasState = sinkTransformation
+				.getSink()
+				.getWriterStateSerializer()
+				.isPresent();
+		checkState(sinkTransformation.getInputs().size() == 1);
+		@SuppressWarnings("unchecked")
+		final Transformation<InputT> input = (Transformation<InputT>) sinkTransformation
+				.getInputs()
+				.get(0);
+		final TypeInformation<InputT> inputTypeInfo = input.getOutputType();
+
+		final StreamOperatorFactory<CommT> writer =
+				hasState ? new StatefulWriterOperatorFactory<>(sinkTransformation.getSink()) : new StatelessWriterOperatorFactory<>(
+						sinkTransformation.getSink());
+
+		final int writerId = addOperatorToStreamGraph(
+				writer, input.getId(),

Review comment:
       nit: put `input.getId()` in new line, and also other calls to `addOperatorToStreamGraph`.

##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/translators/SinkTransformationTranslator.java
##########
@@ -0,0 +1,292 @@
+/*
+ * 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.runtime.translators;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.ExecutionConfig;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.connector.sink.Sink;
+import org.apache.flink.api.dag.Transformation;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.streaming.api.graph.StreamGraph;
+import org.apache.flink.streaming.api.graph.TransformationTranslator;
+import org.apache.flink.streaming.api.operators.ChainingStrategy;
+import org.apache.flink.streaming.api.operators.OneInputStreamOperatorFactory;
+import org.apache.flink.streaming.api.operators.StreamOperatorFactory;
+import org.apache.flink.streaming.api.transformations.SinkTransformation;
+import org.apache.flink.streaming.runtime.operators.sink.BatchCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.BatchGlobalCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.GlobalStreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatefulWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatelessWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.util.graph.StreamGraphUtils;
+import org.apache.flink.streaming.util.typeutils.CommittableTypeInformation;
+
+import java.lang.reflect.Type;
+import java.util.Collection;
+import java.util.Collections;
+
+import static org.apache.flink.api.java.typeutils.TypeExtractionUtils.typeToClass;
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * A {@link TransformationTranslator} for the {@link SinkTransformation}.
+ */
+@Internal
+public class SinkTransformationTranslator<InputT, CommT, WriterStateT, GlobalCommT> implements
+		TransformationTranslator<Object, SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT>> {
+
+	@Override
+	public Collection<Integer> translateForBatch(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+		final int parallelism = getParallelism(transformation, context);
+
+		int writerId = addWriter(
+				transformation,
+				parallelism, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new BatchCommitterOperatorFactory<>(transformation.getSink()),
+				parallelism, context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new BatchGlobalCommitterOperatorFactory<>(transformation.getSink()), context);
+		return Collections.emptyList();
+	}
+
+	@Override
+	public Collection<Integer> translateForStreaming(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+
+		final int parallelism = getParallelism(transformation, context);
+
+		int writerId = addWriter(
+				transformation,
+				parallelism, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new StreamingCommitterOperatorFactory<>(transformation.getSink()),
+				parallelism, context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new GlobalStreamingCommitterOperatorFactory<>(transformation.getSink()), context);
+
+		return Collections.emptyList();
+	}
+
+	/**
+	 * Add a sink writer node to the stream graph.
+	 *
+	 * @param sinkTransformation The transformation that the writer belongs to
+	 * @param parallelism The parallelism of the writer
+	 *
+	 * @return The stream node id of the writer
+	 */
+	private int addWriter(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			int parallelism,
+			Context context) {
+		final boolean hasState = sinkTransformation
+				.getSink()
+				.getWriterStateSerializer()
+				.isPresent();
+		checkState(sinkTransformation.getInputs().size() == 1);
+		@SuppressWarnings("unchecked")
+		final Transformation<InputT> input = (Transformation<InputT>) sinkTransformation
+				.getInputs()
+				.get(0);
+		final TypeInformation<InputT> inputTypeInfo = input.getOutputType();
+
+		final StreamOperatorFactory<CommT> writer =
+				hasState ? new StatefulWriterOperatorFactory<>(sinkTransformation.getSink()) : new StatelessWriterOperatorFactory<>(
+						sinkTransformation.getSink());
+
+		final int writerId = addOperatorToStreamGraph(
+				writer, input.getId(),
+				inputTypeInfo,
+				extractCommittableTypeInformation(sinkTransformation.getSink()),
+				"Sink Writer:",
+				parallelism,
+				sinkTransformation.getMaxParallelism(),
+				sinkTransformation,
+				context);
+
+		StreamGraphUtils.configureResourceProperties(
+				context.getStreamGraph(),
+				writerId,
+				sinkTransformation);
+		return writerId;
+	}
+
+	/**
+	 * Try to add a sink committer to the stream graph.
+	 *
+	 * @param inputId The committer's input stream node id
+	 * @param sinkTransformation The transformation that the committer belongs to
+	 * @param committerFactory The committer operator's factory
+	 * @param parallelism The parallelism of the committer
+	 *
+	 * @return The stream node id of the committer or -1 if the sink topology does not include a committer.
+	 */
+	private int addCommitter(
+			int inputId,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			OneInputStreamOperatorFactory<CommT, CommT> committerFactory,
+			int parallelism,
+			Context context) {
+
+		if (!sinkTransformation.getSink().createCommitter().isPresent()) {
+			return -1;
+		}
+
+		final CommittableTypeInformation<CommT> committableTypeInfo = extractCommittableTypeInformation(
+				sinkTransformation.getSink());
+		checkNotNull(committableTypeInfo);
+
+		return addOperatorToStreamGraph(
+				committerFactory, inputId,
+				committableTypeInfo,
+				committableTypeInfo,
+				"Sink Committer:",
+				parallelism,
+				sinkTransformation.getMaxParallelism(),
+				sinkTransformation,
+				context);
+	}
+
+	/**
+	 * Try to add a sink global committer to the stream graph.
+	 *
+	 * @param inputId The global committer's input stream node id.
+	 * @param sinkTransformation The transformation that the global committer belongs to
+	 * @param globalCommitterFactory The global committer factory
+	 */
+	private void addGlobalCommitter(
+			int inputId,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			OneInputStreamOperatorFactory<CommT, GlobalCommT> globalCommitterFactory,
+			Context context) {
+
+		if (!sinkTransformation.getSink().createGlobalCommitter().isPresent()) {
+			return;
+		}
+
+		addOperatorToStreamGraph(
+				globalCommitterFactory, inputId,
+				checkNotNull(extractCommittableTypeInformation(sinkTransformation.getSink())),
+				null,
+				"Sink Global Committer:",
+				1,
+				1,
+				sinkTransformation,
+				context);
+	}
+
+	private int getParallelism(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		return sinkTransformation.getParallelism() != ExecutionConfig.PARALLELISM_DEFAULT
+				? sinkTransformation.getParallelism()
+				: context.getStreamGraph().getExecutionConfig().getParallelism();
+	}
+
+	/**
+	 * Add a operator to the {@link StreamGraph}.
+	 *
+	 * @param operatorFactory The operator factory
+	 * @param inputId The upstream stream node id of the operator
+	 * @param inTypeInfo The input type information of the operator
+	 * @param outTypInfo The output type information of the operator
+	 * @param prefix The prefix of the name and uid of the operator
+	 * @param parallelism The parallelism of the operator
+	 * @param maxParallelism The max parallelism of the operator
+	 * @param sinkTransformation The sink transformation which the operator belongs to
+	 *
+	 * @return The stream node id of the operator
+	 */
+	private <IN, OUT> int addOperatorToStreamGraph(
+			StreamOperatorFactory<OUT> operatorFactory, int inputId,
+			TypeInformation<IN> inTypeInfo,
+			TypeInformation<OUT> outTypInfo,
+			String prefix,
+			int parallelism,
+			int maxParallelism,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		final StreamGraph streamGraph = context.getStreamGraph();
+		final String slotSharingGroup = context.getSlotSharingGroup();
+		final int transformationId = Transformation.getNewNodeId();
+
+		final ChainingStrategy chainingStrategy = sinkTransformation.getChainingStrategy();
+		if (chainingStrategy != null) {
+			operatorFactory.setChainingStrategy(chainingStrategy);
+		}
+
+		streamGraph.addOperator(
+				transformationId,
+				slotSharingGroup,
+				sinkTransformation.getCoLocationGroupKey(),
+				operatorFactory,
+				inTypeInfo,
+				outTypInfo,
+				String.format("%s %s", prefix, sinkTransformation.getName()));
+
+		streamGraph.setParallelism(transformationId, parallelism);
+		streamGraph.setMaxParallelism(transformationId, maxParallelism);
+
+		StreamGraphUtils.configureBufferTimeout(
+				streamGraph,
+				transformationId,
+				sinkTransformation,
+				context.getDefaultBufferTimeout());
+		if (sinkTransformation.getUid() != null) {
+			streamGraph.setTransformationUID(
+					transformationId,
+					String.format("%s %s", prefix, sinkTransformation.getUid()));
+		}
+		streamGraph.addEdge(inputId, transformationId, 0);
+
+		return transformationId;
+	}
+
+	private CommittableTypeInformation<CommT> extractCommittableTypeInformation(Sink<InputT, CommT, WriterStateT, GlobalCommT> sink) {

Review comment:
       We might extract the type information only one time at the begin of the translation

##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/translators/SinkTransformationTranslator.java
##########
@@ -0,0 +1,292 @@
+/*
+ * 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.runtime.translators;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.ExecutionConfig;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.connector.sink.Sink;
+import org.apache.flink.api.dag.Transformation;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.streaming.api.graph.StreamGraph;
+import org.apache.flink.streaming.api.graph.TransformationTranslator;
+import org.apache.flink.streaming.api.operators.ChainingStrategy;
+import org.apache.flink.streaming.api.operators.OneInputStreamOperatorFactory;
+import org.apache.flink.streaming.api.operators.StreamOperatorFactory;
+import org.apache.flink.streaming.api.transformations.SinkTransformation;
+import org.apache.flink.streaming.runtime.operators.sink.BatchCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.BatchGlobalCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.GlobalStreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatefulWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatelessWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.util.graph.StreamGraphUtils;
+import org.apache.flink.streaming.util.typeutils.CommittableTypeInformation;
+
+import java.lang.reflect.Type;
+import java.util.Collection;
+import java.util.Collections;
+
+import static org.apache.flink.api.java.typeutils.TypeExtractionUtils.typeToClass;
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * A {@link TransformationTranslator} for the {@link SinkTransformation}.
+ */
+@Internal
+public class SinkTransformationTranslator<InputT, CommT, WriterStateT, GlobalCommT> implements
+		TransformationTranslator<Object, SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT>> {
+
+	@Override
+	public Collection<Integer> translateForBatch(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+		final int parallelism = getParallelism(transformation, context);
+
+		int writerId = addWriter(
+				transformation,
+				parallelism, context);

Review comment:
       nit: put `context` in new line, and also for line 72 and 76

##########
File path: flink-tests/src/test/java/org/apache/flink/test/streaming/runtime/StreamSinkITCase.java
##########
@@ -0,0 +1,119 @@
+/*
+ * 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.test.streaming.runtime;
+
+import org.apache.flink.api.common.RuntimeExecutionMode;
+import org.apache.flink.api.common.typeinfo.IntegerTypeInfo;
+import org.apache.flink.api.java.tuple.Tuple3;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.configuration.ExecutionOptions;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.streaming.runtime.operators.sink.TestSink;
+import org.apache.flink.streaming.util.FiniteTestSource;
+import org.apache.flink.test.util.AbstractTestBase;
+
+import org.junit.Test;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+
+import static java.util.stream.Collectors.joining;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.containsInAnyOrder;
+
+/**
+ * Integration test for {@link org.apache.flink.api.connector.sink.Sink} run time implementation.
+ */
+public class StreamSinkITCase extends AbstractTestBase {

Review comment:
       Would it be better to use "SinkITCase" since it tests both streaming and batch mode ?

##########
File path: flink-streaming-java/src/test/java/org/apache/flink/streaming/api/graph/SinkTransformationTranslatorTest.java
##########
@@ -0,0 +1,248 @@
+/*
+ * 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.graph;
+
+import org.apache.flink.api.common.RuntimeExecutionMode;
+import org.apache.flink.api.common.typeutils.base.IntSerializer;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.configuration.ExecutionOptions;
+import org.apache.flink.core.io.SimpleVersionedSerializerTypeSerializerProxy;
+import org.apache.flink.streaming.api.datastream.DataStreamSink;
+import org.apache.flink.streaming.api.datastream.DataStreamSource;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.streaming.api.operators.ChainingStrategy;
+import org.apache.flink.streaming.runtime.operators.sink.BatchCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.BatchGlobalCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.GlobalStreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatelessWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.TestSink;
+import org.apache.flink.util.TestLogger;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+/**
+ * Tests for {@link org.apache.flink.streaming.api.transformations.SinkTransformation}.
+ */
+@RunWith(Parameterized.class)
+public class SinkTransformationTranslatorTest extends TestLogger {
+
+	@Parameterized.Parameters

Review comment:
       might also set name to reveal the parameter values, like `@Parameterized.Parameters(name="mode: {0}, ....")`

##########
File path: flink-tests/src/test/java/org/apache/flink/test/streaming/runtime/StreamSinkITCase.java
##########
@@ -0,0 +1,119 @@
+/*
+ * 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.test.streaming.runtime;
+
+import org.apache.flink.api.common.RuntimeExecutionMode;
+import org.apache.flink.api.common.typeinfo.IntegerTypeInfo;
+import org.apache.flink.api.java.tuple.Tuple3;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.configuration.ExecutionOptions;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.streaming.runtime.operators.sink.TestSink;
+import org.apache.flink.streaming.util.FiniteTestSource;
+import org.apache.flink.test.util.AbstractTestBase;
+
+import org.junit.Test;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+
+import static java.util.stream.Collectors.joining;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.containsInAnyOrder;
+
+/**
+ * Integration test for {@link org.apache.flink.api.connector.sink.Sink} run time implementation.
+ */
+public class StreamSinkITCase extends AbstractTestBase {
+
+	static final List<Integer> SOURCE_DATA = Arrays.asList(
+			895, 127, 148, 161, 148, 662, 822, 491, 275, 122,
+			850, 630, 682, 765, 434, 970, 714, 795, 288, 422);
+
+	static final Queue<String> STREAMING_COMMIT_QUEUE = new ConcurrentLinkedQueue<>();
+
+	static final Queue<String> STREAMING_GLOBAL_COMMIT_QUEUE = new ConcurrentLinkedQueue<>();
+
+	static final Queue<String> BATCH_COMMIT_QUEUE = new ConcurrentLinkedQueue<>();
+
+	static final Queue<String> BATCH_GLOBAL_COMMIT_QUEUE = new ConcurrentLinkedQueue<>();
+
+	@Test
+	public void streamingExecutionMode() throws Exception {
+		StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
+		env.enableCheckpointing(100);
+		FiniteTestSource<Integer> source = new FiniteTestSource<>(SOURCE_DATA);
+
+		env.addSource(source, IntegerTypeInfo.INT_TYPE_INFO)
+				.addSink(TestSink
+						.newBuilder()
+						.setDefaultCommitter((Supplier<Queue<String>> & Serializable) () -> STREAMING_COMMIT_QUEUE)
+						.setGlobalCommitter((Supplier<Queue<String>> & Serializable) () -> STREAMING_GLOBAL_COMMIT_QUEUE)
+						.build());
+
+		env.execute();
+
+		final List<String> expectedCommittedData = SOURCE_DATA
+				.stream()
+				.map(x -> Tuple3.of(x, null, Long.MIN_VALUE).toString())
+				.collect(
+						Collectors.toList());
+
+		// source send data two times
+		expectedCommittedData.addAll(expectedCommittedData);

Review comment:
       Might remove this line since add itself might contains risks. We may direct use `.flatMap(x -> Stream.of(x, x))` to duplicate the elements in the streaming operator before.

##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/translators/SinkTransformationTranslator.java
##########
@@ -0,0 +1,292 @@
+/*
+ * 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.runtime.translators;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.ExecutionConfig;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.connector.sink.Sink;
+import org.apache.flink.api.dag.Transformation;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.streaming.api.graph.StreamGraph;
+import org.apache.flink.streaming.api.graph.TransformationTranslator;
+import org.apache.flink.streaming.api.operators.ChainingStrategy;
+import org.apache.flink.streaming.api.operators.OneInputStreamOperatorFactory;
+import org.apache.flink.streaming.api.operators.StreamOperatorFactory;
+import org.apache.flink.streaming.api.transformations.SinkTransformation;
+import org.apache.flink.streaming.runtime.operators.sink.BatchCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.BatchGlobalCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.GlobalStreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatefulWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatelessWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.util.graph.StreamGraphUtils;
+import org.apache.flink.streaming.util.typeutils.CommittableTypeInformation;
+
+import java.lang.reflect.Type;
+import java.util.Collection;
+import java.util.Collections;
+
+import static org.apache.flink.api.java.typeutils.TypeExtractionUtils.typeToClass;
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * A {@link TransformationTranslator} for the {@link SinkTransformation}.
+ */
+@Internal
+public class SinkTransformationTranslator<InputT, CommT, WriterStateT, GlobalCommT> implements
+		TransformationTranslator<Object, SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT>> {
+
+	@Override
+	public Collection<Integer> translateForBatch(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+		final int parallelism = getParallelism(transformation, context);
+
+		int writerId = addWriter(
+				transformation,
+				parallelism, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new BatchCommitterOperatorFactory<>(transformation.getSink()),
+				parallelism, context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new BatchGlobalCommitterOperatorFactory<>(transformation.getSink()), context);
+		return Collections.emptyList();
+	}
+
+	@Override
+	public Collection<Integer> translateForStreaming(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+
+		final int parallelism = getParallelism(transformation, context);
+
+		int writerId = addWriter(
+				transformation,
+				parallelism, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new StreamingCommitterOperatorFactory<>(transformation.getSink()),
+				parallelism, context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new GlobalStreamingCommitterOperatorFactory<>(transformation.getSink()), context);
+
+		return Collections.emptyList();
+	}
+
+	/**
+	 * Add a sink writer node to the stream graph.
+	 *
+	 * @param sinkTransformation The transformation that the writer belongs to
+	 * @param parallelism The parallelism of the writer
+	 *
+	 * @return The stream node id of the writer
+	 */
+	private int addWriter(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			int parallelism,
+			Context context) {
+		final boolean hasState = sinkTransformation
+				.getSink()
+				.getWriterStateSerializer()
+				.isPresent();
+		checkState(sinkTransformation.getInputs().size() == 1);
+		@SuppressWarnings("unchecked")
+		final Transformation<InputT> input = (Transformation<InputT>) sinkTransformation
+				.getInputs()
+				.get(0);
+		final TypeInformation<InputT> inputTypeInfo = input.getOutputType();
+
+		final StreamOperatorFactory<CommT> writer =
+				hasState ? new StatefulWriterOperatorFactory<>(sinkTransformation.getSink()) : new StatelessWriterOperatorFactory<>(
+						sinkTransformation.getSink());
+
+		final int writerId = addOperatorToStreamGraph(
+				writer, input.getId(),
+				inputTypeInfo,
+				extractCommittableTypeInformation(sinkTransformation.getSink()),
+				"Sink Writer:",
+				parallelism,
+				sinkTransformation.getMaxParallelism(),
+				sinkTransformation,
+				context);
+
+		StreamGraphUtils.configureResourceProperties(
+				context.getStreamGraph(),
+				writerId,
+				sinkTransformation);
+		return writerId;
+	}
+
+	/**
+	 * Try to add a sink committer to the stream graph.
+	 *
+	 * @param inputId The committer's input stream node id
+	 * @param sinkTransformation The transformation that the committer belongs to
+	 * @param committerFactory The committer operator's factory
+	 * @param parallelism The parallelism of the committer
+	 *
+	 * @return The stream node id of the committer or -1 if the sink topology does not include a committer.
+	 */
+	private int addCommitter(
+			int inputId,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			OneInputStreamOperatorFactory<CommT, CommT> committerFactory,
+			int parallelism,
+			Context context) {
+
+		if (!sinkTransformation.getSink().createCommitter().isPresent()) {
+			return -1;
+		}
+
+		final CommittableTypeInformation<CommT> committableTypeInfo = extractCommittableTypeInformation(
+				sinkTransformation.getSink());
+		checkNotNull(committableTypeInfo);
+
+		return addOperatorToStreamGraph(
+				committerFactory, inputId,
+				committableTypeInfo,
+				committableTypeInfo,
+				"Sink Committer:",
+				parallelism,
+				sinkTransformation.getMaxParallelism(),
+				sinkTransformation,
+				context);
+	}
+
+	/**
+	 * Try to add a sink global committer to the stream graph.
+	 *
+	 * @param inputId The global committer's input stream node id.
+	 * @param sinkTransformation The transformation that the global committer belongs to
+	 * @param globalCommitterFactory The global committer factory
+	 */
+	private void addGlobalCommitter(
+			int inputId,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			OneInputStreamOperatorFactory<CommT, GlobalCommT> globalCommitterFactory,
+			Context context) {
+
+		if (!sinkTransformation.getSink().createGlobalCommitter().isPresent()) {
+			return;
+		}
+
+		addOperatorToStreamGraph(
+				globalCommitterFactory, inputId,
+				checkNotNull(extractCommittableTypeInformation(sinkTransformation.getSink())),
+				null,
+				"Sink Global Committer:",
+				1,
+				1,
+				sinkTransformation,
+				context);
+	}
+
+	private int getParallelism(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		return sinkTransformation.getParallelism() != ExecutionConfig.PARALLELISM_DEFAULT
+				? sinkTransformation.getParallelism()
+				: context.getStreamGraph().getExecutionConfig().getParallelism();
+	}
+
+	/**
+	 * Add a operator to the {@link StreamGraph}.
+	 *
+	 * @param operatorFactory The operator factory
+	 * @param inputId The upstream stream node id of the operator
+	 * @param inTypeInfo The input type information of the operator
+	 * @param outTypInfo The output type information of the operator
+	 * @param prefix The prefix of the name and uid of the operator
+	 * @param parallelism The parallelism of the operator
+	 * @param maxParallelism The max parallelism of the operator
+	 * @param sinkTransformation The sink transformation which the operator belongs to
+	 *
+	 * @return The stream node id of the operator
+	 */
+	private <IN, OUT> int addOperatorToStreamGraph(
+			StreamOperatorFactory<OUT> operatorFactory, int inputId,
+			TypeInformation<IN> inTypeInfo,
+			TypeInformation<OUT> outTypInfo,
+			String prefix,
+			int parallelism,
+			int maxParallelism,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		final StreamGraph streamGraph = context.getStreamGraph();
+		final String slotSharingGroup = context.getSlotSharingGroup();
+		final int transformationId = Transformation.getNewNodeId();
+
+		final ChainingStrategy chainingStrategy = sinkTransformation.getChainingStrategy();
+		if (chainingStrategy != null) {
+			operatorFactory.setChainingStrategy(chainingStrategy);
+		}
+
+		streamGraph.addOperator(
+				transformationId,
+				slotSharingGroup,
+				sinkTransformation.getCoLocationGroupKey(),
+				operatorFactory,
+				inTypeInfo,
+				outTypInfo,
+				String.format("%s %s", prefix, sinkTransformation.getName()));
+
+		streamGraph.setParallelism(transformationId, parallelism);
+		streamGraph.setMaxParallelism(transformationId, maxParallelism);
+
+		StreamGraphUtils.configureBufferTimeout(
+				streamGraph,
+				transformationId,
+				sinkTransformation,
+				context.getDefaultBufferTimeout());
+		if (sinkTransformation.getUid() != null) {
+			streamGraph.setTransformationUID(
+					transformationId,
+					String.format("%s %s", prefix, sinkTransformation.getUid()));
+		}
+		streamGraph.addEdge(inputId, transformationId, 0);
+
+		return transformationId;
+	}
+
+	private CommittableTypeInformation<CommT> extractCommittableTypeInformation(Sink<InputT, CommT, WriterStateT, GlobalCommT> sink) {
+		if (sink.getCommittableSerializer().isPresent()) {
+			final Type committableType = TypeExtractor.getParameterType(
+					Sink.class,
+					sink.getClass(),
+					1);
+			return new CommittableTypeInformation<>(
+					typeToClass(committableType), () -> sink.getCommittableSerializer().get());

Review comment:
       nit: ` () -> sink.getCommittableSerializer().get())` in new line




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] aljoscha closed pull request #13824: [FLINK-19736] Add the SinkTransformation

Posted by GitBox <gi...@apache.org>.
aljoscha closed pull request #13824:
URL: https://github.com/apache/flink/pull/13824


   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] kl0u commented on a change in pull request #13824: [FLINK-19736] Add the SinkTransformation

Posted by GitBox <gi...@apache.org>.
kl0u commented on a change in pull request #13824:
URL: https://github.com/apache/flink/pull/13824#discussion_r514211286



##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/api/transformations/SinkTransformation.java
##########
@@ -0,0 +1,91 @@
+/*
+ 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.transformations;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.connector.sink.Sink;
+import org.apache.flink.api.dag.Transformation;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.streaming.api.operators.ChainingStrategy;
+
+import org.apache.flink.shaded.guava18.com.google.common.collect.Lists;
+
+import java.util.Collections;
+import java.util.List;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/**
+ * This Transformation represents a {@link org.apache.flink.api.connector.sink.Sink}.
+ *
+ * @param <InputT> The input type of the {@link org.apache.flink.api.connector.sink.Writer}
+ * @param <CommT> The committable type of the {@link org.apache.flink.api.connector.sink.Writer}
+ * @param <WriterStateT> The state type of the {@link org.apache.flink.api.connector.sink.Writer}
+ * @param <GlobalCommT> The global committable type of the {@link org.apache.flink.api.connector.sink.GlobalCommitter}
+ */
+@Internal
+public class SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> extends PhysicalTransformation<Object> {
+
+	private final Transformation<InputT> input;
+
+	private final Sink<InputT, CommT, WriterStateT, GlobalCommT> sink;
+
+	private ChainingStrategy chainingStrategy;
+
+	public SinkTransformation(
+			Transformation<InputT> input,
+			Sink<InputT, CommT, WriterStateT, GlobalCommT> sink,
+			String name,
+			int parallelism) {
+		super(name, TypeExtractor.getForClass(Object.class), parallelism);
+		this.input = checkNotNull(input);
+		this.sink = checkNotNull(sink);
+	}
+
+	@Override
+	public void setChainingStrategy(ChainingStrategy strategy) {
+		chainingStrategy = strategy;

Review comment:
       We can add a `checkNotNull` here and if we also set a default value to `ALWAYS` (as in the `AbstractStreamOperatorFactory`) then we could remove the `null` check in the `SinkTransformationTranslator.addOperatorToStreamGraph()#257`.

##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/translators/SinkTransformationTranslator.java
##########
@@ -0,0 +1,283 @@
+/*
+ * 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.runtime.translators;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.ExecutionConfig;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.connector.sink.Sink;
+import org.apache.flink.api.dag.Transformation;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.streaming.api.graph.StreamGraph;
+import org.apache.flink.streaming.api.graph.TransformationTranslator;
+import org.apache.flink.streaming.api.operators.ChainingStrategy;
+import org.apache.flink.streaming.api.operators.OneInputStreamOperatorFactory;
+import org.apache.flink.streaming.api.operators.StreamOperatorFactory;
+import org.apache.flink.streaming.api.transformations.SinkTransformation;
+import org.apache.flink.streaming.runtime.operators.sink.BatchCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.BatchGlobalCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.GlobalStreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatefulWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatelessWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.util.graph.StreamGraphUtils;
+
+import java.util.Collection;
+import java.util.Collections;
+
+/**
+ * A {@link TransformationTranslator} for the {@link SinkTransformation}.
+ */
+@Internal
+public class SinkTransformationTranslator<InputT, CommT, WriterStateT, GlobalCommT> implements
+		TransformationTranslator<Object, SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT>> {
+
+	@Override
+	public Collection<Integer> translateForBatch(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+
+		int writerId = addWriter(transformation, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new BatchCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new BatchGlobalCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		return Collections.emptyList();
+	}
+
+	@Override
+	public Collection<Integer> translateForStreaming(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+
+		int writerId = addWriter(transformation, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new StreamingCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new GlobalStreamingCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+
+		return Collections.emptyList();
+	}
+
+	/**
+	 * Add a sink writer node to the stream graph.
+	 *
+	 * @param sinkTransformation The transformation that the writer belongs to
+	 *
+	 * @return The stream node id of the writer
+	 */
+	private int addWriter(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		final boolean hasState = sinkTransformation
+				.getSink()
+				.getWriterStateSerializer()
+				.isPresent();
+		final TypeInformation<CommT> outTypeInfo = TypeExtractor.createTypeInfo(

Review comment:
       I guess this will change after https://github.com/apache/flink/pull/13825 but we could even move it to a separate method as it is used in many places.

##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/translators/SinkTransformationTranslator.java
##########
@@ -0,0 +1,283 @@
+/*
+ * 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.runtime.translators;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.ExecutionConfig;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.connector.sink.Sink;
+import org.apache.flink.api.dag.Transformation;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.streaming.api.graph.StreamGraph;
+import org.apache.flink.streaming.api.graph.TransformationTranslator;
+import org.apache.flink.streaming.api.operators.ChainingStrategy;
+import org.apache.flink.streaming.api.operators.OneInputStreamOperatorFactory;
+import org.apache.flink.streaming.api.operators.StreamOperatorFactory;
+import org.apache.flink.streaming.api.transformations.SinkTransformation;
+import org.apache.flink.streaming.runtime.operators.sink.BatchCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.BatchGlobalCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.GlobalStreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatefulWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatelessWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.util.graph.StreamGraphUtils;
+
+import java.util.Collection;
+import java.util.Collections;
+
+/**
+ * A {@link TransformationTranslator} for the {@link SinkTransformation}.
+ */
+@Internal
+public class SinkTransformationTranslator<InputT, CommT, WriterStateT, GlobalCommT> implements
+		TransformationTranslator<Object, SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT>> {
+
+	@Override
+	public Collection<Integer> translateForBatch(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+
+		int writerId = addWriter(transformation, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new BatchCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new BatchGlobalCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		return Collections.emptyList();
+	}
+
+	@Override
+	public Collection<Integer> translateForStreaming(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+
+		int writerId = addWriter(transformation, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new StreamingCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new GlobalStreamingCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+
+		return Collections.emptyList();
+	}
+
+	/**
+	 * Add a sink writer node to the stream graph.
+	 *
+	 * @param sinkTransformation The transformation that the writer belongs to
+	 *
+	 * @return The stream node id of the writer
+	 */
+	private int addWriter(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		final boolean hasState = sinkTransformation
+				.getSink()
+				.getWriterStateSerializer()
+				.isPresent();
+		final TypeInformation<CommT> outTypeInfo = TypeExtractor.createTypeInfo(
+				Sink.class,
+				sinkTransformation.getSink().getClass(),
+				1,
+				null,
+				null);
+		@SuppressWarnings("unchecked")
+		final Transformation<InputT> input = (Transformation<InputT>) sinkTransformation
+				.getInputs()
+				.get(0);
+		final TypeInformation<InputT> inputTypeInfo = input.getOutputType();
+
+		final int parallelism = getParallelism(sinkTransformation, context);
+
+		final StreamOperatorFactory<CommT> writer =
+				hasState ? new StatefulWriterOperatorFactory<>(sinkTransformation.getSink()) : new StatelessWriterOperatorFactory<>(
+						sinkTransformation.getSink());
+
+		final int writerId = addOperatorToStreamGraph(
+				writer, input.getId(),
+				inputTypeInfo,
+				outTypeInfo,
+				"Sink Writer:",
+				parallelism,
+				sinkTransformation.getMaxParallelism(),
+				sinkTransformation,
+				context);
+
+		StreamGraphUtils.configureResourceProperties(
+				context.getStreamGraph(),
+				writerId,
+				sinkTransformation);
+		return writerId;
+	}
+
+	/**
+	 * Try to add a sink committer to the stream graph.
+	 *
+	 * @param inputId The committer's input stream node id
+	 * @param sinkTransformation The transformation that the committer belongs to
+	 * @param committerFactory The committer operator's factory
+	 *
+	 * @return The stream node id of the committer or -1 if the sink topology does not include a committer.
+	 */
+	private int addCommitter(
+			int inputId,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			OneInputStreamOperatorFactory<CommT, CommT> committerFactory,
+			Context context) {
+
+		if (!sinkTransformation.getSink().getCommittableSerializer().isPresent()) {
+			return -1;
+		}
+
+		final TypeInformation<CommT> committableTypeInfo = TypeExtractor.createTypeInfo(
+				Sink.class,
+				sinkTransformation.getSink().getClass(),
+				1,
+				null,
+				null);
+
+		final int parallelism = getParallelism(sinkTransformation, context);
+
+		return addOperatorToStreamGraph(
+				committerFactory, inputId,
+				committableTypeInfo,
+				committableTypeInfo,
+				"Sink Committer:",
+				parallelism,
+				sinkTransformation.getMaxParallelism(),
+				sinkTransformation,
+				context);
+	}
+
+	/**
+	 * Try to add a sink global committer to the stream graph.
+	 *
+	 * @param inputId The global committer's input stream node id.
+	 * @param sinkTransformation The transformation that the global committer belongs to
+	 * @param globalCommitterFactory The global committer factory
+	 */
+	private void addGlobalCommitter(
+			int inputId,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			OneInputStreamOperatorFactory<CommT, GlobalCommT> globalCommitterFactory,
+			Context context) {
+
+		if (!sinkTransformation.getSink().getGlobalCommittableSerializer().isPresent()) {
+			return;
+		}
+
+		final TypeInformation<CommT> committableTypeInfo = TypeExtractor.createTypeInfo(
+				Sink.class,
+				sinkTransformation.getSink().getClass(),
+				1,
+				null,
+				null);
+
+		addOperatorToStreamGraph(
+				globalCommitterFactory, inputId,
+				committableTypeInfo,
+				null,
+				"Sink Global Committer:",
+				1,
+				1,
+				sinkTransformation,
+				context);
+	}
+
+	private int getParallelism(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		return sinkTransformation.getParallelism() != ExecutionConfig.PARALLELISM_DEFAULT
+				? sinkTransformation.getParallelism()
+				: context.getStreamGraph().getExecutionConfig().getParallelism();
+	}
+
+	/**
+	 * Add a operator to the {@link StreamGraph}.
+	 *
+	 * @param operatorFactory The operator factory
+	 * @param inputId The upstream stream node id of the operator
+	 * @param inTypeInfo The input type information of the operator
+	 * @param outTypInfo The output type information of the operator
+	 * @param prefix The prefix of the name and uid of the operator
+	 * @param parallelism The parallelism of the operator
+	 * @param maxParallelism The max parallelism of the operator
+	 * @param sinkTransformation The sink transformation which the operator belongs to
+	 *
+	 * @return The stream node id of the operator
+	 */
+	private <IN, OUT> int addOperatorToStreamGraph(
+			StreamOperatorFactory<OUT> operatorFactory, int inputId,
+			TypeInformation<IN> inTypeInfo,
+			TypeInformation<OUT> outTypInfo,
+			String prefix,
+			int parallelism,
+			int maxParallelism,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		final StreamGraph streamGraph = context.getStreamGraph();
+		final String slotSharingGroup = context.getSlotSharingGroup();
+		final int transformationId = Transformation.getNewNodeId();
+
+		final ChainingStrategy chainingStrategy = sinkTransformation.getChainingStrategy();
+		if (chainingStrategy != null) {
+			operatorFactory.setChainingStrategy(chainingStrategy);
+		}
+
+		streamGraph.addOperator(
+				transformationId,
+				slotSharingGroup,
+				sinkTransformation.getCoLocationGroupKey(),
+				operatorFactory,
+				inTypeInfo,
+				outTypInfo,
+				String.format("%s %s", prefix, sinkTransformation.getName()));
+
+		streamGraph.setParallelism(transformationId, parallelism);
+		streamGraph.setMaxParallelism(transformationId, maxParallelism);
+
+		StreamGraphUtils.configureBufferTimeout(
+				streamGraph,
+				transformationId,
+				sinkTransformation,
+				context.getDefaultBufferTimeout());
+		if (sinkTransformation.getUid() != null) {
+			streamGraph.setTransformationUID(
+					transformationId,
+					String.format("%s %s", prefix, sinkTransformation.getUid()));
+		}

Review comment:
       Do we know what happens if nothing is set?

##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/translators/SinkTransformationTranslator.java
##########
@@ -0,0 +1,283 @@
+/*
+ * 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.runtime.translators;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.ExecutionConfig;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.connector.sink.Sink;
+import org.apache.flink.api.dag.Transformation;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.streaming.api.graph.StreamGraph;
+import org.apache.flink.streaming.api.graph.TransformationTranslator;
+import org.apache.flink.streaming.api.operators.ChainingStrategy;
+import org.apache.flink.streaming.api.operators.OneInputStreamOperatorFactory;
+import org.apache.flink.streaming.api.operators.StreamOperatorFactory;
+import org.apache.flink.streaming.api.transformations.SinkTransformation;
+import org.apache.flink.streaming.runtime.operators.sink.BatchCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.BatchGlobalCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.GlobalStreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatefulWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatelessWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.util.graph.StreamGraphUtils;
+
+import java.util.Collection;
+import java.util.Collections;
+
+/**
+ * A {@link TransformationTranslator} for the {@link SinkTransformation}.
+ */
+@Internal
+public class SinkTransformationTranslator<InputT, CommT, WriterStateT, GlobalCommT> implements
+		TransformationTranslator<Object, SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT>> {
+
+	@Override
+	public Collection<Integer> translateForBatch(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+
+		int writerId = addWriter(transformation, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new BatchCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new BatchGlobalCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		return Collections.emptyList();
+	}
+
+	@Override
+	public Collection<Integer> translateForStreaming(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+
+		int writerId = addWriter(transformation, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new StreamingCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new GlobalStreamingCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+
+		return Collections.emptyList();
+	}
+
+	/**
+	 * Add a sink writer node to the stream graph.
+	 *
+	 * @param sinkTransformation The transformation that the writer belongs to
+	 *
+	 * @return The stream node id of the writer
+	 */
+	private int addWriter(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		final boolean hasState = sinkTransformation
+				.getSink()
+				.getWriterStateSerializer()
+				.isPresent();
+		final TypeInformation<CommT> outTypeInfo = TypeExtractor.createTypeInfo(
+				Sink.class,
+				sinkTransformation.getSink().getClass(),
+				1,
+				null,
+				null);
+		@SuppressWarnings("unchecked")
+		final Transformation<InputT> input = (Transformation<InputT>) sinkTransformation
+				.getInputs()
+				.get(0);
+		final TypeInformation<InputT> inputTypeInfo = input.getOutputType();
+
+		final int parallelism = getParallelism(sinkTransformation, context);
+
+		final StreamOperatorFactory<CommT> writer =
+				hasState ? new StatefulWriterOperatorFactory<>(sinkTransformation.getSink()) : new StatelessWriterOperatorFactory<>(
+						sinkTransformation.getSink());
+
+		final int writerId = addOperatorToStreamGraph(
+				writer, input.getId(),
+				inputTypeInfo,
+				outTypeInfo,
+				"Sink Writer:",
+				parallelism,
+				sinkTransformation.getMaxParallelism(),
+				sinkTransformation,
+				context);
+
+		StreamGraphUtils.configureResourceProperties(
+				context.getStreamGraph(),
+				writerId,
+				sinkTransformation);
+		return writerId;
+	}
+
+	/**
+	 * Try to add a sink committer to the stream graph.
+	 *
+	 * @param inputId The committer's input stream node id
+	 * @param sinkTransformation The transformation that the committer belongs to
+	 * @param committerFactory The committer operator's factory
+	 *
+	 * @return The stream node id of the committer or -1 if the sink topology does not include a committer.
+	 */
+	private int addCommitter(
+			int inputId,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			OneInputStreamOperatorFactory<CommT, CommT> committerFactory,
+			Context context) {
+
+		if (!sinkTransformation.getSink().getCommittableSerializer().isPresent()) {
+			return -1;
+		}
+
+		final TypeInformation<CommT> committableTypeInfo = TypeExtractor.createTypeInfo(
+				Sink.class,
+				sinkTransformation.getSink().getClass(),
+				1,
+				null,
+				null);
+
+		final int parallelism = getParallelism(sinkTransformation, context);

Review comment:
       Are we sure that in both `BATCH` and `STREAMING` the parallelism of the `Committter` will be equal to `parallelism`? Why not passing it as an argument and figuring the actual value in the `translateForStreaming` or `translateForBatch`?

##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/translators/SinkTransformationTranslator.java
##########
@@ -0,0 +1,283 @@
+/*
+ * 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.runtime.translators;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.ExecutionConfig;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.connector.sink.Sink;
+import org.apache.flink.api.dag.Transformation;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.streaming.api.graph.StreamGraph;
+import org.apache.flink.streaming.api.graph.TransformationTranslator;
+import org.apache.flink.streaming.api.operators.ChainingStrategy;
+import org.apache.flink.streaming.api.operators.OneInputStreamOperatorFactory;
+import org.apache.flink.streaming.api.operators.StreamOperatorFactory;
+import org.apache.flink.streaming.api.transformations.SinkTransformation;
+import org.apache.flink.streaming.runtime.operators.sink.BatchCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.BatchGlobalCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.GlobalStreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatefulWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatelessWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.util.graph.StreamGraphUtils;
+
+import java.util.Collection;
+import java.util.Collections;
+
+/**
+ * A {@link TransformationTranslator} for the {@link SinkTransformation}.
+ */
+@Internal
+public class SinkTransformationTranslator<InputT, CommT, WriterStateT, GlobalCommT> implements
+		TransformationTranslator<Object, SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT>> {
+
+	@Override
+	public Collection<Integer> translateForBatch(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+
+		int writerId = addWriter(transformation, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new BatchCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new BatchGlobalCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		return Collections.emptyList();
+	}
+
+	@Override
+	public Collection<Integer> translateForStreaming(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+
+		int writerId = addWriter(transformation, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new StreamingCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new GlobalStreamingCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+
+		return Collections.emptyList();
+	}
+
+	/**
+	 * Add a sink writer node to the stream graph.
+	 *
+	 * @param sinkTransformation The transformation that the writer belongs to
+	 *
+	 * @return The stream node id of the writer
+	 */
+	private int addWriter(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		final boolean hasState = sinkTransformation
+				.getSink()
+				.getWriterStateSerializer()
+				.isPresent();
+		final TypeInformation<CommT> outTypeInfo = TypeExtractor.createTypeInfo(
+				Sink.class,
+				sinkTransformation.getSink().getClass(),
+				1,
+				null,
+				null);
+		@SuppressWarnings("unchecked")
+		final Transformation<InputT> input = (Transformation<InputT>) sinkTransformation
+				.getInputs()
+				.get(0);

Review comment:
       Here we need to verify if the size exactly 1 before calling `get()`. Probably a `checkState()`?

##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/translators/SinkTransformationTranslator.java
##########
@@ -0,0 +1,283 @@
+/*
+ * 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.runtime.translators;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.ExecutionConfig;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.connector.sink.Sink;
+import org.apache.flink.api.dag.Transformation;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.streaming.api.graph.StreamGraph;
+import org.apache.flink.streaming.api.graph.TransformationTranslator;
+import org.apache.flink.streaming.api.operators.ChainingStrategy;
+import org.apache.flink.streaming.api.operators.OneInputStreamOperatorFactory;
+import org.apache.flink.streaming.api.operators.StreamOperatorFactory;
+import org.apache.flink.streaming.api.transformations.SinkTransformation;
+import org.apache.flink.streaming.runtime.operators.sink.BatchCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.BatchGlobalCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.GlobalStreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatefulWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatelessWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.util.graph.StreamGraphUtils;
+
+import java.util.Collection;
+import java.util.Collections;
+
+/**
+ * A {@link TransformationTranslator} for the {@link SinkTransformation}.
+ */
+@Internal
+public class SinkTransformationTranslator<InputT, CommT, WriterStateT, GlobalCommT> implements
+		TransformationTranslator<Object, SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT>> {
+
+	@Override
+	public Collection<Integer> translateForBatch(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+
+		int writerId = addWriter(transformation, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new BatchCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new BatchGlobalCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		return Collections.emptyList();
+	}
+
+	@Override
+	public Collection<Integer> translateForStreaming(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+
+		int writerId = addWriter(transformation, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new StreamingCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new GlobalStreamingCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+
+		return Collections.emptyList();
+	}
+
+	/**
+	 * Add a sink writer node to the stream graph.
+	 *
+	 * @param sinkTransformation The transformation that the writer belongs to
+	 *
+	 * @return The stream node id of the writer
+	 */
+	private int addWriter(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		final boolean hasState = sinkTransformation
+				.getSink()
+				.getWriterStateSerializer()
+				.isPresent();
+		final TypeInformation<CommT> outTypeInfo = TypeExtractor.createTypeInfo(
+				Sink.class,
+				sinkTransformation.getSink().getClass(),
+				1,
+				null,
+				null);
+		@SuppressWarnings("unchecked")
+		final Transformation<InputT> input = (Transformation<InputT>) sinkTransformation
+				.getInputs()
+				.get(0);
+		final TypeInformation<InputT> inputTypeInfo = input.getOutputType();
+
+		final int parallelism = getParallelism(sinkTransformation, context);
+
+		final StreamOperatorFactory<CommT> writer =
+				hasState ? new StatefulWriterOperatorFactory<>(sinkTransformation.getSink()) : new StatelessWriterOperatorFactory<>(
+						sinkTransformation.getSink());
+
+		final int writerId = addOperatorToStreamGraph(
+				writer, input.getId(),
+				inputTypeInfo,
+				outTypeInfo,
+				"Sink Writer:",
+				parallelism,
+				sinkTransformation.getMaxParallelism(),
+				sinkTransformation,
+				context);
+
+		StreamGraphUtils.configureResourceProperties(
+				context.getStreamGraph(),
+				writerId,
+				sinkTransformation);
+		return writerId;
+	}
+
+	/**
+	 * Try to add a sink committer to the stream graph.
+	 *
+	 * @param inputId The committer's input stream node id
+	 * @param sinkTransformation The transformation that the committer belongs to
+	 * @param committerFactory The committer operator's factory
+	 *
+	 * @return The stream node id of the committer or -1 if the sink topology does not include a committer.
+	 */
+	private int addCommitter(
+			int inputId,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			OneInputStreamOperatorFactory<CommT, CommT> committerFactory,
+			Context context) {
+
+		if (!sinkTransformation.getSink().getCommittableSerializer().isPresent()) {
+			return -1;
+		}
+
+		final TypeInformation<CommT> committableTypeInfo = TypeExtractor.createTypeInfo(
+				Sink.class,
+				sinkTransformation.getSink().getClass(),
+				1,
+				null,
+				null);
+
+		final int parallelism = getParallelism(sinkTransformation, context);
+
+		return addOperatorToStreamGraph(
+				committerFactory, inputId,
+				committableTypeInfo,
+				committableTypeInfo,
+				"Sink Committer:",
+				parallelism,
+				sinkTransformation.getMaxParallelism(),
+				sinkTransformation,
+				context);
+	}
+
+	/**
+	 * Try to add a sink global committer to the stream graph.
+	 *
+	 * @param inputId The global committer's input stream node id.
+	 * @param sinkTransformation The transformation that the global committer belongs to
+	 * @param globalCommitterFactory The global committer factory
+	 */
+	private void addGlobalCommitter(
+			int inputId,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			OneInputStreamOperatorFactory<CommT, GlobalCommT> globalCommitterFactory,
+			Context context) {
+
+		if (!sinkTransformation.getSink().getGlobalCommittableSerializer().isPresent()) {
+			return;
+		}
+
+		final TypeInformation<CommT> committableTypeInfo = TypeExtractor.createTypeInfo(
+				Sink.class,
+				sinkTransformation.getSink().getClass(),
+				1,
+				null,
+				null);
+
+		addOperatorToStreamGraph(
+				globalCommitterFactory, inputId,
+				committableTypeInfo,
+				null,
+				"Sink Global Committer:",
+				1,
+				1,
+				sinkTransformation,
+				context);
+	}
+
+	private int getParallelism(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		return sinkTransformation.getParallelism() != ExecutionConfig.PARALLELISM_DEFAULT
+				? sinkTransformation.getParallelism()
+				: context.getStreamGraph().getExecutionConfig().getParallelism();
+	}
+
+	/**
+	 * Add a operator to the {@link StreamGraph}.
+	 *
+	 * @param operatorFactory The operator factory
+	 * @param inputId The upstream stream node id of the operator
+	 * @param inTypeInfo The input type information of the operator
+	 * @param outTypInfo The output type information of the operator
+	 * @param prefix The prefix of the name and uid of the operator
+	 * @param parallelism The parallelism of the operator
+	 * @param maxParallelism The max parallelism of the operator
+	 * @param sinkTransformation The sink transformation which the operator belongs to
+	 *
+	 * @return The stream node id of the operator
+	 */
+	private <IN, OUT> int addOperatorToStreamGraph(
+			StreamOperatorFactory<OUT> operatorFactory, int inputId,
+			TypeInformation<IN> inTypeInfo,
+			TypeInformation<OUT> outTypInfo,
+			String prefix,
+			int parallelism,
+			int maxParallelism,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		final StreamGraph streamGraph = context.getStreamGraph();
+		final String slotSharingGroup = context.getSlotSharingGroup();
+		final int transformationId = Transformation.getNewNodeId();
+

Review comment:
        Do we want this setting to apply it to all operators or only the `Writer`? I think that it seems more relevant for the writer only as that is what the user "sees", but I am not 100% sure.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] aljoscha commented on pull request #13824: [FLINK-19736] Add the SinkTransformation

Posted by GitBox <gi...@apache.org>.
aljoscha commented on pull request #13824:
URL: https://github.com/apache/flink/pull/13824#issuecomment-720432482


   I merged this! 😃


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13824: [FLINK-19736] Add the SinkTransformation

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13824:
URL: https://github.com/apache/flink/pull/13824#issuecomment-717895470


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "a10e3dcdaf7ff215ec455ce60ca7594651ad8d04",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8509",
       "triggerID" : "a10e3dcdaf7ff215ec455ce60ca7594651ad8d04",
       "triggerType" : "PUSH"
     }, {
       "hash" : "140a65e1ecfaf98837a4510c548cb8c14a9ed380",
       "status" : "FAILURE",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8617",
       "triggerID" : "140a65e1ecfaf98837a4510c548cb8c14a9ed380",
       "triggerType" : "PUSH"
     }, {
       "hash" : "637a67222bb80feee759c66f8a823bf285ef9cfc",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "637a67222bb80feee759c66f8a823bf285ef9cfc",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 140a65e1ecfaf98837a4510c548cb8c14a9ed380 Azure: [FAILURE](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8617) 
   * 637a67222bb80feee759c66f8a823bf285ef9cfc UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] kl0u commented on a change in pull request #13824: [FLINK-19736] Add the SinkTransformation

Posted by GitBox <gi...@apache.org>.
kl0u commented on a change in pull request #13824:
URL: https://github.com/apache/flink/pull/13824#discussion_r514950553



##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/translators/SinkTransformationTranslator.java
##########
@@ -0,0 +1,283 @@
+/*
+ * 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.runtime.translators;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.ExecutionConfig;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.connector.sink.Sink;
+import org.apache.flink.api.dag.Transformation;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.streaming.api.graph.StreamGraph;
+import org.apache.flink.streaming.api.graph.TransformationTranslator;
+import org.apache.flink.streaming.api.operators.ChainingStrategy;
+import org.apache.flink.streaming.api.operators.OneInputStreamOperatorFactory;
+import org.apache.flink.streaming.api.operators.StreamOperatorFactory;
+import org.apache.flink.streaming.api.transformations.SinkTransformation;
+import org.apache.flink.streaming.runtime.operators.sink.BatchCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.BatchGlobalCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.GlobalStreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatefulWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatelessWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.util.graph.StreamGraphUtils;
+
+import java.util.Collection;
+import java.util.Collections;
+
+/**
+ * A {@link TransformationTranslator} for the {@link SinkTransformation}.
+ */
+@Internal
+public class SinkTransformationTranslator<InputT, CommT, WriterStateT, GlobalCommT> implements
+		TransformationTranslator<Object, SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT>> {
+
+	@Override
+	public Collection<Integer> translateForBatch(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+
+		int writerId = addWriter(transformation, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new BatchCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new BatchGlobalCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		return Collections.emptyList();
+	}
+
+	@Override
+	public Collection<Integer> translateForStreaming(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+
+		int writerId = addWriter(transformation, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new StreamingCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new GlobalStreamingCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+
+		return Collections.emptyList();
+	}
+
+	/**
+	 * Add a sink writer node to the stream graph.
+	 *
+	 * @param sinkTransformation The transformation that the writer belongs to
+	 *
+	 * @return The stream node id of the writer
+	 */
+	private int addWriter(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		final boolean hasState = sinkTransformation
+				.getSink()
+				.getWriterStateSerializer()
+				.isPresent();
+		final TypeInformation<CommT> outTypeInfo = TypeExtractor.createTypeInfo(
+				Sink.class,
+				sinkTransformation.getSink().getClass(),
+				1,
+				null,
+				null);
+		@SuppressWarnings("unchecked")
+		final Transformation<InputT> input = (Transformation<InputT>) sinkTransformation
+				.getInputs()
+				.get(0);
+		final TypeInformation<InputT> inputTypeInfo = input.getOutputType();
+
+		final int parallelism = getParallelism(sinkTransformation, context);
+
+		final StreamOperatorFactory<CommT> writer =
+				hasState ? new StatefulWriterOperatorFactory<>(sinkTransformation.getSink()) : new StatelessWriterOperatorFactory<>(
+						sinkTransformation.getSink());
+
+		final int writerId = addOperatorToStreamGraph(
+				writer, input.getId(),
+				inputTypeInfo,
+				outTypeInfo,
+				"Sink Writer:",
+				parallelism,
+				sinkTransformation.getMaxParallelism(),
+				sinkTransformation,
+				context);
+
+		StreamGraphUtils.configureResourceProperties(
+				context.getStreamGraph(),
+				writerId,
+				sinkTransformation);
+		return writerId;
+	}
+
+	/**
+	 * Try to add a sink committer to the stream graph.
+	 *
+	 * @param inputId The committer's input stream node id
+	 * @param sinkTransformation The transformation that the committer belongs to
+	 * @param committerFactory The committer operator's factory
+	 *
+	 * @return The stream node id of the committer or -1 if the sink topology does not include a committer.
+	 */
+	private int addCommitter(
+			int inputId,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			OneInputStreamOperatorFactory<CommT, CommT> committerFactory,
+			Context context) {
+
+		if (!sinkTransformation.getSink().getCommittableSerializer().isPresent()) {
+			return -1;
+		}
+
+		final TypeInformation<CommT> committableTypeInfo = TypeExtractor.createTypeInfo(
+				Sink.class,
+				sinkTransformation.getSink().getClass(),
+				1,
+				null,
+				null);
+
+		final int parallelism = getParallelism(sinkTransformation, context);
+
+		return addOperatorToStreamGraph(
+				committerFactory, inputId,
+				committableTypeInfo,
+				committableTypeInfo,
+				"Sink Committer:",
+				parallelism,
+				sinkTransformation.getMaxParallelism(),
+				sinkTransformation,
+				context);
+	}
+
+	/**
+	 * Try to add a sink global committer to the stream graph.
+	 *
+	 * @param inputId The global committer's input stream node id.
+	 * @param sinkTransformation The transformation that the global committer belongs to
+	 * @param globalCommitterFactory The global committer factory
+	 */
+	private void addGlobalCommitter(
+			int inputId,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			OneInputStreamOperatorFactory<CommT, GlobalCommT> globalCommitterFactory,
+			Context context) {
+
+		if (!sinkTransformation.getSink().getGlobalCommittableSerializer().isPresent()) {
+			return;
+		}
+
+		final TypeInformation<CommT> committableTypeInfo = TypeExtractor.createTypeInfo(
+				Sink.class,
+				sinkTransformation.getSink().getClass(),
+				1,
+				null,
+				null);
+
+		addOperatorToStreamGraph(
+				globalCommitterFactory, inputId,
+				committableTypeInfo,
+				null,
+				"Sink Global Committer:",
+				1,
+				1,
+				sinkTransformation,
+				context);
+	}
+
+	private int getParallelism(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		return sinkTransformation.getParallelism() != ExecutionConfig.PARALLELISM_DEFAULT
+				? sinkTransformation.getParallelism()
+				: context.getStreamGraph().getExecutionConfig().getParallelism();
+	}
+
+	/**
+	 * Add a operator to the {@link StreamGraph}.
+	 *
+	 * @param operatorFactory The operator factory
+	 * @param inputId The upstream stream node id of the operator
+	 * @param inTypeInfo The input type information of the operator
+	 * @param outTypInfo The output type information of the operator
+	 * @param prefix The prefix of the name and uid of the operator
+	 * @param parallelism The parallelism of the operator
+	 * @param maxParallelism The max parallelism of the operator
+	 * @param sinkTransformation The sink transformation which the operator belongs to
+	 *
+	 * @return The stream node id of the operator
+	 */
+	private <IN, OUT> int addOperatorToStreamGraph(
+			StreamOperatorFactory<OUT> operatorFactory, int inputId,
+			TypeInformation<IN> inTypeInfo,
+			TypeInformation<OUT> outTypInfo,
+			String prefix,
+			int parallelism,
+			int maxParallelism,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		final StreamGraph streamGraph = context.getStreamGraph();
+		final String slotSharingGroup = context.getSlotSharingGroup();
+		final int transformationId = Transformation.getNewNodeId();
+
+		final ChainingStrategy chainingStrategy = sinkTransformation.getChainingStrategy();
+		if (chainingStrategy != null) {
+			operatorFactory.setChainingStrategy(chainingStrategy);
+		}
+
+		streamGraph.addOperator(
+				transformationId,
+				slotSharingGroup,
+				sinkTransformation.getCoLocationGroupKey(),
+				operatorFactory,
+				inTypeInfo,
+				outTypInfo,
+				String.format("%s %s", prefix, sinkTransformation.getName()));
+
+		streamGraph.setParallelism(transformationId, parallelism);
+		streamGraph.setMaxParallelism(transformationId, maxParallelism);
+
+		StreamGraphUtils.configureBufferTimeout(
+				streamGraph,
+				transformationId,
+				sinkTransformation,
+				context.getDefaultBufferTimeout());
+		if (sinkTransformation.getUid() != null) {
+			streamGraph.setTransformationUID(
+					transformationId,
+					String.format("%s %s", prefix, sinkTransformation.getUid()));
+		}

Review comment:
       I see. I was wondering then if we should force the user to set it, but this would be different behaviour than the rest of the transformations, so we may not want to do it.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] guoweiM commented on a change in pull request #13824: [FLINK-19736] Add the SinkTransformation

Posted by GitBox <gi...@apache.org>.
guoweiM commented on a change in pull request #13824:
URL: https://github.com/apache/flink/pull/13824#discussion_r514348756



##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/translators/SinkTransformationTranslator.java
##########
@@ -0,0 +1,283 @@
+/*
+ * 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.runtime.translators;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.ExecutionConfig;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.connector.sink.Sink;
+import org.apache.flink.api.dag.Transformation;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.streaming.api.graph.StreamGraph;
+import org.apache.flink.streaming.api.graph.TransformationTranslator;
+import org.apache.flink.streaming.api.operators.ChainingStrategy;
+import org.apache.flink.streaming.api.operators.OneInputStreamOperatorFactory;
+import org.apache.flink.streaming.api.operators.StreamOperatorFactory;
+import org.apache.flink.streaming.api.transformations.SinkTransformation;
+import org.apache.flink.streaming.runtime.operators.sink.BatchCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.BatchGlobalCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.GlobalStreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatefulWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatelessWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.util.graph.StreamGraphUtils;
+
+import java.util.Collection;
+import java.util.Collections;
+
+/**
+ * A {@link TransformationTranslator} for the {@link SinkTransformation}.
+ */
+@Internal
+public class SinkTransformationTranslator<InputT, CommT, WriterStateT, GlobalCommT> implements
+		TransformationTranslator<Object, SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT>> {
+
+	@Override
+	public Collection<Integer> translateForBatch(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+
+		int writerId = addWriter(transformation, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new BatchCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new BatchGlobalCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		return Collections.emptyList();
+	}
+
+	@Override
+	public Collection<Integer> translateForStreaming(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+
+		int writerId = addWriter(transformation, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new StreamingCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new GlobalStreamingCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+
+		return Collections.emptyList();
+	}
+
+	/**
+	 * Add a sink writer node to the stream graph.
+	 *
+	 * @param sinkTransformation The transformation that the writer belongs to
+	 *
+	 * @return The stream node id of the writer
+	 */
+	private int addWriter(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		final boolean hasState = sinkTransformation
+				.getSink()
+				.getWriterStateSerializer()
+				.isPresent();
+		final TypeInformation<CommT> outTypeInfo = TypeExtractor.createTypeInfo(
+				Sink.class,
+				sinkTransformation.getSink().getClass(),
+				1,
+				null,
+				null);
+		@SuppressWarnings("unchecked")
+		final Transformation<InputT> input = (Transformation<InputT>) sinkTransformation
+				.getInputs()
+				.get(0);
+		final TypeInformation<InputT> inputTypeInfo = input.getOutputType();
+
+		final int parallelism = getParallelism(sinkTransformation, context);
+
+		final StreamOperatorFactory<CommT> writer =
+				hasState ? new StatefulWriterOperatorFactory<>(sinkTransformation.getSink()) : new StatelessWriterOperatorFactory<>(
+						sinkTransformation.getSink());
+
+		final int writerId = addOperatorToStreamGraph(
+				writer, input.getId(),
+				inputTypeInfo,
+				outTypeInfo,
+				"Sink Writer:",
+				parallelism,
+				sinkTransformation.getMaxParallelism(),
+				sinkTransformation,
+				context);
+
+		StreamGraphUtils.configureResourceProperties(
+				context.getStreamGraph(),
+				writerId,
+				sinkTransformation);
+		return writerId;
+	}
+
+	/**
+	 * Try to add a sink committer to the stream graph.
+	 *
+	 * @param inputId The committer's input stream node id
+	 * @param sinkTransformation The transformation that the committer belongs to
+	 * @param committerFactory The committer operator's factory
+	 *
+	 * @return The stream node id of the committer or -1 if the sink topology does not include a committer.
+	 */
+	private int addCommitter(
+			int inputId,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			OneInputStreamOperatorFactory<CommT, CommT> committerFactory,
+			Context context) {
+
+		if (!sinkTransformation.getSink().getCommittableSerializer().isPresent()) {
+			return -1;
+		}
+
+		final TypeInformation<CommT> committableTypeInfo = TypeExtractor.createTypeInfo(
+				Sink.class,
+				sinkTransformation.getSink().getClass(),
+				1,
+				null,
+				null);
+
+		final int parallelism = getParallelism(sinkTransformation, context);
+
+		return addOperatorToStreamGraph(
+				committerFactory, inputId,
+				committableTypeInfo,
+				committableTypeInfo,
+				"Sink Committer:",
+				parallelism,
+				sinkTransformation.getMaxParallelism(),
+				sinkTransformation,
+				context);
+	}
+
+	/**
+	 * Try to add a sink global committer to the stream graph.
+	 *
+	 * @param inputId The global committer's input stream node id.
+	 * @param sinkTransformation The transformation that the global committer belongs to
+	 * @param globalCommitterFactory The global committer factory
+	 */
+	private void addGlobalCommitter(
+			int inputId,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			OneInputStreamOperatorFactory<CommT, GlobalCommT> globalCommitterFactory,
+			Context context) {
+
+		if (!sinkTransformation.getSink().getGlobalCommittableSerializer().isPresent()) {
+			return;
+		}
+
+		final TypeInformation<CommT> committableTypeInfo = TypeExtractor.createTypeInfo(
+				Sink.class,
+				sinkTransformation.getSink().getClass(),
+				1,
+				null,
+				null);
+
+		addOperatorToStreamGraph(
+				globalCommitterFactory, inputId,
+				committableTypeInfo,
+				null,
+				"Sink Global Committer:",
+				1,
+				1,
+				sinkTransformation,
+				context);
+	}
+
+	private int getParallelism(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		return sinkTransformation.getParallelism() != ExecutionConfig.PARALLELISM_DEFAULT
+				? sinkTransformation.getParallelism()
+				: context.getStreamGraph().getExecutionConfig().getParallelism();
+	}
+
+	/**
+	 * Add a operator to the {@link StreamGraph}.
+	 *
+	 * @param operatorFactory The operator factory
+	 * @param inputId The upstream stream node id of the operator
+	 * @param inTypeInfo The input type information of the operator
+	 * @param outTypInfo The output type information of the operator
+	 * @param prefix The prefix of the name and uid of the operator
+	 * @param parallelism The parallelism of the operator
+	 * @param maxParallelism The max parallelism of the operator
+	 * @param sinkTransformation The sink transformation which the operator belongs to
+	 *
+	 * @return The stream node id of the operator
+	 */
+	private <IN, OUT> int addOperatorToStreamGraph(
+			StreamOperatorFactory<OUT> operatorFactory, int inputId,
+			TypeInformation<IN> inTypeInfo,
+			TypeInformation<OUT> outTypInfo,
+			String prefix,
+			int parallelism,
+			int maxParallelism,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		final StreamGraph streamGraph = context.getStreamGraph();
+		final String slotSharingGroup = context.getSlotSharingGroup();
+		final int transformationId = Transformation.getNewNodeId();
+
+		final ChainingStrategy chainingStrategy = sinkTransformation.getChainingStrategy();
+		if (chainingStrategy != null) {
+			operatorFactory.setChainingStrategy(chainingStrategy);
+		}
+
+		streamGraph.addOperator(
+				transformationId,
+				slotSharingGroup,
+				sinkTransformation.getCoLocationGroupKey(),
+				operatorFactory,
+				inTypeInfo,
+				outTypInfo,
+				String.format("%s %s", prefix, sinkTransformation.getName()));
+
+		streamGraph.setParallelism(transformationId, parallelism);
+		streamGraph.setMaxParallelism(transformationId, maxParallelism);
+
+		StreamGraphUtils.configureBufferTimeout(
+				streamGraph,
+				transformationId,
+				sinkTransformation,
+				context.getDefaultBufferTimeout());
+		if (sinkTransformation.getUid() != null) {
+			streamGraph.setTransformationUID(
+					transformationId,
+					String.format("%s %s", prefix, sinkTransformation.getUid()));
+		}

Review comment:
       IIRC, we could not reuse the state if we not set the uid.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] guoweiM commented on a change in pull request #13824: [FLINK-19736] Add the SinkTransformation

Posted by GitBox <gi...@apache.org>.
guoweiM commented on a change in pull request #13824:
URL: https://github.com/apache/flink/pull/13824#discussion_r514886269



##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/translators/SinkTransformationTranslator.java
##########
@@ -0,0 +1,283 @@
+/*
+ * 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.runtime.translators;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.ExecutionConfig;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.connector.sink.Sink;
+import org.apache.flink.api.dag.Transformation;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.streaming.api.graph.StreamGraph;
+import org.apache.flink.streaming.api.graph.TransformationTranslator;
+import org.apache.flink.streaming.api.operators.ChainingStrategy;
+import org.apache.flink.streaming.api.operators.OneInputStreamOperatorFactory;
+import org.apache.flink.streaming.api.operators.StreamOperatorFactory;
+import org.apache.flink.streaming.api.transformations.SinkTransformation;
+import org.apache.flink.streaming.runtime.operators.sink.BatchCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.BatchGlobalCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.GlobalStreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatefulWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatelessWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.util.graph.StreamGraphUtils;
+
+import java.util.Collection;
+import java.util.Collections;
+
+/**
+ * A {@link TransformationTranslator} for the {@link SinkTransformation}.
+ */
+@Internal
+public class SinkTransformationTranslator<InputT, CommT, WriterStateT, GlobalCommT> implements
+		TransformationTranslator<Object, SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT>> {
+
+	@Override
+	public Collection<Integer> translateForBatch(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+
+		int writerId = addWriter(transformation, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new BatchCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new BatchGlobalCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		return Collections.emptyList();
+	}
+
+	@Override
+	public Collection<Integer> translateForStreaming(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+
+		int writerId = addWriter(transformation, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new StreamingCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new GlobalStreamingCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+
+		return Collections.emptyList();
+	}
+
+	/**
+	 * Add a sink writer node to the stream graph.
+	 *
+	 * @param sinkTransformation The transformation that the writer belongs to
+	 *
+	 * @return The stream node id of the writer
+	 */
+	private int addWriter(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		final boolean hasState = sinkTransformation
+				.getSink()
+				.getWriterStateSerializer()
+				.isPresent();
+		final TypeInformation<CommT> outTypeInfo = TypeExtractor.createTypeInfo(
+				Sink.class,
+				sinkTransformation.getSink().getClass(),
+				1,
+				null,
+				null);
+		@SuppressWarnings("unchecked")
+		final Transformation<InputT> input = (Transformation<InputT>) sinkTransformation
+				.getInputs()
+				.get(0);
+		final TypeInformation<InputT> inputTypeInfo = input.getOutputType();
+
+		final int parallelism = getParallelism(sinkTransformation, context);
+
+		final StreamOperatorFactory<CommT> writer =
+				hasState ? new StatefulWriterOperatorFactory<>(sinkTransformation.getSink()) : new StatelessWriterOperatorFactory<>(
+						sinkTransformation.getSink());
+
+		final int writerId = addOperatorToStreamGraph(
+				writer, input.getId(),
+				inputTypeInfo,
+				outTypeInfo,
+				"Sink Writer:",
+				parallelism,
+				sinkTransformation.getMaxParallelism(),
+				sinkTransformation,
+				context);
+
+		StreamGraphUtils.configureResourceProperties(
+				context.getStreamGraph(),
+				writerId,
+				sinkTransformation);
+		return writerId;
+	}
+
+	/**
+	 * Try to add a sink committer to the stream graph.
+	 *
+	 * @param inputId The committer's input stream node id
+	 * @param sinkTransformation The transformation that the committer belongs to
+	 * @param committerFactory The committer operator's factory
+	 *
+	 * @return The stream node id of the committer or -1 if the sink topology does not include a committer.
+	 */
+	private int addCommitter(
+			int inputId,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			OneInputStreamOperatorFactory<CommT, CommT> committerFactory,
+			Context context) {
+
+		if (!sinkTransformation.getSink().getCommittableSerializer().isPresent()) {
+			return -1;
+		}
+
+		final TypeInformation<CommT> committableTypeInfo = TypeExtractor.createTypeInfo(
+				Sink.class,
+				sinkTransformation.getSink().getClass(),
+				1,
+				null,
+				null);
+
+		final int parallelism = getParallelism(sinkTransformation, context);
+
+		return addOperatorToStreamGraph(
+				committerFactory, inputId,
+				committableTypeInfo,
+				committableTypeInfo,
+				"Sink Committer:",
+				parallelism,
+				sinkTransformation.getMaxParallelism(),
+				sinkTransformation,
+				context);
+	}
+
+	/**
+	 * Try to add a sink global committer to the stream graph.
+	 *
+	 * @param inputId The global committer's input stream node id.
+	 * @param sinkTransformation The transformation that the global committer belongs to
+	 * @param globalCommitterFactory The global committer factory
+	 */
+	private void addGlobalCommitter(
+			int inputId,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			OneInputStreamOperatorFactory<CommT, GlobalCommT> globalCommitterFactory,
+			Context context) {
+
+		if (!sinkTransformation.getSink().getGlobalCommittableSerializer().isPresent()) {
+			return;
+		}
+
+		final TypeInformation<CommT> committableTypeInfo = TypeExtractor.createTypeInfo(
+				Sink.class,
+				sinkTransformation.getSink().getClass(),
+				1,
+				null,
+				null);
+
+		addOperatorToStreamGraph(
+				globalCommitterFactory, inputId,
+				committableTypeInfo,
+				null,
+				"Sink Global Committer:",
+				1,
+				1,
+				sinkTransformation,
+				context);
+	}
+
+	private int getParallelism(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		return sinkTransformation.getParallelism() != ExecutionConfig.PARALLELISM_DEFAULT
+				? sinkTransformation.getParallelism()
+				: context.getStreamGraph().getExecutionConfig().getParallelism();
+	}
+
+	/**
+	 * Add a operator to the {@link StreamGraph}.
+	 *
+	 * @param operatorFactory The operator factory
+	 * @param inputId The upstream stream node id of the operator
+	 * @param inTypeInfo The input type information of the operator
+	 * @param outTypInfo The output type information of the operator
+	 * @param prefix The prefix of the name and uid of the operator
+	 * @param parallelism The parallelism of the operator
+	 * @param maxParallelism The max parallelism of the operator
+	 * @param sinkTransformation The sink transformation which the operator belongs to
+	 *
+	 * @return The stream node id of the operator
+	 */
+	private <IN, OUT> int addOperatorToStreamGraph(
+			StreamOperatorFactory<OUT> operatorFactory, int inputId,
+			TypeInformation<IN> inTypeInfo,
+			TypeInformation<OUT> outTypInfo,
+			String prefix,
+			int parallelism,
+			int maxParallelism,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		final StreamGraph streamGraph = context.getStreamGraph();
+		final String slotSharingGroup = context.getSlotSharingGroup();
+		final int transformationId = Transformation.getNewNodeId();
+

Review comment:
       Do you mean whether we want to set the same slot sharing group to all these three operators or not? 
   
   I think the answer might be yes. It is because this would let the operator use less slots. Specifically:
   In case of the streaming execution mode, the same share slot group could make `Writer` and `Committer` be chained together and make the `GlobalComitter` reuse the same slot.
   In case of the batch execution mode, the same slot group could make `Committer` and `GlobalCommitter` be chained together if the parallelism of `Committer` is 1.
   
   Of course maybe I miss something. What do you think?




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] guoweiM commented on a change in pull request #13824: [FLINK-19736] Add the SinkTransformation

Posted by GitBox <gi...@apache.org>.
guoweiM commented on a change in pull request #13824:
URL: https://github.com/apache/flink/pull/13824#discussion_r514929389



##########
File path: flink-tests/src/test/java/org/apache/flink/test/streaming/runtime/StreamSinkITCase.java
##########
@@ -0,0 +1,119 @@
+/*
+ * 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.test.streaming.runtime;
+
+import org.apache.flink.api.common.RuntimeExecutionMode;
+import org.apache.flink.api.common.typeinfo.IntegerTypeInfo;
+import org.apache.flink.api.java.tuple.Tuple3;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.configuration.ExecutionOptions;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.streaming.runtime.operators.sink.TestSink;
+import org.apache.flink.streaming.util.FiniteTestSource;
+import org.apache.flink.test.util.AbstractTestBase;
+
+import org.junit.Test;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+
+import static java.util.stream.Collectors.joining;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.containsInAnyOrder;
+
+/**
+ * Integration test for {@link org.apache.flink.api.connector.sink.Sink} run time implementation.
+ */
+public class StreamSinkITCase extends AbstractTestBase {

Review comment:
       yes.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] kl0u commented on a change in pull request #13824: [FLINK-19736] Add the SinkTransformation

Posted by GitBox <gi...@apache.org>.
kl0u commented on a change in pull request #13824:
URL: https://github.com/apache/flink/pull/13824#discussion_r514942224



##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/translators/SinkTransformationTranslator.java
##########
@@ -0,0 +1,283 @@
+/*
+ * 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.runtime.translators;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.ExecutionConfig;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.connector.sink.Sink;
+import org.apache.flink.api.dag.Transformation;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.streaming.api.graph.StreamGraph;
+import org.apache.flink.streaming.api.graph.TransformationTranslator;
+import org.apache.flink.streaming.api.operators.ChainingStrategy;
+import org.apache.flink.streaming.api.operators.OneInputStreamOperatorFactory;
+import org.apache.flink.streaming.api.operators.StreamOperatorFactory;
+import org.apache.flink.streaming.api.transformations.SinkTransformation;
+import org.apache.flink.streaming.runtime.operators.sink.BatchCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.BatchGlobalCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.GlobalStreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatefulWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatelessWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.util.graph.StreamGraphUtils;
+
+import java.util.Collection;
+import java.util.Collections;
+
+/**
+ * A {@link TransformationTranslator} for the {@link SinkTransformation}.
+ */
+@Internal
+public class SinkTransformationTranslator<InputT, CommT, WriterStateT, GlobalCommT> implements
+		TransformationTranslator<Object, SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT>> {
+
+	@Override
+	public Collection<Integer> translateForBatch(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+
+		int writerId = addWriter(transformation, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new BatchCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new BatchGlobalCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		return Collections.emptyList();
+	}
+
+	@Override
+	public Collection<Integer> translateForStreaming(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+
+		int writerId = addWriter(transformation, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new StreamingCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new GlobalStreamingCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+
+		return Collections.emptyList();
+	}
+
+	/**
+	 * Add a sink writer node to the stream graph.
+	 *
+	 * @param sinkTransformation The transformation that the writer belongs to
+	 *
+	 * @return The stream node id of the writer
+	 */
+	private int addWriter(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		final boolean hasState = sinkTransformation
+				.getSink()
+				.getWriterStateSerializer()
+				.isPresent();
+		final TypeInformation<CommT> outTypeInfo = TypeExtractor.createTypeInfo(
+				Sink.class,
+				sinkTransformation.getSink().getClass(),
+				1,
+				null,
+				null);
+		@SuppressWarnings("unchecked")
+		final Transformation<InputT> input = (Transformation<InputT>) sinkTransformation
+				.getInputs()
+				.get(0);
+		final TypeInformation<InputT> inputTypeInfo = input.getOutputType();
+
+		final int parallelism = getParallelism(sinkTransformation, context);
+
+		final StreamOperatorFactory<CommT> writer =
+				hasState ? new StatefulWriterOperatorFactory<>(sinkTransformation.getSink()) : new StatelessWriterOperatorFactory<>(
+						sinkTransformation.getSink());
+
+		final int writerId = addOperatorToStreamGraph(
+				writer, input.getId(),
+				inputTypeInfo,
+				outTypeInfo,
+				"Sink Writer:",
+				parallelism,
+				sinkTransformation.getMaxParallelism(),
+				sinkTransformation,
+				context);
+
+		StreamGraphUtils.configureResourceProperties(
+				context.getStreamGraph(),
+				writerId,
+				sinkTransformation);
+		return writerId;
+	}
+
+	/**
+	 * Try to add a sink committer to the stream graph.
+	 *
+	 * @param inputId The committer's input stream node id
+	 * @param sinkTransformation The transformation that the committer belongs to
+	 * @param committerFactory The committer operator's factory
+	 *
+	 * @return The stream node id of the committer or -1 if the sink topology does not include a committer.
+	 */
+	private int addCommitter(
+			int inputId,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			OneInputStreamOperatorFactory<CommT, CommT> committerFactory,
+			Context context) {
+
+		if (!sinkTransformation.getSink().getCommittableSerializer().isPresent()) {
+			return -1;
+		}
+
+		final TypeInformation<CommT> committableTypeInfo = TypeExtractor.createTypeInfo(
+				Sink.class,
+				sinkTransformation.getSink().getClass(),
+				1,
+				null,
+				null);
+
+		final int parallelism = getParallelism(sinkTransformation, context);
+
+		return addOperatorToStreamGraph(
+				committerFactory, inputId,
+				committableTypeInfo,
+				committableTypeInfo,
+				"Sink Committer:",
+				parallelism,
+				sinkTransformation.getMaxParallelism(),
+				sinkTransformation,
+				context);
+	}
+
+	/**
+	 * Try to add a sink global committer to the stream graph.
+	 *
+	 * @param inputId The global committer's input stream node id.
+	 * @param sinkTransformation The transformation that the global committer belongs to
+	 * @param globalCommitterFactory The global committer factory
+	 */
+	private void addGlobalCommitter(
+			int inputId,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			OneInputStreamOperatorFactory<CommT, GlobalCommT> globalCommitterFactory,
+			Context context) {
+
+		if (!sinkTransformation.getSink().getGlobalCommittableSerializer().isPresent()) {
+			return;
+		}
+
+		final TypeInformation<CommT> committableTypeInfo = TypeExtractor.createTypeInfo(
+				Sink.class,
+				sinkTransformation.getSink().getClass(),
+				1,
+				null,
+				null);
+
+		addOperatorToStreamGraph(
+				globalCommitterFactory, inputId,
+				committableTypeInfo,
+				null,
+				"Sink Global Committer:",
+				1,
+				1,
+				sinkTransformation,
+				context);
+	}
+
+	private int getParallelism(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		return sinkTransformation.getParallelism() != ExecutionConfig.PARALLELISM_DEFAULT
+				? sinkTransformation.getParallelism()
+				: context.getStreamGraph().getExecutionConfig().getParallelism();
+	}
+
+	/**
+	 * Add a operator to the {@link StreamGraph}.
+	 *
+	 * @param operatorFactory The operator factory
+	 * @param inputId The upstream stream node id of the operator
+	 * @param inTypeInfo The input type information of the operator
+	 * @param outTypInfo The output type information of the operator
+	 * @param prefix The prefix of the name and uid of the operator
+	 * @param parallelism The parallelism of the operator
+	 * @param maxParallelism The max parallelism of the operator
+	 * @param sinkTransformation The sink transformation which the operator belongs to
+	 *
+	 * @return The stream node id of the operator
+	 */
+	private <IN, OUT> int addOperatorToStreamGraph(
+			StreamOperatorFactory<OUT> operatorFactory, int inputId,
+			TypeInformation<IN> inTypeInfo,
+			TypeInformation<OUT> outTypInfo,
+			String prefix,
+			int parallelism,
+			int maxParallelism,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		final StreamGraph streamGraph = context.getStreamGraph();
+		final String slotSharingGroup = context.getSlotSharingGroup();
+		final int transformationId = Transformation.getNewNodeId();
+

Review comment:
       Sorry, I was referring to the `chainingStrategy`.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13824: [FLINK-19736] Add the SinkTransformation

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13824:
URL: https://github.com/apache/flink/pull/13824#issuecomment-717895470


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "a10e3dcdaf7ff215ec455ce60ca7594651ad8d04",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8509",
       "triggerID" : "a10e3dcdaf7ff215ec455ce60ca7594651ad8d04",
       "triggerType" : "PUSH"
     }, {
       "hash" : "140a65e1ecfaf98837a4510c548cb8c14a9ed380",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8617",
       "triggerID" : "140a65e1ecfaf98837a4510c548cb8c14a9ed380",
       "triggerType" : "PUSH"
     }, {
       "hash" : "637a67222bb80feee759c66f8a823bf285ef9cfc",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8661",
       "triggerID" : "637a67222bb80feee759c66f8a823bf285ef9cfc",
       "triggerType" : "PUSH"
     }, {
       "hash" : "0eea4ff74a7d07b9cd3f5d9cd52666b92e52a6f7",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8679",
       "triggerID" : "0eea4ff74a7d07b9cd3f5d9cd52666b92e52a6f7",
       "triggerType" : "PUSH"
     }, {
       "hash" : "c532c1e8ddc40c467f015e05976e048ae77037c0",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8687",
       "triggerID" : "c532c1e8ddc40c467f015e05976e048ae77037c0",
       "triggerType" : "PUSH"
     }, {
       "hash" : "d499ba8f59b5961a215882d0485b6c2bdfcbda00",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "d499ba8f59b5961a215882d0485b6c2bdfcbda00",
       "triggerType" : "PUSH"
     }, {
       "hash" : "6388af5e1d75c962d1acd15dd76c6a01dddea081",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "6388af5e1d75c962d1acd15dd76c6a01dddea081",
       "triggerType" : "PUSH"
     }, {
       "hash" : "105577cdf76f1799d8088b59c885c7219ead8060",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8690",
       "triggerID" : "105577cdf76f1799d8088b59c885c7219ead8060",
       "triggerType" : "PUSH"
     }, {
       "hash" : "686c538f23fd5a19ac4544ffd05162a9a747b533",
       "status" : "FAILURE",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8747",
       "triggerID" : "686c538f23fd5a19ac4544ffd05162a9a747b533",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * d499ba8f59b5961a215882d0485b6c2bdfcbda00 UNKNOWN
   * 6388af5e1d75c962d1acd15dd76c6a01dddea081 UNKNOWN
   * 686c538f23fd5a19ac4544ffd05162a9a747b533 Azure: [FAILURE](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8747) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13824: [FLINK-19736] Add the SinkTransformation

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13824:
URL: https://github.com/apache/flink/pull/13824#issuecomment-717895470


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "a10e3dcdaf7ff215ec455ce60ca7594651ad8d04",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8509",
       "triggerID" : "a10e3dcdaf7ff215ec455ce60ca7594651ad8d04",
       "triggerType" : "PUSH"
     }, {
       "hash" : "140a65e1ecfaf98837a4510c548cb8c14a9ed380",
       "status" : "FAILURE",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8617",
       "triggerID" : "140a65e1ecfaf98837a4510c548cb8c14a9ed380",
       "triggerType" : "PUSH"
     }, {
       "hash" : "637a67222bb80feee759c66f8a823bf285ef9cfc",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8661",
       "triggerID" : "637a67222bb80feee759c66f8a823bf285ef9cfc",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 140a65e1ecfaf98837a4510c548cb8c14a9ed380 Azure: [FAILURE](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8617) 
   * 637a67222bb80feee759c66f8a823bf285ef9cfc Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8661) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] gaoyunhaii commented on a change in pull request #13824: [FLINK-19736] Add the SinkTransformation

Posted by GitBox <gi...@apache.org>.
gaoyunhaii commented on a change in pull request #13824:
URL: https://github.com/apache/flink/pull/13824#discussion_r514947954



##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/translators/SinkTransformationTranslator.java
##########
@@ -0,0 +1,292 @@
+/*
+ * 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.runtime.translators;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.ExecutionConfig;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.connector.sink.Sink;
+import org.apache.flink.api.dag.Transformation;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.streaming.api.graph.StreamGraph;
+import org.apache.flink.streaming.api.graph.TransformationTranslator;
+import org.apache.flink.streaming.api.operators.ChainingStrategy;
+import org.apache.flink.streaming.api.operators.OneInputStreamOperatorFactory;
+import org.apache.flink.streaming.api.operators.StreamOperatorFactory;
+import org.apache.flink.streaming.api.transformations.SinkTransformation;
+import org.apache.flink.streaming.runtime.operators.sink.BatchCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.BatchGlobalCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.GlobalStreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatefulWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatelessWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.util.graph.StreamGraphUtils;
+import org.apache.flink.streaming.util.typeutils.CommittableTypeInformation;
+
+import java.lang.reflect.Type;
+import java.util.Collection;
+import java.util.Collections;
+
+import static org.apache.flink.api.java.typeutils.TypeExtractionUtils.typeToClass;
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * A {@link TransformationTranslator} for the {@link SinkTransformation}.
+ */
+@Internal
+public class SinkTransformationTranslator<InputT, CommT, WriterStateT, GlobalCommT> implements
+		TransformationTranslator<Object, SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT>> {
+
+	@Override
+	public Collection<Integer> translateForBatch(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+		final int parallelism = getParallelism(transformation, context);
+
+		int writerId = addWriter(
+				transformation,
+				parallelism, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new BatchCommitterOperatorFactory<>(transformation.getSink()),
+				parallelism, context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new BatchGlobalCommitterOperatorFactory<>(transformation.getSink()), context);
+		return Collections.emptyList();
+	}
+
+	@Override
+	public Collection<Integer> translateForStreaming(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+
+		final int parallelism = getParallelism(transformation, context);
+
+		int writerId = addWriter(
+				transformation,
+				parallelism, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new StreamingCommitterOperatorFactory<>(transformation.getSink()),
+				parallelism, context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new GlobalStreamingCommitterOperatorFactory<>(transformation.getSink()), context);
+
+		return Collections.emptyList();
+	}
+
+	/**
+	 * Add a sink writer node to the stream graph.
+	 *
+	 * @param sinkTransformation The transformation that the writer belongs to
+	 * @param parallelism The parallelism of the writer
+	 *
+	 * @return The stream node id of the writer
+	 */
+	private int addWriter(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			int parallelism,
+			Context context) {
+		final boolean hasState = sinkTransformation
+				.getSink()
+				.getWriterStateSerializer()
+				.isPresent();
+		checkState(sinkTransformation.getInputs().size() == 1);
+		@SuppressWarnings("unchecked")
+		final Transformation<InputT> input = (Transformation<InputT>) sinkTransformation
+				.getInputs()
+				.get(0);
+		final TypeInformation<InputT> inputTypeInfo = input.getOutputType();
+
+		final StreamOperatorFactory<CommT> writer =
+				hasState ? new StatefulWriterOperatorFactory<>(sinkTransformation.getSink()) : new StatelessWriterOperatorFactory<>(
+						sinkTransformation.getSink());
+
+		final int writerId = addOperatorToStreamGraph(
+				writer, input.getId(),
+				inputTypeInfo,
+				extractCommittableTypeInformation(sinkTransformation.getSink()),
+				"Sink Writer:",
+				parallelism,
+				sinkTransformation.getMaxParallelism(),
+				sinkTransformation,
+				context);
+
+		StreamGraphUtils.configureResourceProperties(
+				context.getStreamGraph(),
+				writerId,
+				sinkTransformation);
+		return writerId;
+	}
+
+	/**
+	 * Try to add a sink committer to the stream graph.
+	 *
+	 * @param inputId The committer's input stream node id
+	 * @param sinkTransformation The transformation that the committer belongs to
+	 * @param committerFactory The committer operator's factory
+	 * @param parallelism The parallelism of the committer
+	 *
+	 * @return The stream node id of the committer or -1 if the sink topology does not include a committer.
+	 */
+	private int addCommitter(
+			int inputId,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			OneInputStreamOperatorFactory<CommT, CommT> committerFactory,
+			int parallelism,
+			Context context) {
+
+		if (!sinkTransformation.getSink().createCommitter().isPresent()) {
+			return -1;
+		}
+
+		final CommittableTypeInformation<CommT> committableTypeInfo = extractCommittableTypeInformation(
+				sinkTransformation.getSink());
+		checkNotNull(committableTypeInfo);
+
+		return addOperatorToStreamGraph(
+				committerFactory, inputId,
+				committableTypeInfo,
+				committableTypeInfo,
+				"Sink Committer:",
+				parallelism,
+				sinkTransformation.getMaxParallelism(),
+				sinkTransformation,
+				context);
+	}
+
+	/**
+	 * Try to add a sink global committer to the stream graph.
+	 *
+	 * @param inputId The global committer's input stream node id.
+	 * @param sinkTransformation The transformation that the global committer belongs to
+	 * @param globalCommitterFactory The global committer factory
+	 */
+	private void addGlobalCommitter(
+			int inputId,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			OneInputStreamOperatorFactory<CommT, GlobalCommT> globalCommitterFactory,
+			Context context) {
+
+		if (!sinkTransformation.getSink().createGlobalCommitter().isPresent()) {
+			return;
+		}
+
+		addOperatorToStreamGraph(
+				globalCommitterFactory, inputId,
+				checkNotNull(extractCommittableTypeInformation(sinkTransformation.getSink())),
+				null,
+				"Sink Global Committer:",
+				1,
+				1,
+				sinkTransformation,
+				context);
+	}
+
+	private int getParallelism(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		return sinkTransformation.getParallelism() != ExecutionConfig.PARALLELISM_DEFAULT
+				? sinkTransformation.getParallelism()
+				: context.getStreamGraph().getExecutionConfig().getParallelism();
+	}
+
+	/**
+	 * Add a operator to the {@link StreamGraph}.
+	 *
+	 * @param operatorFactory The operator factory
+	 * @param inputId The upstream stream node id of the operator
+	 * @param inTypeInfo The input type information of the operator
+	 * @param outTypInfo The output type information of the operator
+	 * @param prefix The prefix of the name and uid of the operator
+	 * @param parallelism The parallelism of the operator
+	 * @param maxParallelism The max parallelism of the operator
+	 * @param sinkTransformation The sink transformation which the operator belongs to
+	 *
+	 * @return The stream node id of the operator
+	 */
+	private <IN, OUT> int addOperatorToStreamGraph(
+			StreamOperatorFactory<OUT> operatorFactory, int inputId,
+			TypeInformation<IN> inTypeInfo,
+			TypeInformation<OUT> outTypInfo,
+			String prefix,
+			int parallelism,
+			int maxParallelism,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		final StreamGraph streamGraph = context.getStreamGraph();
+		final String slotSharingGroup = context.getSlotSharingGroup();
+		final int transformationId = Transformation.getNewNodeId();
+
+		final ChainingStrategy chainingStrategy = sinkTransformation.getChainingStrategy();
+		if (chainingStrategy != null) {
+			operatorFactory.setChainingStrategy(chainingStrategy);
+		}
+
+		streamGraph.addOperator(
+				transformationId,
+				slotSharingGroup,
+				sinkTransformation.getCoLocationGroupKey(),
+				operatorFactory,
+				inTypeInfo,
+				outTypInfo,
+				String.format("%s %s", prefix, sinkTransformation.getName()));
+
+		streamGraph.setParallelism(transformationId, parallelism);
+		streamGraph.setMaxParallelism(transformationId, maxParallelism);
+
+		StreamGraphUtils.configureBufferTimeout(
+				streamGraph,
+				transformationId,
+				sinkTransformation,
+				context.getDefaultBufferTimeout());
+		if (sinkTransformation.getUid() != null) {
+			streamGraph.setTransformationUID(
+					transformationId,
+					String.format("%s %s", prefix, sinkTransformation.getUid()));
+		}
+		streamGraph.addEdge(inputId, transformationId, 0);
+
+		return transformationId;
+	}
+
+	private CommittableTypeInformation<CommT> extractCommittableTypeInformation(Sink<InputT, CommT, WriterStateT, GlobalCommT> sink) {

Review comment:
       I think we might only move the extract method, and pass it to each method, but leaves the checks in the current place. 




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] kl0u commented on a change in pull request #13824: [FLINK-19736] Add the SinkTransformation

Posted by GitBox <gi...@apache.org>.
kl0u commented on a change in pull request #13824:
URL: https://github.com/apache/flink/pull/13824#discussion_r514211286



##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/api/transformations/SinkTransformation.java
##########
@@ -0,0 +1,91 @@
+/*
+ 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.transformations;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.connector.sink.Sink;
+import org.apache.flink.api.dag.Transformation;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.streaming.api.operators.ChainingStrategy;
+
+import org.apache.flink.shaded.guava18.com.google.common.collect.Lists;
+
+import java.util.Collections;
+import java.util.List;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/**
+ * This Transformation represents a {@link org.apache.flink.api.connector.sink.Sink}.
+ *
+ * @param <InputT> The input type of the {@link org.apache.flink.api.connector.sink.Writer}
+ * @param <CommT> The committable type of the {@link org.apache.flink.api.connector.sink.Writer}
+ * @param <WriterStateT> The state type of the {@link org.apache.flink.api.connector.sink.Writer}
+ * @param <GlobalCommT> The global committable type of the {@link org.apache.flink.api.connector.sink.GlobalCommitter}
+ */
+@Internal
+public class SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> extends PhysicalTransformation<Object> {
+
+	private final Transformation<InputT> input;
+
+	private final Sink<InputT, CommT, WriterStateT, GlobalCommT> sink;
+
+	private ChainingStrategy chainingStrategy;
+
+	public SinkTransformation(
+			Transformation<InputT> input,
+			Sink<InputT, CommT, WriterStateT, GlobalCommT> sink,
+			String name,
+			int parallelism) {
+		super(name, TypeExtractor.getForClass(Object.class), parallelism);
+		this.input = checkNotNull(input);
+		this.sink = checkNotNull(sink);
+	}
+
+	@Override
+	public void setChainingStrategy(ChainingStrategy strategy) {
+		chainingStrategy = strategy;

Review comment:
       We can add a `checkNotNull` here.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] guoweiM commented on a change in pull request #13824: [FLINK-19736] Add the SinkTransformation

Posted by GitBox <gi...@apache.org>.
guoweiM commented on a change in pull request #13824:
URL: https://github.com/apache/flink/pull/13824#discussion_r514346615



##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/translators/SinkTransformationTranslator.java
##########
@@ -0,0 +1,283 @@
+/*
+ * 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.runtime.translators;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.ExecutionConfig;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.connector.sink.Sink;
+import org.apache.flink.api.dag.Transformation;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.streaming.api.graph.StreamGraph;
+import org.apache.flink.streaming.api.graph.TransformationTranslator;
+import org.apache.flink.streaming.api.operators.ChainingStrategy;
+import org.apache.flink.streaming.api.operators.OneInputStreamOperatorFactory;
+import org.apache.flink.streaming.api.operators.StreamOperatorFactory;
+import org.apache.flink.streaming.api.transformations.SinkTransformation;
+import org.apache.flink.streaming.runtime.operators.sink.BatchCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.BatchGlobalCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.GlobalStreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatefulWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatelessWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.util.graph.StreamGraphUtils;
+
+import java.util.Collection;
+import java.util.Collections;
+
+/**
+ * A {@link TransformationTranslator} for the {@link SinkTransformation}.
+ */
+@Internal
+public class SinkTransformationTranslator<InputT, CommT, WriterStateT, GlobalCommT> implements
+		TransformationTranslator<Object, SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT>> {
+
+	@Override
+	public Collection<Integer> translateForBatch(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+
+		int writerId = addWriter(transformation, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new BatchCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new BatchGlobalCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		return Collections.emptyList();
+	}
+
+	@Override
+	public Collection<Integer> translateForStreaming(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+
+		int writerId = addWriter(transformation, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new StreamingCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new GlobalStreamingCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+
+		return Collections.emptyList();
+	}
+
+	/**
+	 * Add a sink writer node to the stream graph.
+	 *
+	 * @param sinkTransformation The transformation that the writer belongs to
+	 *
+	 * @return The stream node id of the writer
+	 */
+	private int addWriter(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		final boolean hasState = sinkTransformation
+				.getSink()
+				.getWriterStateSerializer()
+				.isPresent();
+		final TypeInformation<CommT> outTypeInfo = TypeExtractor.createTypeInfo(

Review comment:
       yes.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot commented on pull request #13824: [FLINK-19736] Add the SinkTransformation

Posted by GitBox <gi...@apache.org>.
flinkbot commented on pull request #13824:
URL: https://github.com/apache/flink/pull/13824#issuecomment-717883808


   Thanks a lot for your contribution to the Apache Flink project. I'm the @flinkbot. I help the community
   to review your pull request. We will use this comment to track the progress of the review.
   
   
   ## Automated Checks
   Last check on commit a10e3dcdaf7ff215ec455ce60ca7594651ad8d04 (Wed Oct 28 11:54:03 UTC 2020)
   
   **Warnings:**
    * No documentation files were touched! Remember to keep the Flink docs up to date!
    * **This pull request references an unassigned [Jira ticket](https://issues.apache.org/jira/browse/FLINK-19736).** According to the [code contribution guide](https://flink.apache.org/contributing/contribute-code.html), tickets need to be assigned before starting with the implementation work.
   
   
   <sub>Mention the bot in a comment to re-run the automated checks.</sub>
   ## Review Progress
   
   * ❓ 1. The [description] looks good.
   * ❓ 2. There is [consensus] that the contribution should go into to Flink.
   * ❓ 3. Needs [attention] from.
   * ❓ 4. The change fits into the overall [architecture].
   * ❓ 5. Overall code [quality] is good.
   
   Please see the [Pull Request Review Guide](https://flink.apache.org/contributing/reviewing-prs.html) for a full explanation of the review process.<details>
    The Bot is tracking the review progress through labels. Labels are applied according to the order of the review items. For consensus, approval by a Flink committer of PMC member is required <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot approve description` to approve one or more aspects (aspects: `description`, `consensus`, `architecture` and `quality`)
    - `@flinkbot approve all` to approve all aspects
    - `@flinkbot approve-until architecture` to approve everything until `architecture`
    - `@flinkbot attention @username1 [@username2 ..]` to require somebody's attention
    - `@flinkbot disapprove architecture` to remove an approval you gave earlier
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] guoweiM commented on a change in pull request #13824: [FLINK-19736] Add the SinkTransformation

Posted by GitBox <gi...@apache.org>.
guoweiM commented on a change in pull request #13824:
URL: https://github.com/apache/flink/pull/13824#discussion_r514942680



##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/translators/SinkTransformationTranslator.java
##########
@@ -0,0 +1,292 @@
+/*
+ * 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.runtime.translators;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.ExecutionConfig;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.connector.sink.Sink;
+import org.apache.flink.api.dag.Transformation;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.streaming.api.graph.StreamGraph;
+import org.apache.flink.streaming.api.graph.TransformationTranslator;
+import org.apache.flink.streaming.api.operators.ChainingStrategy;
+import org.apache.flink.streaming.api.operators.OneInputStreamOperatorFactory;
+import org.apache.flink.streaming.api.operators.StreamOperatorFactory;
+import org.apache.flink.streaming.api.transformations.SinkTransformation;
+import org.apache.flink.streaming.runtime.operators.sink.BatchCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.BatchGlobalCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.GlobalStreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatefulWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatelessWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.util.graph.StreamGraphUtils;
+import org.apache.flink.streaming.util.typeutils.CommittableTypeInformation;
+
+import java.lang.reflect.Type;
+import java.util.Collection;
+import java.util.Collections;
+
+import static org.apache.flink.api.java.typeutils.TypeExtractionUtils.typeToClass;
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * A {@link TransformationTranslator} for the {@link SinkTransformation}.
+ */
+@Internal
+public class SinkTransformationTranslator<InputT, CommT, WriterStateT, GlobalCommT> implements
+		TransformationTranslator<Object, SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT>> {
+
+	@Override
+	public Collection<Integer> translateForBatch(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+		final int parallelism = getParallelism(transformation, context);
+
+		int writerId = addWriter(
+				transformation,
+				parallelism, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new BatchCommitterOperatorFactory<>(transformation.getSink()),
+				parallelism, context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new BatchGlobalCommitterOperatorFactory<>(transformation.getSink()), context);
+		return Collections.emptyList();
+	}
+
+	@Override
+	public Collection<Integer> translateForStreaming(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+
+		final int parallelism = getParallelism(transformation, context);
+
+		int writerId = addWriter(
+				transformation,
+				parallelism, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new StreamingCommitterOperatorFactory<>(transformation.getSink()),
+				parallelism, context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new GlobalStreamingCommitterOperatorFactory<>(transformation.getSink()), context);
+
+		return Collections.emptyList();
+	}
+
+	/**
+	 * Add a sink writer node to the stream graph.
+	 *
+	 * @param sinkTransformation The transformation that the writer belongs to
+	 * @param parallelism The parallelism of the writer
+	 *
+	 * @return The stream node id of the writer
+	 */
+	private int addWriter(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			int parallelism,
+			Context context) {
+		final boolean hasState = sinkTransformation
+				.getSink()
+				.getWriterStateSerializer()
+				.isPresent();
+		checkState(sinkTransformation.getInputs().size() == 1);
+		@SuppressWarnings("unchecked")
+		final Transformation<InputT> input = (Transformation<InputT>) sinkTransformation
+				.getInputs()
+				.get(0);
+		final TypeInformation<InputT> inputTypeInfo = input.getOutputType();
+
+		final StreamOperatorFactory<CommT> writer =
+				hasState ? new StatefulWriterOperatorFactory<>(sinkTransformation.getSink()) : new StatelessWriterOperatorFactory<>(
+						sinkTransformation.getSink());
+
+		final int writerId = addOperatorToStreamGraph(
+				writer, input.getId(),
+				inputTypeInfo,
+				extractCommittableTypeInformation(sinkTransformation.getSink()),
+				"Sink Writer:",
+				parallelism,
+				sinkTransformation.getMaxParallelism(),
+				sinkTransformation,
+				context);
+
+		StreamGraphUtils.configureResourceProperties(
+				context.getStreamGraph(),
+				writerId,
+				sinkTransformation);
+		return writerId;
+	}
+
+	/**
+	 * Try to add a sink committer to the stream graph.
+	 *
+	 * @param inputId The committer's input stream node id
+	 * @param sinkTransformation The transformation that the committer belongs to
+	 * @param committerFactory The committer operator's factory
+	 * @param parallelism The parallelism of the committer
+	 *
+	 * @return The stream node id of the committer or -1 if the sink topology does not include a committer.
+	 */
+	private int addCommitter(
+			int inputId,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			OneInputStreamOperatorFactory<CommT, CommT> committerFactory,
+			int parallelism,
+			Context context) {
+
+		if (!sinkTransformation.getSink().createCommitter().isPresent()) {
+			return -1;
+		}
+
+		final CommittableTypeInformation<CommT> committableTypeInfo = extractCommittableTypeInformation(
+				sinkTransformation.getSink());
+		checkNotNull(committableTypeInfo);
+
+		return addOperatorToStreamGraph(
+				committerFactory, inputId,
+				committableTypeInfo,
+				committableTypeInfo,
+				"Sink Committer:",
+				parallelism,
+				sinkTransformation.getMaxParallelism(),
+				sinkTransformation,
+				context);
+	}
+
+	/**
+	 * Try to add a sink global committer to the stream graph.
+	 *
+	 * @param inputId The global committer's input stream node id.
+	 * @param sinkTransformation The transformation that the global committer belongs to
+	 * @param globalCommitterFactory The global committer factory
+	 */
+	private void addGlobalCommitter(
+			int inputId,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			OneInputStreamOperatorFactory<CommT, GlobalCommT> globalCommitterFactory,
+			Context context) {
+
+		if (!sinkTransformation.getSink().createGlobalCommitter().isPresent()) {
+			return;
+		}
+
+		addOperatorToStreamGraph(
+				globalCommitterFactory, inputId,
+				checkNotNull(extractCommittableTypeInformation(sinkTransformation.getSink())),
+				null,
+				"Sink Global Committer:",
+				1,
+				1,
+				sinkTransformation,
+				context);
+	}
+
+	private int getParallelism(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		return sinkTransformation.getParallelism() != ExecutionConfig.PARALLELISM_DEFAULT
+				? sinkTransformation.getParallelism()
+				: context.getStreamGraph().getExecutionConfig().getParallelism();
+	}
+
+	/**
+	 * Add a operator to the {@link StreamGraph}.
+	 *
+	 * @param operatorFactory The operator factory
+	 * @param inputId The upstream stream node id of the operator
+	 * @param inTypeInfo The input type information of the operator
+	 * @param outTypInfo The output type information of the operator
+	 * @param prefix The prefix of the name and uid of the operator
+	 * @param parallelism The parallelism of the operator
+	 * @param maxParallelism The max parallelism of the operator
+	 * @param sinkTransformation The sink transformation which the operator belongs to
+	 *
+	 * @return The stream node id of the operator
+	 */
+	private <IN, OUT> int addOperatorToStreamGraph(
+			StreamOperatorFactory<OUT> operatorFactory, int inputId,
+			TypeInformation<IN> inTypeInfo,
+			TypeInformation<OUT> outTypInfo,
+			String prefix,
+			int parallelism,
+			int maxParallelism,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		final StreamGraph streamGraph = context.getStreamGraph();
+		final String slotSharingGroup = context.getSlotSharingGroup();
+		final int transformationId = Transformation.getNewNodeId();
+
+		final ChainingStrategy chainingStrategy = sinkTransformation.getChainingStrategy();
+		if (chainingStrategy != null) {
+			operatorFactory.setChainingStrategy(chainingStrategy);
+		}
+
+		streamGraph.addOperator(
+				transformationId,
+				slotSharingGroup,
+				sinkTransformation.getCoLocationGroupKey(),
+				operatorFactory,
+				inTypeInfo,
+				outTypInfo,
+				String.format("%s %s", prefix, sinkTransformation.getName()));
+
+		streamGraph.setParallelism(transformationId, parallelism);
+		streamGraph.setMaxParallelism(transformationId, maxParallelism);
+
+		StreamGraphUtils.configureBufferTimeout(
+				streamGraph,
+				transformationId,
+				sinkTransformation,
+				context.getDefaultBufferTimeout());
+		if (sinkTransformation.getUid() != null) {
+			streamGraph.setTransformationUID(
+					transformationId,
+					String.format("%s %s", prefix, sinkTransformation.getUid()));
+		}
+		streamGraph.addEdge(inputId, transformationId, 0);
+
+		return transformationId;
+	}
+
+	private CommittableTypeInformation<CommT> extractCommittableTypeInformation(Sink<InputT, CommT, WriterStateT, GlobalCommT> sink) {

Review comment:
       CommittableTypeInformation could not be extracted successfully. According to the topology sometime it is valid sometime it is not. It is naturally to do the validation during creating the operator node. 
   
   I could see the benefit of putting it at the begin of the translation. But If we put the extracting logical at the begin of the translation we also need to add the validate logical according to the topology.  As a result we would have some if/else for extracting and building topology at the very beginning, which might be not so good to understand.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13824: [FLINK-19736] Add the SinkTransformation

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13824:
URL: https://github.com/apache/flink/pull/13824#issuecomment-717895470


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "a10e3dcdaf7ff215ec455ce60ca7594651ad8d04",
       "status" : "FAILURE",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8509",
       "triggerID" : "a10e3dcdaf7ff215ec455ce60ca7594651ad8d04",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * a10e3dcdaf7ff215ec455ce60ca7594651ad8d04 Azure: [FAILURE](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8509) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13824: [FLINK-19736] Add the SinkTransformation

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13824:
URL: https://github.com/apache/flink/pull/13824#issuecomment-717895470


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "a10e3dcdaf7ff215ec455ce60ca7594651ad8d04",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8509",
       "triggerID" : "a10e3dcdaf7ff215ec455ce60ca7594651ad8d04",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * a10e3dcdaf7ff215ec455ce60ca7594651ad8d04 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8509) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13824: [FLINK-19736] Add the SinkTransformation

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13824:
URL: https://github.com/apache/flink/pull/13824#issuecomment-717895470


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "a10e3dcdaf7ff215ec455ce60ca7594651ad8d04",
       "status" : "FAILURE",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8509",
       "triggerID" : "a10e3dcdaf7ff215ec455ce60ca7594651ad8d04",
       "triggerType" : "PUSH"
     }, {
       "hash" : "140a65e1ecfaf98837a4510c548cb8c14a9ed380",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8617",
       "triggerID" : "140a65e1ecfaf98837a4510c548cb8c14a9ed380",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * a10e3dcdaf7ff215ec455ce60ca7594651ad8d04 Azure: [FAILURE](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8509) 
   * 140a65e1ecfaf98837a4510c548cb8c14a9ed380 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8617) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] guoweiM commented on a change in pull request #13824: [FLINK-19736] Add the SinkTransformation

Posted by GitBox <gi...@apache.org>.
guoweiM commented on a change in pull request #13824:
URL: https://github.com/apache/flink/pull/13824#discussion_r514929314



##########
File path: flink-tests/src/test/java/org/apache/flink/test/streaming/runtime/StreamSinkITCase.java
##########
@@ -0,0 +1,119 @@
+/*
+ * 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.test.streaming.runtime;
+
+import org.apache.flink.api.common.RuntimeExecutionMode;
+import org.apache.flink.api.common.typeinfo.IntegerTypeInfo;
+import org.apache.flink.api.java.tuple.Tuple3;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.configuration.ExecutionOptions;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.streaming.runtime.operators.sink.TestSink;
+import org.apache.flink.streaming.util.FiniteTestSource;
+import org.apache.flink.test.util.AbstractTestBase;
+
+import org.junit.Test;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+
+import static java.util.stream.Collectors.joining;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.containsInAnyOrder;
+
+/**
+ * Integration test for {@link org.apache.flink.api.connector.sink.Sink} run time implementation.
+ */
+public class StreamSinkITCase extends AbstractTestBase {
+
+	static final List<Integer> SOURCE_DATA = Arrays.asList(
+			895, 127, 148, 161, 148, 662, 822, 491, 275, 122,
+			850, 630, 682, 765, 434, 970, 714, 795, 288, 422);
+
+	static final Queue<String> STREAMING_COMMIT_QUEUE = new ConcurrentLinkedQueue<>();
+
+	static final Queue<String> STREAMING_GLOBAL_COMMIT_QUEUE = new ConcurrentLinkedQueue<>();
+
+	static final Queue<String> BATCH_COMMIT_QUEUE = new ConcurrentLinkedQueue<>();
+
+	static final Queue<String> BATCH_GLOBAL_COMMIT_QUEUE = new ConcurrentLinkedQueue<>();
+
+	@Test
+	public void streamingExecutionMode() throws Exception {
+		StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
+		env.enableCheckpointing(100);
+		FiniteTestSource<Integer> source = new FiniteTestSource<>(SOURCE_DATA);
+
+		env.addSource(source, IntegerTypeInfo.INT_TYPE_INFO)
+				.addSink(TestSink
+						.newBuilder()
+						.setDefaultCommitter((Supplier<Queue<String>> & Serializable) () -> STREAMING_COMMIT_QUEUE)
+						.setGlobalCommitter((Supplier<Queue<String>> & Serializable) () -> STREAMING_GLOBAL_COMMIT_QUEUE)
+						.build());
+
+		env.execute();
+
+		final List<String> expectedCommittedData = SOURCE_DATA
+				.stream()
+				.map(x -> Tuple3.of(x, null, Long.MIN_VALUE).toString())
+				.collect(
+						Collectors.toList());
+
+		// source send data two times
+		expectedCommittedData.addAll(expectedCommittedData);

Review comment:
       thanks. I have already done like this. 




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13824: [FLINK-19736] Add the SinkTransformation

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13824:
URL: https://github.com/apache/flink/pull/13824#issuecomment-717895470


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "a10e3dcdaf7ff215ec455ce60ca7594651ad8d04",
       "status" : "FAILURE",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8509",
       "triggerID" : "a10e3dcdaf7ff215ec455ce60ca7594651ad8d04",
       "triggerType" : "PUSH"
     }, {
       "hash" : "140a65e1ecfaf98837a4510c548cb8c14a9ed380",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "140a65e1ecfaf98837a4510c548cb8c14a9ed380",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * a10e3dcdaf7ff215ec455ce60ca7594651ad8d04 Azure: [FAILURE](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8509) 
   * 140a65e1ecfaf98837a4510c548cb8c14a9ed380 UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] guoweiM commented on a change in pull request #13824: [FLINK-19736] Add the SinkTransformation

Posted by GitBox <gi...@apache.org>.
guoweiM commented on a change in pull request #13824:
URL: https://github.com/apache/flink/pull/13824#discussion_r514355921



##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/translators/SinkTransformationTranslator.java
##########
@@ -0,0 +1,283 @@
+/*
+ * 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.runtime.translators;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.ExecutionConfig;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.connector.sink.Sink;
+import org.apache.flink.api.dag.Transformation;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.streaming.api.graph.StreamGraph;
+import org.apache.flink.streaming.api.graph.TransformationTranslator;
+import org.apache.flink.streaming.api.operators.ChainingStrategy;
+import org.apache.flink.streaming.api.operators.OneInputStreamOperatorFactory;
+import org.apache.flink.streaming.api.operators.StreamOperatorFactory;
+import org.apache.flink.streaming.api.transformations.SinkTransformation;
+import org.apache.flink.streaming.runtime.operators.sink.BatchCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.BatchGlobalCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.GlobalStreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatefulWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatelessWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.util.graph.StreamGraphUtils;
+
+import java.util.Collection;
+import java.util.Collections;
+
+/**
+ * A {@link TransformationTranslator} for the {@link SinkTransformation}.
+ */
+@Internal
+public class SinkTransformationTranslator<InputT, CommT, WriterStateT, GlobalCommT> implements
+		TransformationTranslator<Object, SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT>> {
+
+	@Override
+	public Collection<Integer> translateForBatch(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+
+		int writerId = addWriter(transformation, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new BatchCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new BatchGlobalCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		return Collections.emptyList();
+	}
+
+	@Override
+	public Collection<Integer> translateForStreaming(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+
+		int writerId = addWriter(transformation, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new StreamingCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new GlobalStreamingCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+
+		return Collections.emptyList();
+	}
+
+	/**
+	 * Add a sink writer node to the stream graph.
+	 *
+	 * @param sinkTransformation The transformation that the writer belongs to
+	 *
+	 * @return The stream node id of the writer
+	 */
+	private int addWriter(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		final boolean hasState = sinkTransformation
+				.getSink()
+				.getWriterStateSerializer()
+				.isPresent();
+		final TypeInformation<CommT> outTypeInfo = TypeExtractor.createTypeInfo(
+				Sink.class,
+				sinkTransformation.getSink().getClass(),
+				1,
+				null,
+				null);
+		@SuppressWarnings("unchecked")
+		final Transformation<InputT> input = (Transformation<InputT>) sinkTransformation
+				.getInputs()
+				.get(0);
+		final TypeInformation<InputT> inputTypeInfo = input.getOutputType();
+
+		final int parallelism = getParallelism(sinkTransformation, context);
+
+		final StreamOperatorFactory<CommT> writer =
+				hasState ? new StatefulWriterOperatorFactory<>(sinkTransformation.getSink()) : new StatelessWriterOperatorFactory<>(
+						sinkTransformation.getSink());
+
+		final int writerId = addOperatorToStreamGraph(
+				writer, input.getId(),
+				inputTypeInfo,
+				outTypeInfo,
+				"Sink Writer:",
+				parallelism,
+				sinkTransformation.getMaxParallelism(),
+				sinkTransformation,
+				context);
+
+		StreamGraphUtils.configureResourceProperties(
+				context.getStreamGraph(),
+				writerId,
+				sinkTransformation);
+		return writerId;
+	}
+
+	/**
+	 * Try to add a sink committer to the stream graph.
+	 *
+	 * @param inputId The committer's input stream node id
+	 * @param sinkTransformation The transformation that the committer belongs to
+	 * @param committerFactory The committer operator's factory
+	 *
+	 * @return The stream node id of the committer or -1 if the sink topology does not include a committer.
+	 */
+	private int addCommitter(
+			int inputId,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			OneInputStreamOperatorFactory<CommT, CommT> committerFactory,
+			Context context) {
+
+		if (!sinkTransformation.getSink().getCommittableSerializer().isPresent()) {
+			return -1;
+		}
+
+		final TypeInformation<CommT> committableTypeInfo = TypeExtractor.createTypeInfo(
+				Sink.class,
+				sinkTransformation.getSink().getClass(),
+				1,
+				null,
+				null);
+
+		final int parallelism = getParallelism(sinkTransformation, context);

Review comment:
       I agree this would need some discussion.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] kl0u commented on a change in pull request #13824: [FLINK-19736] Add the SinkTransformation

Posted by GitBox <gi...@apache.org>.
kl0u commented on a change in pull request #13824:
URL: https://github.com/apache/flink/pull/13824#discussion_r514960481



##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/translators/SinkTransformationTranslator.java
##########
@@ -0,0 +1,293 @@
+/*
+ * 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.runtime.translators;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.ExecutionConfig;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.connector.sink.Sink;
+import org.apache.flink.api.dag.Transformation;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.streaming.api.graph.StreamGraph;
+import org.apache.flink.streaming.api.graph.TransformationTranslator;
+import org.apache.flink.streaming.api.operators.ChainingStrategy;
+import org.apache.flink.streaming.api.operators.OneInputStreamOperatorFactory;
+import org.apache.flink.streaming.api.operators.StreamOperatorFactory;
+import org.apache.flink.streaming.api.transformations.SinkTransformation;
+import org.apache.flink.streaming.runtime.operators.sink.BatchCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.BatchGlobalCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.GlobalStreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatefulWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatelessWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.util.graph.StreamGraphUtils;
+import org.apache.flink.streaming.util.typeutils.CommittableTypeInformation;
+
+import java.lang.reflect.Type;
+import java.util.Collection;
+import java.util.Collections;
+
+import static org.apache.flink.api.java.typeutils.TypeExtractionUtils.typeToClass;
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * A {@link TransformationTranslator} for the {@link SinkTransformation}.
+ */
+@Internal
+public class SinkTransformationTranslator<InputT, CommT, WriterStateT, GlobalCommT> implements
+		TransformationTranslator<Object, SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT>> {
+
+	@Override
+	public Collection<Integer> translateForBatch(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+		final int parallelism = getParallelism(transformation, context);
+
+		int writerId = addWriter(
+				transformation,
+				parallelism, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new BatchCommitterOperatorFactory<>(transformation.getSink()),
+				parallelism, context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new BatchGlobalCommitterOperatorFactory<>(transformation.getSink()), context);
+		return Collections.emptyList();
+	}
+
+	@Override
+	public Collection<Integer> translateForStreaming(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+
+		final int parallelism = getParallelism(transformation, context);
+
+		int writerId = addWriter(
+				transformation,
+				parallelism, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new StreamingCommitterOperatorFactory<>(transformation.getSink()),
+				parallelism, context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new GlobalStreamingCommitterOperatorFactory<>(transformation.getSink()), context);
+
+		return Collections.emptyList();
+	}
+
+	/**
+	 * Add a sink writer node to the stream graph.
+	 *
+	 * @param sinkTransformation The transformation that the writer belongs to
+	 * @param parallelism The parallelism of the writer
+	 *
+	 * @return The stream node id of the writer
+	 */
+	private int addWriter(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			int parallelism,
+			Context context) {
+		final boolean hasState = sinkTransformation
+				.getSink()
+				.getWriterStateSerializer()
+				.isPresent();
+		checkState(sinkTransformation.getInputs().size() == 1);
+		@SuppressWarnings("unchecked")
+		final Transformation<InputT> input = (Transformation<InputT>) sinkTransformation
+				.getInputs()
+				.get(0);
+		final TypeInformation<InputT> inputTypeInfo = input.getOutputType();
+
+		final StreamOperatorFactory<CommT> writer =
+				hasState ? new StatefulWriterOperatorFactory<>(sinkTransformation.getSink()) : new StatelessWriterOperatorFactory<>(
+						sinkTransformation.getSink());
+
+		final int writerId = addOperatorToStreamGraph(
+				writer, input.getId(),
+				inputTypeInfo,
+				extractCommittableTypeInformation(sinkTransformation.getSink()),

Review comment:
       In general in the translation process here we make an implicit assumption that if we do not have a `Committer`, then we also do not have a `GlobalCommitter`, right? Because when we set the output type of the `Writer` we always set it to `CommT`. In addition, when we extract the output type of the writer in the `extractCommittableTypeInformation()` we only look if there is a `committableSerializer`. If this is not present then the `Writer` cannot communicate with the `GlobalCommitter`.
   
   Is this something we want? And if yes, then we should have a validation step for the sink that throws an exception if we have a sink with a `GlobalCommitter` but without a `Committer`. If we do not want that, then I think we should adjust the `addWrier`. Is this analysis correct @guoweiM ? 
   	




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] kl0u commented on a change in pull request #13824: [FLINK-19736] Add the SinkTransformation

Posted by GitBox <gi...@apache.org>.
kl0u commented on a change in pull request #13824:
URL: https://github.com/apache/flink/pull/13824#discussion_r514965363



##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/translators/SinkTransformationTranslator.java
##########
@@ -0,0 +1,293 @@
+/*
+ * 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.runtime.translators;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.ExecutionConfig;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.connector.sink.Sink;
+import org.apache.flink.api.dag.Transformation;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.streaming.api.graph.StreamGraph;
+import org.apache.flink.streaming.api.graph.TransformationTranslator;
+import org.apache.flink.streaming.api.operators.ChainingStrategy;
+import org.apache.flink.streaming.api.operators.OneInputStreamOperatorFactory;
+import org.apache.flink.streaming.api.operators.StreamOperatorFactory;
+import org.apache.flink.streaming.api.transformations.SinkTransformation;
+import org.apache.flink.streaming.runtime.operators.sink.BatchCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.BatchGlobalCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.GlobalStreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatefulWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatelessWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.util.graph.StreamGraphUtils;
+import org.apache.flink.streaming.util.typeutils.CommittableTypeInformation;
+
+import java.lang.reflect.Type;
+import java.util.Collection;
+import java.util.Collections;
+
+import static org.apache.flink.api.java.typeutils.TypeExtractionUtils.typeToClass;
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * A {@link TransformationTranslator} for the {@link SinkTransformation}.
+ */
+@Internal
+public class SinkTransformationTranslator<InputT, CommT, WriterStateT, GlobalCommT> implements
+		TransformationTranslator<Object, SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT>> {
+
+	@Override
+	public Collection<Integer> translateForBatch(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+		final int parallelism = getParallelism(transformation, context);
+
+		int writerId = addWriter(
+				transformation,
+				parallelism, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new BatchCommitterOperatorFactory<>(transformation.getSink()),
+				parallelism, context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new BatchGlobalCommitterOperatorFactory<>(transformation.getSink()), context);
+		return Collections.emptyList();
+	}
+
+	@Override
+	public Collection<Integer> translateForStreaming(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+
+		final int parallelism = getParallelism(transformation, context);
+
+		int writerId = addWriter(
+				transformation,
+				parallelism, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new StreamingCommitterOperatorFactory<>(transformation.getSink()),
+				parallelism, context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new GlobalStreamingCommitterOperatorFactory<>(transformation.getSink()), context);
+
+		return Collections.emptyList();
+	}
+
+	/**
+	 * Add a sink writer node to the stream graph.
+	 *
+	 * @param sinkTransformation The transformation that the writer belongs to
+	 * @param parallelism The parallelism of the writer
+	 *
+	 * @return The stream node id of the writer
+	 */
+	private int addWriter(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			int parallelism,
+			Context context) {
+		final boolean hasState = sinkTransformation
+				.getSink()
+				.getWriterStateSerializer()
+				.isPresent();
+		checkState(sinkTransformation.getInputs().size() == 1);
+		@SuppressWarnings("unchecked")
+		final Transformation<InputT> input = (Transformation<InputT>) sinkTransformation
+				.getInputs()
+				.get(0);
+		final TypeInformation<InputT> inputTypeInfo = input.getOutputType();
+
+		final StreamOperatorFactory<CommT> writer =
+				hasState ? new StatefulWriterOperatorFactory<>(sinkTransformation.getSink()) : new StatelessWriterOperatorFactory<>(
+						sinkTransformation.getSink());
+
+		final int writerId = addOperatorToStreamGraph(
+				writer, input.getId(),
+				inputTypeInfo,
+				extractCommittableTypeInformation(sinkTransformation.getSink()),
+				"Sink Writer:",
+				parallelism,
+				sinkTransformation.getMaxParallelism(),
+				sinkTransformation,
+				context);
+
+		StreamGraphUtils.configureResourceProperties(
+				context.getStreamGraph(),
+				writerId,
+				sinkTransformation);
+		return writerId;
+	}
+
+	/**
+	 * Try to add a sink committer to the stream graph.
+	 *
+	 * @param inputId The committer's input stream node id
+	 * @param sinkTransformation The transformation that the committer belongs to
+	 * @param committerFactory The committer operator's factory
+	 * @param parallelism The parallelism of the committer
+	 *
+	 * @return The stream node id of the committer or -1 if the sink topology does not include a committer.
+	 */
+	private int addCommitter(
+			int inputId,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			OneInputStreamOperatorFactory<CommT, CommT> committerFactory,
+			int parallelism,
+			Context context) {
+
+		if (!sinkTransformation.getSink().createCommitter().isPresent()) {
+			return -1;
+		}
+
+		final CommittableTypeInformation<CommT> committableTypeInfo = extractCommittableTypeInformation(
+				sinkTransformation.getSink());
+		checkNotNull(committableTypeInfo);
+
+		return addOperatorToStreamGraph(
+				committerFactory, inputId,
+				committableTypeInfo,
+				committableTypeInfo,
+				"Sink Committer:",
+				parallelism,
+				sinkTransformation.getMaxParallelism(),
+				sinkTransformation,
+				context);
+	}
+
+	/**
+	 * Try to add a sink global committer to the stream graph.
+	 *
+	 * @param inputId The global committer's input stream node id.
+	 * @param sinkTransformation The transformation that the global committer belongs to
+	 * @param globalCommitterFactory The global committer factory
+	 */
+	private void addGlobalCommitter(
+			int inputId,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			OneInputStreamOperatorFactory<CommT, GlobalCommT> globalCommitterFactory,
+			Context context) {
+
+		if (!sinkTransformation.getSink().createGlobalCommitter().isPresent()) {
+			return;
+		}
+
+		addOperatorToStreamGraph(
+				globalCommitterFactory, inputId,
+				checkNotNull(extractCommittableTypeInformation(sinkTransformation.getSink())),
+				null,
+				"Sink Global Committer:",
+				1,
+				1,
+				sinkTransformation,
+				context);
+	}
+
+	//TODO maybe move to a util class
+	private int getParallelism(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		return sinkTransformation.getParallelism() != ExecutionConfig.PARALLELISM_DEFAULT
+				? sinkTransformation.getParallelism()
+				: context.getStreamGraph().getExecutionConfig().getParallelism();
+	}
+
+	/**
+	 * Add a operator to the {@link StreamGraph}.
+	 *
+	 * @param operatorFactory The operator factory
+	 * @param inputId The upstream stream node id of the operator
+	 * @param inTypeInfo The input type information of the operator
+	 * @param outTypInfo The output type information of the operator
+	 * @param prefix The prefix of the name and uid of the operator
+	 * @param parallelism The parallelism of the operator
+	 * @param maxParallelism The max parallelism of the operator
+	 * @param sinkTransformation The sink transformation which the operator belongs to
+	 *
+	 * @return The stream node id of the operator
+	 */
+	private <IN, OUT> int addOperatorToStreamGraph(
+			StreamOperatorFactory<OUT> operatorFactory, int inputId,
+			TypeInformation<IN> inTypeInfo,
+			TypeInformation<OUT> outTypInfo,
+			String prefix,
+			int parallelism,
+			int maxParallelism,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		final StreamGraph streamGraph = context.getStreamGraph();
+		final String slotSharingGroup = context.getSlotSharingGroup();
+		final int transformationId = Transformation.getNewNodeId();
+
+		final ChainingStrategy chainingStrategy = sinkTransformation.getChainingStrategy();
+		if (chainingStrategy != null) {
+			operatorFactory.setChainingStrategy(chainingStrategy);
+		}
+
+		streamGraph.addOperator(
+				transformationId,
+				slotSharingGroup,
+				sinkTransformation.getCoLocationGroupKey(),
+				operatorFactory,
+				inTypeInfo,
+				outTypInfo,
+				String.format("%s %s", prefix, sinkTransformation.getName()));
+
+		streamGraph.setParallelism(transformationId, parallelism);
+		streamGraph.setMaxParallelism(transformationId, maxParallelism);
+
+		StreamGraphUtils.configureBufferTimeout(
+				streamGraph,
+				transformationId,
+				sinkTransformation,
+				context.getDefaultBufferTimeout());
+		if (sinkTransformation.getUid() != null) {
+			streamGraph.setTransformationUID(
+					transformationId,
+					String.format("%s %s", prefix, sinkTransformation.getUid()));
+		}
+		streamGraph.addEdge(inputId, transformationId, 0);
+
+		return transformationId;
+	}
+
+	private CommittableTypeInformation<CommT> extractCommittableTypeInformation(Sink<InputT, CommT, WriterStateT, GlobalCommT> sink) {
+		if (sink.getCommittableSerializer().isPresent()) {
+			final Type committableType = TypeExtractor.getParameterType(
+					Sink.class,
+					sink.getClass(),
+					1);

Review comment:
       Is this whole type extraction logic correct? Is the `1` position correct? @aljoscha could you also have a look on this one? 




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] kl0u commented on a change in pull request #13824: [FLINK-19736] Add the SinkTransformation

Posted by GitBox <gi...@apache.org>.
kl0u commented on a change in pull request #13824:
URL: https://github.com/apache/flink/pull/13824#discussion_r514942224



##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/translators/SinkTransformationTranslator.java
##########
@@ -0,0 +1,283 @@
+/*
+ * 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.runtime.translators;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.ExecutionConfig;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.connector.sink.Sink;
+import org.apache.flink.api.dag.Transformation;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.streaming.api.graph.StreamGraph;
+import org.apache.flink.streaming.api.graph.TransformationTranslator;
+import org.apache.flink.streaming.api.operators.ChainingStrategy;
+import org.apache.flink.streaming.api.operators.OneInputStreamOperatorFactory;
+import org.apache.flink.streaming.api.operators.StreamOperatorFactory;
+import org.apache.flink.streaming.api.transformations.SinkTransformation;
+import org.apache.flink.streaming.runtime.operators.sink.BatchCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.BatchGlobalCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.GlobalStreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatefulWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatelessWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.util.graph.StreamGraphUtils;
+
+import java.util.Collection;
+import java.util.Collections;
+
+/**
+ * A {@link TransformationTranslator} for the {@link SinkTransformation}.
+ */
+@Internal
+public class SinkTransformationTranslator<InputT, CommT, WriterStateT, GlobalCommT> implements
+		TransformationTranslator<Object, SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT>> {
+
+	@Override
+	public Collection<Integer> translateForBatch(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+
+		int writerId = addWriter(transformation, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new BatchCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new BatchGlobalCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		return Collections.emptyList();
+	}
+
+	@Override
+	public Collection<Integer> translateForStreaming(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+
+		int writerId = addWriter(transformation, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new StreamingCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new GlobalStreamingCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+
+		return Collections.emptyList();
+	}
+
+	/**
+	 * Add a sink writer node to the stream graph.
+	 *
+	 * @param sinkTransformation The transformation that the writer belongs to
+	 *
+	 * @return The stream node id of the writer
+	 */
+	private int addWriter(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		final boolean hasState = sinkTransformation
+				.getSink()
+				.getWriterStateSerializer()
+				.isPresent();
+		final TypeInformation<CommT> outTypeInfo = TypeExtractor.createTypeInfo(
+				Sink.class,
+				sinkTransformation.getSink().getClass(),
+				1,
+				null,
+				null);
+		@SuppressWarnings("unchecked")
+		final Transformation<InputT> input = (Transformation<InputT>) sinkTransformation
+				.getInputs()
+				.get(0);
+		final TypeInformation<InputT> inputTypeInfo = input.getOutputType();
+
+		final int parallelism = getParallelism(sinkTransformation, context);
+
+		final StreamOperatorFactory<CommT> writer =
+				hasState ? new StatefulWriterOperatorFactory<>(sinkTransformation.getSink()) : new StatelessWriterOperatorFactory<>(
+						sinkTransformation.getSink());
+
+		final int writerId = addOperatorToStreamGraph(
+				writer, input.getId(),
+				inputTypeInfo,
+				outTypeInfo,
+				"Sink Writer:",
+				parallelism,
+				sinkTransformation.getMaxParallelism(),
+				sinkTransformation,
+				context);
+
+		StreamGraphUtils.configureResourceProperties(
+				context.getStreamGraph(),
+				writerId,
+				sinkTransformation);
+		return writerId;
+	}
+
+	/**
+	 * Try to add a sink committer to the stream graph.
+	 *
+	 * @param inputId The committer's input stream node id
+	 * @param sinkTransformation The transformation that the committer belongs to
+	 * @param committerFactory The committer operator's factory
+	 *
+	 * @return The stream node id of the committer or -1 if the sink topology does not include a committer.
+	 */
+	private int addCommitter(
+			int inputId,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			OneInputStreamOperatorFactory<CommT, CommT> committerFactory,
+			Context context) {
+
+		if (!sinkTransformation.getSink().getCommittableSerializer().isPresent()) {
+			return -1;
+		}
+
+		final TypeInformation<CommT> committableTypeInfo = TypeExtractor.createTypeInfo(
+				Sink.class,
+				sinkTransformation.getSink().getClass(),
+				1,
+				null,
+				null);
+
+		final int parallelism = getParallelism(sinkTransformation, context);
+
+		return addOperatorToStreamGraph(
+				committerFactory, inputId,
+				committableTypeInfo,
+				committableTypeInfo,
+				"Sink Committer:",
+				parallelism,
+				sinkTransformation.getMaxParallelism(),
+				sinkTransformation,
+				context);
+	}
+
+	/**
+	 * Try to add a sink global committer to the stream graph.
+	 *
+	 * @param inputId The global committer's input stream node id.
+	 * @param sinkTransformation The transformation that the global committer belongs to
+	 * @param globalCommitterFactory The global committer factory
+	 */
+	private void addGlobalCommitter(
+			int inputId,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			OneInputStreamOperatorFactory<CommT, GlobalCommT> globalCommitterFactory,
+			Context context) {
+
+		if (!sinkTransformation.getSink().getGlobalCommittableSerializer().isPresent()) {
+			return;
+		}
+
+		final TypeInformation<CommT> committableTypeInfo = TypeExtractor.createTypeInfo(
+				Sink.class,
+				sinkTransformation.getSink().getClass(),
+				1,
+				null,
+				null);
+
+		addOperatorToStreamGraph(
+				globalCommitterFactory, inputId,
+				committableTypeInfo,
+				null,
+				"Sink Global Committer:",
+				1,
+				1,
+				sinkTransformation,
+				context);
+	}
+
+	private int getParallelism(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		return sinkTransformation.getParallelism() != ExecutionConfig.PARALLELISM_DEFAULT
+				? sinkTransformation.getParallelism()
+				: context.getStreamGraph().getExecutionConfig().getParallelism();
+	}
+
+	/**
+	 * Add a operator to the {@link StreamGraph}.
+	 *
+	 * @param operatorFactory The operator factory
+	 * @param inputId The upstream stream node id of the operator
+	 * @param inTypeInfo The input type information of the operator
+	 * @param outTypInfo The output type information of the operator
+	 * @param prefix The prefix of the name and uid of the operator
+	 * @param parallelism The parallelism of the operator
+	 * @param maxParallelism The max parallelism of the operator
+	 * @param sinkTransformation The sink transformation which the operator belongs to
+	 *
+	 * @return The stream node id of the operator
+	 */
+	private <IN, OUT> int addOperatorToStreamGraph(
+			StreamOperatorFactory<OUT> operatorFactory, int inputId,
+			TypeInformation<IN> inTypeInfo,
+			TypeInformation<OUT> outTypInfo,
+			String prefix,
+			int parallelism,
+			int maxParallelism,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		final StreamGraph streamGraph = context.getStreamGraph();
+		final String slotSharingGroup = context.getSlotSharingGroup();
+		final int transformationId = Transformation.getNewNodeId();
+

Review comment:
       Sorry, I was referring to the `chainingStrategy`. If we want the `chainingStrategy` that the user specified to be applied to each stage `Writer`, `Committer` and `GlobalCommitter` individually. If the user, for example, says `disableChaining`, or set it to any other value like `HEAD`, do we want this to apply to each operator individually?




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] guoweiM commented on a change in pull request #13824: [FLINK-19736] Add the SinkTransformation

Posted by GitBox <gi...@apache.org>.
guoweiM commented on a change in pull request #13824:
URL: https://github.com/apache/flink/pull/13824#discussion_r514968953



##########
File path: flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/translators/SinkTransformationTranslator.java
##########
@@ -0,0 +1,283 @@
+/*
+ * 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.runtime.translators;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.ExecutionConfig;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.connector.sink.Sink;
+import org.apache.flink.api.dag.Transformation;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.streaming.api.graph.StreamGraph;
+import org.apache.flink.streaming.api.graph.TransformationTranslator;
+import org.apache.flink.streaming.api.operators.ChainingStrategy;
+import org.apache.flink.streaming.api.operators.OneInputStreamOperatorFactory;
+import org.apache.flink.streaming.api.operators.StreamOperatorFactory;
+import org.apache.flink.streaming.api.transformations.SinkTransformation;
+import org.apache.flink.streaming.runtime.operators.sink.BatchCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.BatchGlobalCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.GlobalStreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatefulWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StatelessWriterOperatorFactory;
+import org.apache.flink.streaming.runtime.operators.sink.StreamingCommitterOperatorFactory;
+import org.apache.flink.streaming.util.graph.StreamGraphUtils;
+
+import java.util.Collection;
+import java.util.Collections;
+
+/**
+ * A {@link TransformationTranslator} for the {@link SinkTransformation}.
+ */
+@Internal
+public class SinkTransformationTranslator<InputT, CommT, WriterStateT, GlobalCommT> implements
+		TransformationTranslator<Object, SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT>> {
+
+	@Override
+	public Collection<Integer> translateForBatch(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+
+		int writerId = addWriter(transformation, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new BatchCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new BatchGlobalCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		return Collections.emptyList();
+	}
+
+	@Override
+	public Collection<Integer> translateForStreaming(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> transformation,
+			Context context) {
+
+		StreamGraphUtils.validateTransformationUid(context.getStreamGraph(), transformation);
+
+		int writerId = addWriter(transformation, context);
+		int committerId = addCommitter(
+				writerId,
+				transformation,
+				new StreamingCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+		addGlobalCommitter(
+				committerId >= 0 ? committerId : writerId,
+				transformation,
+				new GlobalStreamingCommitterOperatorFactory<>(transformation.getSink()),
+				context);
+
+		return Collections.emptyList();
+	}
+
+	/**
+	 * Add a sink writer node to the stream graph.
+	 *
+	 * @param sinkTransformation The transformation that the writer belongs to
+	 *
+	 * @return The stream node id of the writer
+	 */
+	private int addWriter(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		final boolean hasState = sinkTransformation
+				.getSink()
+				.getWriterStateSerializer()
+				.isPresent();
+		final TypeInformation<CommT> outTypeInfo = TypeExtractor.createTypeInfo(
+				Sink.class,
+				sinkTransformation.getSink().getClass(),
+				1,
+				null,
+				null);
+		@SuppressWarnings("unchecked")
+		final Transformation<InputT> input = (Transformation<InputT>) sinkTransformation
+				.getInputs()
+				.get(0);
+		final TypeInformation<InputT> inputTypeInfo = input.getOutputType();
+
+		final int parallelism = getParallelism(sinkTransformation, context);
+
+		final StreamOperatorFactory<CommT> writer =
+				hasState ? new StatefulWriterOperatorFactory<>(sinkTransformation.getSink()) : new StatelessWriterOperatorFactory<>(
+						sinkTransformation.getSink());
+
+		final int writerId = addOperatorToStreamGraph(
+				writer, input.getId(),
+				inputTypeInfo,
+				outTypeInfo,
+				"Sink Writer:",
+				parallelism,
+				sinkTransformation.getMaxParallelism(),
+				sinkTransformation,
+				context);
+
+		StreamGraphUtils.configureResourceProperties(
+				context.getStreamGraph(),
+				writerId,
+				sinkTransformation);
+		return writerId;
+	}
+
+	/**
+	 * Try to add a sink committer to the stream graph.
+	 *
+	 * @param inputId The committer's input stream node id
+	 * @param sinkTransformation The transformation that the committer belongs to
+	 * @param committerFactory The committer operator's factory
+	 *
+	 * @return The stream node id of the committer or -1 if the sink topology does not include a committer.
+	 */
+	private int addCommitter(
+			int inputId,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			OneInputStreamOperatorFactory<CommT, CommT> committerFactory,
+			Context context) {
+
+		if (!sinkTransformation.getSink().getCommittableSerializer().isPresent()) {
+			return -1;
+		}
+
+		final TypeInformation<CommT> committableTypeInfo = TypeExtractor.createTypeInfo(
+				Sink.class,
+				sinkTransformation.getSink().getClass(),
+				1,
+				null,
+				null);
+
+		final int parallelism = getParallelism(sinkTransformation, context);
+
+		return addOperatorToStreamGraph(
+				committerFactory, inputId,
+				committableTypeInfo,
+				committableTypeInfo,
+				"Sink Committer:",
+				parallelism,
+				sinkTransformation.getMaxParallelism(),
+				sinkTransformation,
+				context);
+	}
+
+	/**
+	 * Try to add a sink global committer to the stream graph.
+	 *
+	 * @param inputId The global committer's input stream node id.
+	 * @param sinkTransformation The transformation that the global committer belongs to
+	 * @param globalCommitterFactory The global committer factory
+	 */
+	private void addGlobalCommitter(
+			int inputId,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			OneInputStreamOperatorFactory<CommT, GlobalCommT> globalCommitterFactory,
+			Context context) {
+
+		if (!sinkTransformation.getSink().getGlobalCommittableSerializer().isPresent()) {
+			return;
+		}
+
+		final TypeInformation<CommT> committableTypeInfo = TypeExtractor.createTypeInfo(
+				Sink.class,
+				sinkTransformation.getSink().getClass(),
+				1,
+				null,
+				null);
+
+		addOperatorToStreamGraph(
+				globalCommitterFactory, inputId,
+				committableTypeInfo,
+				null,
+				"Sink Global Committer:",
+				1,
+				1,
+				sinkTransformation,
+				context);
+	}
+
+	private int getParallelism(
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		return sinkTransformation.getParallelism() != ExecutionConfig.PARALLELISM_DEFAULT
+				? sinkTransformation.getParallelism()
+				: context.getStreamGraph().getExecutionConfig().getParallelism();
+	}
+
+	/**
+	 * Add a operator to the {@link StreamGraph}.
+	 *
+	 * @param operatorFactory The operator factory
+	 * @param inputId The upstream stream node id of the operator
+	 * @param inTypeInfo The input type information of the operator
+	 * @param outTypInfo The output type information of the operator
+	 * @param prefix The prefix of the name and uid of the operator
+	 * @param parallelism The parallelism of the operator
+	 * @param maxParallelism The max parallelism of the operator
+	 * @param sinkTransformation The sink transformation which the operator belongs to
+	 *
+	 * @return The stream node id of the operator
+	 */
+	private <IN, OUT> int addOperatorToStreamGraph(
+			StreamOperatorFactory<OUT> operatorFactory, int inputId,
+			TypeInformation<IN> inTypeInfo,
+			TypeInformation<OUT> outTypInfo,
+			String prefix,
+			int parallelism,
+			int maxParallelism,
+			SinkTransformation<InputT, CommT, WriterStateT, GlobalCommT> sinkTransformation,
+			Context context) {
+		final StreamGraph streamGraph = context.getStreamGraph();
+		final String slotSharingGroup = context.getSlotSharingGroup();
+		final int transformationId = Transformation.getNewNodeId();
+

Review comment:
       I think you are right. It is more intuitive to only set to the `writer`




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot commented on pull request #13824: [FLINK-19736] Add the SinkTransformation

Posted by GitBox <gi...@apache.org>.
flinkbot commented on pull request #13824:
URL: https://github.com/apache/flink/pull/13824#issuecomment-717895470


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "a10e3dcdaf7ff215ec455ce60ca7594651ad8d04",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "a10e3dcdaf7ff215ec455ce60ca7594651ad8d04",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * a10e3dcdaf7ff215ec455ce60ca7594651ad8d04 UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org