rmetzger 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_r399508649
 
 

 ##########
 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:
   (General comment: I'm not familiar enough with this part of the code yet to 
feel really qualified for a comment here, so I'm potentially shooting in the 
dark with this feedback item):
   
   Are we sure that we can always ignore the `Exception e`? 
   Does it make sense to add it as a cause to the `RuntimeException`, or log it 
on DEBUG level?
   
   The CI on Azure for this PR was failing with the following error:
   
   ```
   [INFO] Running org.apache.flink.tests.util.kafka.StreamingKafkaITCase
   [ERROR] Tests run: 3, Failures: 0, Errors: 3, Skipped: 0, Time elapsed: 
0.006 s <<< FAILURE! - in org.apache.flink.tests.util.kafka.StreamingKafkaITCase
   [ERROR] testKafka[0: 
kafka-version:0.10.2.0](org.apache.flink.tests.util.kafka.StreamingKafkaITCase) 
 Time elapsed: 0.004 s  <<< ERROR!
   java.lang.RuntimeException: Could not instantiate instance.
        at 
org.apache.flink.tests.util.kafka.StreamingKafkaITCase.<init>(StreamingKafkaITCase.java:72)
   
   [ERROR] testKafka[1: 
kafka-version:0.11.0.2](org.apache.flink.tests.util.kafka.StreamingKafkaITCase) 
 Time elapsed: 0.001 s  <<< ERROR!
   java.lang.RuntimeException: Could not instantiate instance.
        at 
org.apache.flink.tests.util.kafka.StreamingKafkaITCase.<init>(StreamingKafkaITCase.java:72)
   
   [ERROR] testKafka[2: 
kafka-version:2.2.0](org.apache.flink.tests.util.kafka.StreamingKafkaITCase)  
Time elapsed: 0 s  <<< ERROR!
   java.lang.RuntimeException: Could not instantiate instance.
        at 
org.apache.flink.tests.util.kafka.StreamingKafkaITCase.<init>(StreamingKafkaITCase.java:72)
   ```
   
   I would have assumed that this PR is improving the exception output for this 
case specifically.
   
   I wonder if throwing a RuntimeException here is the right approach? Maybe we 
should have a checked exception type for this, so that we are forced to handle 
it at the call site (`FlinkResource.get()`, where we could rethrow with 
additonal information ("Error instantiating FlinkResource")).
   

----------------------------------------------------------------
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