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-11103][runtime] Set a configurable uncaught exception handler …
…for all entrypoints [FLINK-11103][docs] Generate docs for ClusterOptions changes [FLINK-11103][tests] Add integration test for ClusterUncaughtExceptionHandler to check exit behaviour This closes apache#15938.
- Loading branch information
1 parent
4fddcb2
commit 4a3e6d6
Showing
7 changed files
with
229 additions
and
0 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
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
53 changes: 53 additions & 0 deletions
53
...-runtime/src/main/java/org/apache/flink/runtime/util/ClusterUncaughtExceptionHandler.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,53 @@ | ||
/* | ||
* 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.runtime.util; | ||
|
||
import org.apache.flink.configuration.ClusterOptions; | ||
import org.apache.flink.util.FatalExitExceptionHandler; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Utility for handling any uncaught exceptions | ||
* | ||
* <p>Handles any uncaught exceptions according to cluster configuration in {@link ClusterOptions} | ||
* to either just log exception, or fail job. | ||
*/ | ||
public class ClusterUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler { | ||
private static final Logger LOG = | ||
LoggerFactory.getLogger(ClusterUncaughtExceptionHandler.class); | ||
private final ClusterOptions.UncaughtExceptionHandleMode handleMode; | ||
|
||
public ClusterUncaughtExceptionHandler(ClusterOptions.UncaughtExceptionHandleMode handleMode) { | ||
this.handleMode = handleMode; | ||
} | ||
|
||
@Override | ||
public void uncaughtException(Thread t, Throwable e) { | ||
if (handleMode == ClusterOptions.UncaughtExceptionHandleMode.LOG) { | ||
LOG.error( | ||
"WARNING: Thread '{}' produced an uncaught exception. If you want to fail on uncaught exceptions, then configure {} accordingly", | ||
t.getName(), | ||
ClusterOptions.UNCAUGHT_EXCEPTION_HANDLING.key()); | ||
} else { // by default, fail the job | ||
FatalExitExceptionHandler.INSTANCE.uncaughtException(t, e); | ||
} | ||
} | ||
} |
137 changes: 137 additions & 0 deletions
137
.../test/java/org/apache/flink/runtime/entrypoint/ClusterUncaughtExceptionHandlerITCase.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,137 @@ | ||
/* | ||
* 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.runtime.entrypoint; | ||
|
||
import org.apache.flink.configuration.ClusterOptions; | ||
import org.apache.flink.configuration.Configuration; | ||
import org.apache.flink.runtime.dispatcher.ExecutionGraphInfoStore; | ||
import org.apache.flink.runtime.entrypoint.component.DispatcherResourceManagerComponentFactory; | ||
import org.apache.flink.runtime.operators.testutils.ExpectedTestException; | ||
import org.apache.flink.runtime.testutils.TestJvmProcess; | ||
import org.apache.flink.runtime.util.ClusterUncaughtExceptionHandler; | ||
import org.apache.flink.util.FatalExitExceptionHandler; | ||
import org.apache.flink.util.OperatingSystem; | ||
import org.apache.flink.util.TestLogger; | ||
import org.apache.flink.util.concurrent.ScheduledExecutor; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.junit.Assume.assumeTrue; | ||
|
||
/** Integration test to check exit behaviour for the {@link ClusterUncaughtExceptionHandler}. */ | ||
public class ClusterUncaughtExceptionHandlerITCase extends TestLogger { | ||
|
||
@Before | ||
public void ensureSupportedOS() { | ||
// based on the assumption in JvmExitOnFatalErrorTest, and manual testing on Mac, we do not | ||
// support all platforms (in particular not Windows) | ||
assumeTrue(OperatingSystem.isLinux() || OperatingSystem.isMac()); | ||
} | ||
|
||
@Test | ||
public void testExitDueToUncaughtException() throws Exception { | ||
final ForcedJVMExitProcess testProcess = | ||
new ForcedJVMExitProcess(ClusterTestingEntrypoint.class); | ||
|
||
testProcess.startProcess(); | ||
try { | ||
testProcess.waitFor(); | ||
int signedIntegerExitCode = | ||
FatalExitExceptionHandler | ||
.EXIT_CODE; // for FAIL mode, exit is done using this handler. | ||
int unsignedIntegerExitCode = ((byte) signedIntegerExitCode) & 0xFF; | ||
assertThat(testProcess.exitCode(), is(unsignedIntegerExitCode)); | ||
} finally { | ||
testProcess.destroy(); | ||
} | ||
} | ||
|
||
private static class ClusterTestingEntrypoint extends ClusterEntrypoint { | ||
|
||
protected ClusterTestingEntrypoint(Configuration configuration) { | ||
super(configuration); | ||
} | ||
|
||
@Override | ||
protected DispatcherResourceManagerComponentFactory | ||
createDispatcherResourceManagerComponentFactory(Configuration configuration) | ||
throws IOException { | ||
Thread t = | ||
new Thread( | ||
() -> { | ||
throw new RuntimeException("Test exception"); | ||
}); | ||
t.start(); | ||
try { | ||
t.join(1000L); | ||
} catch (InterruptedException e) { | ||
throw new ExpectedTestException("this line should not be reached"); | ||
} | ||
throw new ExpectedTestException("this line should not be reached."); | ||
} | ||
|
||
@Override | ||
protected ExecutionGraphInfoStore createSerializableExecutionGraphStore( | ||
Configuration configuration, ScheduledExecutor scheduledExecutor) | ||
throws IOException { | ||
return null; | ||
} | ||
|
||
public static void main(String[] args) { | ||
try { | ||
final Configuration config = new Configuration(); | ||
config.set( | ||
ClusterOptions.UNCAUGHT_EXCEPTION_HANDLING, | ||
ClusterOptions.UncaughtExceptionHandleMode.FAIL); | ||
ClusterTestingEntrypoint testingEntrypoint = new ClusterTestingEntrypoint(config); | ||
testingEntrypoint.startCluster(); | ||
} catch (Throwable t) { | ||
System.exit(1); | ||
} | ||
} | ||
} | ||
|
||
private static final class ForcedJVMExitProcess extends TestJvmProcess { | ||
private final Class<?> entryPointName; | ||
|
||
private ForcedJVMExitProcess(Class<?> entryPointName) throws Exception { | ||
this.entryPointName = entryPointName; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return getEntryPointClassName(); | ||
} | ||
|
||
@Override | ||
public String[] getJvmArgs() { | ||
return new String[0]; | ||
} | ||
|
||
@Override | ||
public String getEntryPointClassName() { | ||
return entryPointName.getName(); | ||
} | ||
} | ||
} |