forked from apache/flink
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FLINK-1819][core] allow access to RuntimeContext from Input and Outp…
…utFormats 1. Allow access to Runtime Context from I/O formats. 2. Make all existing I/O formats context aware. This closes apache#966.
- Loading branch information
1 parent
7cc85c7
commit 26c6447
Showing
34 changed files
with
1,001 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
flink-core/src/main/java/org/apache/flink/api/common/io/RichInputFormat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
c * 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.api.common.io; | ||
|
||
import org.apache.flink.api.common.functions.RuntimeContext; | ||
import org.apache.flink.core.io.InputSplit; | ||
|
||
/** | ||
* An abstract stub implementation for Rich input formats. | ||
* Rich formats have access to their runtime execution context via {@link #getRuntimeContext()}. | ||
*/ | ||
public abstract class RichInputFormat<OT, T extends InputSplit> implements InputFormat<OT, T> { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
// -------------------------------------------------------------------------------------------- | ||
// Runtime context access | ||
// -------------------------------------------------------------------------------------------- | ||
|
||
private transient RuntimeContext runtimeContext; | ||
|
||
public void setRuntimeContext(RuntimeContext t) { | ||
this.runtimeContext = t; | ||
} | ||
|
||
public RuntimeContext getRuntimeContext() { | ||
if (this.runtimeContext != null) { | ||
return this.runtimeContext; | ||
} else { | ||
throw new IllegalStateException("The runtime context has not been initialized yet. Try accessing " + | ||
"it in one of the other life cycle methods."); | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
flink-core/src/main/java/org/apache/flink/api/common/io/RichOutputFormat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
c * 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.api.common.io; | ||
|
||
import org.apache.flink.api.common.functions.RuntimeContext; | ||
|
||
/** | ||
* An abstract stub implementation for Rich output formats. | ||
* Rich formats have access to their runtime execution context via {@link #getRuntimeContext()}. | ||
*/ | ||
public abstract class RichOutputFormat<IT> implements OutputFormat<IT> { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
// -------------------------------------------------------------------------------------------- | ||
// Runtime context access | ||
// -------------------------------------------------------------------------------------------- | ||
|
||
private transient RuntimeContext runtimeContext; | ||
|
||
public void setRuntimeContext(RuntimeContext t) { | ||
this.runtimeContext = t; | ||
} | ||
|
||
public RuntimeContext getRuntimeContext() { | ||
if (this.runtimeContext != null) { | ||
return this.runtimeContext; | ||
} else { | ||
throw new IllegalStateException("The runtime context has not been initialized yet. Try accessing " + | ||
"it in one of the other life cycle methods."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
flink-core/src/test/java/org/apache/flink/api/common/io/RichInputFormatTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* 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.api.common.io; | ||
|
||
import java.util.HashMap; | ||
|
||
import org.apache.flink.api.common.ExecutionConfig; | ||
import org.apache.flink.api.common.accumulators.Accumulator; | ||
import org.apache.flink.api.common.functions.util.RuntimeUDFContext; | ||
import org.apache.flink.types.Value; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Tests runtime context access from inside an RichInputFormat class | ||
*/ | ||
public class RichInputFormatTest { | ||
|
||
@Test | ||
public void testCheckRuntimeContextAccess() { | ||
final SerializedInputFormat<Value> inputFormat = new SerializedInputFormat<Value>(); | ||
inputFormat.setRuntimeContext(new RuntimeUDFContext("test name", 3, 1, | ||
getClass().getClassLoader(), new ExecutionConfig(), new HashMap<String, Accumulator<?, ?>>())); | ||
|
||
Assert.assertEquals(inputFormat.getRuntimeContext().getIndexOfThisSubtask(), 1); | ||
Assert.assertEquals(inputFormat.getRuntimeContext().getNumberOfParallelSubtasks(),3); | ||
} | ||
} |
Oops, something went wrong.