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_r399539228
 
 

 ##########
 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:
   So, generally speaking it shouldn't be necessary to include `e` as a cause, 
the reason being that the default provider is usually included in the set of 
factories returned by `ServiceLoader.load`. This also implies that the error is 
already being logged.
   There is only one exception to this which is the `LolCache`, which generally 
speaking should never fail (technically it could if it can't create a 
TemporaryDirectory, but then you likely have bigger problems).
   
   Handling at the call-size doesn't really give us a lot; we can't handle the 
error but only slightly enhance the error message which shouldn't be necessary 
in the first place. For some reason the stack trace is being trimmed to 
ridiculous degrees; which may also cause the suppressed exception to not be 
shown.
   
   I will investigate why this happens; I did try it with the `FlinkResoure` 
without `distDir` being set (the original original cause for this PR), and it 
displayed the right exception. But IIRC I did that in the IDE, so maybe 
maven/surefire is getting in the way 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:
[email protected]


With regards,
Apache Git Services

Reply via email to