C0urante commented on code in PR #13971:
URL: https://github.com/apache/kafka/pull/13971#discussion_r1265860597


##########
connect/runtime/src/main/java/org/apache/kafka/connect/runtime/isolation/PluginScanner.java:
##########
@@ -147,6 +150,48 @@ protected <T> SortedSet<PluginDesc<T>> 
getServiceLoaderPluginDesc(Class<T> klass
         return result;
     }
 
+    /**
+     * Helper to evaluate a {@link ServiceLoader} operation while handling 
{@link LinkageError}s.
+     *
+     * @param klass The plugin superclass which is being loaded
+     * @param function A function on a {@link ServiceLoader} which may throw 
{@link LinkageError}
+     * @return the return value of function
+     * @throws Error errors thrown by the passed-in function
+     * @param <T> Type being iterated over by the ServiceLoader
+     * @param <U> Return value of the passed-in function
+     */
+    private <T, U> U handleLinkageError(Class<T> klass, Supplier<U> function) {
+        // It's difficult to know for sure if the iterator was able to advance 
past the first broken
+        // plugin class, or if it will continue to fail on that broken class 
for any subsequent calls
+        // to Iterator::hasNext or Iterator::next
+        // For reference, see https://bugs.openjdk.org/browse/JDK-8196182, 
which describes
+        // the behavior we are trying to mitigate with this logic as buggy, 
but indicates that a fix
+        // in the JDK standard library ServiceLoader implementation is 
unlikely to land
+        LinkageError lastError = null;
+        // Try a fixed maximum number of times in case the ServiceLoader 
cannot move past a faulty plugin,
+        // but the LinkageError varies between calls. This limit is chosen to 
be higher than the typical number
+        // of plugins in a single plugin location, and to limit the amount of 
log-spam on startup.
+        for (int i = 0; i < 100; i++) {
+            try {
+                return function.get();
+            } catch (LinkageError t) {
+                // As an optimization, hide subsequent error logs if two 
consecutive errors look similar.
+                // This reduces log-spam for iterators which cannot advance 
and rethrow the same exception.
+                if (lastError == null
+                        || !Objects.equals(lastError.getClass(), t.getClass())
+                        || !Objects.equals(lastError.getMessage(), 
t.getMessage())) {
+                    log.error("Failed to discover {}{}", 
klass.getSimpleName(), reflectiveErrorDescription(t), t);
+                }
+                lastError = t;
+            }
+        }
+        log.error("Received excessive ServiceLoader errors: assuming the 
runtime ServiceLoader implementation cannot " +
+                        "skip faulty implementations. Use a different JRE, 
resolve error within {} plugin, or " +

Review Comment:
   Nit: "resolve error within Converter plugin" doesn't seem very useful for 
users. Would it be possible to provide them with the plugin location instead?



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

To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to