zentol commented on a change in pull request #11536: [FLINK-16807][e2e] Improve
reporting for instantiation errors
URL: https://github.com/apache/flink/pull/11536#discussion_r399548766
##########
File path:
flink-end-to-end-tests/flink-end-to-end-tests-common/src/main/java/org/apache/flink/tests/util/util/FactoryUtils.java
##########
@@ -43,24 +45,43 @@
* @throws RuntimeException if no or multiple resources could be
instantiated
* @return created instance
*/
- public static <R, F> R loadAndInvokeFactory(final Class<F>
factoryInterface, final Function<F, Optional<R>> factoryInvoker, final
Supplier<F> defaultProvider) {
+ public static <R, F> R loadAndInvokeFactory(final Class<F>
factoryInterface, final FactoryInvoker<F, R> factoryInvoker, final Supplier<F>
defaultProvider) {
final ServiceLoader<F> factories =
ServiceLoader.load(factoryInterface);
- final List<R> resources =
StreamSupport.stream(factories.spliterator(), false)
- .map(factoryInvoker)
- .filter(Optional::isPresent)
- .map(Optional::get)
- .collect(Collectors.toList());
+ final List<R> instantiatedResources = new ArrayList<>();
+ final List<Exception> errorsDuringInitialization = new
ArrayList<>();
+ for (F factory : factories) {
+ try {
+ R resource = factoryInvoker.invoke(factory);
+ instantiatedResources.add(resource);
+ LOG.info("Instantiated {}.",
resource.getClass().getSimpleName());
+ } catch (Exception e) {
+ LOG.debug("Factory {} could not instantiate
instance.", factory.getClass().getSimpleName(), e);
+ errorsDuringInitialization.add(e);
+ }
+ }
- if (resources.size() == 1) {
- return resources.get(0);
+ if (instantiatedResources.size() == 1) {
+ return instantiatedResources.get(0);
}
- if (resources.isEmpty()) {
- return factoryInvoker.apply(defaultProvider.get())
- .orElseThrow(() -> new RuntimeException("Could
not instantiate instance using default factory."));
+ if (instantiatedResources.isEmpty()) {
+ try {
+ return
factoryInvoker.invoke(defaultProvider.get());
+ } catch (Exception e) {
+ final RuntimeException exception = new
RuntimeException("Could not instantiate instance.");
Review comment:
> [...] the default provider is usually included in the set of factories
returned by ServiceLoader.load.
So this is wrong. Neither the Flink nor Kafka resources have a services
resource file, and are always loaded as default resources.
I have amended the PR to also include the error of the default resource.
I have found the reason for the trimmed stack trace (FLINK-16837, surefire
being stupid and old) and have provided a fix for it in this PR.
The exception will now look like this:
```
java.lang.RuntimeException: Could not instantiate any instance.
at
org.apache.flink.tests.util.util.FactoryUtils.loadAndInvokeFactory(FactoryUtils.java:72)
at
org.apache.flink.tests.util.flink.FlinkResource.get(FlinkResource.java:75)
at
org.apache.flink.metrics.prometheus.tests.PrometheusReporterEndToEndITCase.<init>(PrometheusReporterEndToEndITCase.java:117)
at
java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native
Method)
at
java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at
java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at
java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at
org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
at
org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at
org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at
org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at
org.apache.maven.surefire.junitcore.JUnitCore.run(JUnitCore.java:55)
at
org.apache.maven.surefire.junitcore.JUnitCoreWrapper.createRequestAndRun(JUnitCoreWrapper.java:137)
at
org.apache.maven.surefire.junitcore.JUnitCoreWrapper.executeEager(JUnitCoreWrapper.java:107)
at
org.apache.maven.surefire.junitcore.JUnitCoreWrapper.execute(JUnitCoreWrapper.java:83)
at
org.apache.maven.surefire.junitcore.JUnitCoreWrapper.execute(JUnitCoreWrapper.java:75)
at
org.apache.maven.surefire.junitcore.JUnitCoreProvider.invoke(JUnitCoreProvider.java:158)
at
org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:384)
at
org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:345)
at
org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:126)
at
org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:418)
Suppressed: java.lang.RuntimeException: Could not instantiate
default instance.
at
org.apache.flink.tests.util.util.FactoryUtils.loadAndInvokeFactory(FactoryUtils.java:73)
... 37 more
Caused by: java.lang.IllegalArgumentException: The distDir property
was not set. You can set it when running maven via -DdistDir=<path> .
at
org.apache.flink.tests.util.flink.LocalStandaloneFlinkResourceFactory.create(LocalStandaloneFlinkResourceFactory.java:43)
at
org.apache.flink.tests.util.flink.FlinkResource.lambda$get$0(FlinkResource.java:77)
at
org.apache.flink.tests.util.util.FactoryUtils.loadAndInvokeFactory(FactoryUtils.java:70)
... 37 more
```
It is a tad long, but surefire isn't flexible in this regard, and I rather
have an exception that is verbose than one that hides critical information.
----------------------------------------------------------------
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:
[email protected]
With regards,
Apache Git Services