Skip to content

Commit 74ef6be

Browse files
ifndef-SleePyzhuzhurk
authored andcommitted
[FLINK-30798][runtime] Refactor OutputFormat#open to expose attempt number
1 parent dcbb206 commit 74ef6be

5 files changed

Lines changed: 78 additions & 5 deletions

File tree

flink-core/src/main/java/org/apache/flink/api/common/io/OutputFormat.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,22 @@ public interface OutputFormat<IT> extends Serializable {
6262
* @param taskNumber The number of the parallel instance.
6363
* @param numTasks The number of parallel tasks.
6464
* @throws IOException Thrown, if the output could not be opened due to an I/O problem.
65+
* @deprecated Use {@link #open(InitializationContext)} instead
6566
*/
66-
void open(int taskNumber, int numTasks) throws IOException;
67+
@Deprecated
68+
default void open(int taskNumber, int numTasks) throws IOException {}
6769

70+
/**
71+
* Opens a parallel instance of the output format to store the result of its parallel instance.
72+
*
73+
* <p>When this method is called, the output format it guaranteed to be configured.
74+
*
75+
* @param context The context to get task parallel infos.
76+
* @throws IOException Thrown, if the output could not be opened due to an I/O problem.
77+
*/
78+
default void open(InitializationContext context) throws IOException {
79+
open(context.getTaskNumber(), context.getNumTasks());
80+
}
6881
/**
6982
* Adds a record to the output.
7083
*
@@ -85,4 +98,30 @@ public interface OutputFormat<IT> extends Serializable {
8598
* @throws IOException Thrown, if the input could not be closed properly.
8699
*/
87100
void close() throws IOException;
101+
102+
/** The context exposes some runtime info for initializing output format. */
103+
@Public
104+
interface InitializationContext {
105+
/**
106+
* Gets the parallelism with which the parallel task runs.
107+
*
108+
* @return The parallelism with which the parallel task runs.
109+
*/
110+
int getNumTasks();
111+
112+
/**
113+
* Gets the number of this parallel subtask. The numbering starts from 0 and goes up to
114+
* parallelism-1 (parallelism as returned by {@link #getNumTasks()}).
115+
*
116+
* @return The index of the parallel subtask.
117+
*/
118+
int getTaskNumber();
119+
120+
/**
121+
* Gets the attempt number of this parallel subtask. First attempt is numbered 0.
122+
*
123+
* @return Attempt number of the subtask.
124+
*/
125+
int getAttemptNumber();
126+
}
88127
}

flink-runtime/src/main/java/org/apache/flink/runtime/operators/DataSinkTask.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.flink.api.common.functions.RuntimeContext;
2323
import org.apache.flink.api.common.io.CleanupWhenUnsuccessful;
2424
import org.apache.flink.api.common.io.OutputFormat;
25+
import org.apache.flink.api.common.io.OutputFormat.InitializationContext;
2526
import org.apache.flink.api.common.io.RichOutputFormat;
2627
import org.apache.flink.api.common.typeutils.TypeComparatorFactory;
2728
import org.apache.flink.api.common.typeutils.TypeSerializer;
@@ -215,8 +216,22 @@ public void invoke() throws Exception {
215216

216217
// open
217218
format.open(
218-
this.getEnvironment().getTaskInfo().getIndexOfThisSubtask(),
219-
this.getEnvironment().getTaskInfo().getNumberOfParallelSubtasks());
219+
new InitializationContext() {
220+
@Override
221+
public int getNumTasks() {
222+
return getEnvironment().getTaskInfo().getNumberOfParallelSubtasks();
223+
}
224+
225+
@Override
226+
public int getTaskNumber() {
227+
return getEnvironment().getTaskInfo().getIndexOfThisSubtask();
228+
}
229+
230+
@Override
231+
public int getAttemptNumber() {
232+
return getEnvironment().getTaskInfo().getAttemptNumber();
233+
}
234+
});
220235

221236
if (objectReuseEnabled) {
222237
IT record = serializer.createInstance();

flink-streaming-java/src/main/java/org/apache/flink/streaming/api/functions/sink/OutputFormatSinkFunction.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.flink.api.common.functions.RuntimeContext;
2323
import org.apache.flink.api.common.io.CleanupWhenUnsuccessful;
2424
import org.apache.flink.api.common.io.OutputFormat;
25+
import org.apache.flink.api.common.io.OutputFormat.InitializationContext;
2526
import org.apache.flink.api.common.io.RichOutputFormat;
2627
import org.apache.flink.api.common.typeinfo.TypeInformation;
2728
import org.apache.flink.api.java.typeutils.InputTypeConfigurable;
@@ -62,7 +63,23 @@ public void open(Configuration parameters) throws Exception {
6263
format.configure(parameters);
6364
int indexInSubtaskGroup = context.getIndexOfThisSubtask();
6465
int currentNumberOfSubtasks = context.getNumberOfParallelSubtasks();
65-
format.open(indexInSubtaskGroup, currentNumberOfSubtasks);
66+
format.open(
67+
new InitializationContext() {
68+
@Override
69+
public int getNumTasks() {
70+
return currentNumberOfSubtasks;
71+
}
72+
73+
@Override
74+
public int getTaskNumber() {
75+
return indexInSubtaskGroup;
76+
}
77+
78+
@Override
79+
public int getAttemptNumber() {
80+
return context.getAttemptNumber();
81+
}
82+
});
6683
}
6784

6885
@Override

flink-table/flink-table-planner/src/test/scala/org/apache/flink/table/planner/runtime/utils/StreamTestSink.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ class TestingOutputFormat[T](tz: TimeZone) extends OutputFormat[T] {
386386

387387
def configure(var1: Configuration): Unit = {}
388388

389-
def open(taskNumber: Int, numTasks: Int): Unit = {
389+
override def open(taskNumber: Int, numTasks: Int): Unit = {
390390
localRetractResults = mutable.ArrayBuffer.empty[String]
391391
StreamTestSink.synchronized {
392392
StreamTestSink.globalResults(index) += (taskNumber -> localRetractResults)

pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2185,6 +2185,8 @@ under the License.
21852185
<!-- UnionSerializerConfigSnapshot was a PublicEvolving and Deprecated class that has been removed, embedded inside a Public CoGroupedStreams class, triggering this false failure -->
21862186
<exclude>org.apache.flink.streaming.api.datastream.CoGroupedStreams$UnionSerializerConfigSnapshot</exclude>
21872187
<exclude>org.apache.flink.api.connector.source.SourceReaderContext#currentParallelism()</exclude>
2188+
<exclude>org.apache.flink.api.common.io.OutputFormat#open(int,int)</exclude>
2189+
<exclude>org.apache.flink.api.common.io.OutputFormat#open(org.apache.flink.api.common.io.OutputFormat$InitializationContext)</exclude>
21882190
<!-- MARKER: end exclusions -->
21892191
</excludes>
21902192
<accessModifier>public</accessModifier>

0 commit comments

Comments
 (0)