rhauch commented on a change in pull request #10549:
URL: https://github.com/apache/kafka/pull/10549#discussion_r616765881



##########
File path: 
connect/runtime/src/main/java/org/apache/kafka/connect/runtime/isolation/DelegatingClassLoader.java
##########
@@ -153,12 +156,25 @@ public PluginClassLoader pluginClassLoader(String name) {
         if (inner == null) {
             return null;
         }
-        ClassLoader pluginLoader = inner.get(inner.lastKey());
+        ClassLoader pluginLoader = inner.get(usedPluginDesc(inner));
         return pluginLoader instanceof PluginClassLoader
                ? (PluginClassLoader) pluginLoader
                : null;
     }
 
+    //visible for testing
+    PluginDesc<?> usedPluginDesc(String name) {
+        SortedMap<PluginDesc<?>, ClassLoader> inner = pluginLoaders.get(name);
+        if (inner == null) {
+            return null;
+        }
+        return usedPluginDesc(inner);

Review comment:
       In cases like this, it's probably a bit more idiomatic in Connect to use 
a tertiary operator so that there is a single `return`:
   ```suggestion
           return inner == null ? null : usedPluginDesc(inner);
   ```

##########
File path: 
connect/runtime/src/test/java/org/apache/kafka/connect/runtime/isolation/DelegatingClassLoaderTest.java
##########
@@ -129,4 +133,57 @@ public void testLoadingMixOfValidAndInvalidPlugins() 
throws Exception {
             assertNotNull(classLoader.pluginClassLoader(pluginClassName));
         }
     }
+
+    @Test
+    public void testPluginConflictSameClassSameVersion() {
+        DelegatingClassLoader delegatingClassLoader = new 
DelegatingClassLoader(Collections.emptyList());
+
+        Class<Connector> klass = Connector.class;
+        PluginDesc<Connector> connectorDesc1 = new PluginDesc<>(
+                klass,
+                "1.1.0",
+                ClassLoader.getSystemClassLoader()
+        );
+        delegatingClassLoader.addPlugins(Arrays.asList(connectorDesc1, 
connectorDesc1), ClassLoader.getSystemClassLoader());
+        
assertTrue(delegatingClassLoader.reportPluginConflicts().contains(klass.getName()));
+    }
+
+    @Test
+    public void testPluginConflictSameClassDiffVersions() {
+        DelegatingClassLoader delegatingClassLoader = new 
DelegatingClassLoader(Collections.emptyList());
+        PluginDesc<Converter> notConflictingPlugin = new PluginDesc<>(
+                Converter.class,
+                "0.0",
+                ClassLoader.getSystemClassLoader()
+        );
+        delegatingClassLoader.addPlugins(Arrays.asList(notConflictingPlugin), 
ClassLoader.getSystemClassLoader());
+
+        Class<Connector> klass = Connector.class;
+        PluginDesc<Connector> connectorDesc1 = new PluginDesc<>(
+                klass,
+                "1.1.0",
+                ClassLoader.getSystemClassLoader()
+        );
+        delegatingClassLoader.addPlugins(Arrays.asList(connectorDesc1), 
ClassLoader.getSystemClassLoader());
+        assertTrue(delegatingClassLoader.reportPluginConflicts().isEmpty());
+
+        PluginDesc<Connector> connectorDesc2 = new PluginDesc<>(
+                klass,
+                "1.0.0",
+                ClassLoader.getSystemClassLoader()
+        );
+        delegatingClassLoader.addPlugins(Arrays.asList(connectorDesc2), 
ClassLoader.getSystemClassLoader());
+        String pluginClassName = klass.getName();
+        
assertTrue(delegatingClassLoader.reportPluginConflicts().contains(pluginClassName));

Review comment:
       It's probably worthwhile to also assert that the non-conflicting plugin 
has not accidentally been added as a conflict, here and after the final 
`addPlugins(...)` call below.

##########
File path: 
connect/runtime/src/main/java/org/apache/kafka/connect/runtime/isolation/DelegatingClassLoader.java
##########
@@ -208,6 +223,19 @@ protected void initLoaders() {
         // Finally add parent/system loader.
         initPluginLoader(CLASSPATH_NAME);
         addAllAliases();
+        reportPluginConflicts();
+    }
+
+    //visible for testing
+    Set<String> reportPluginConflicts() {
+        return allAddedPlugins.entrySet().stream().filter(e -> 
e.getValue().size() > 1).map(e -> {
+            String pluginClassName = e.getKey();
+            PluginDesc<?> usedPluginDesc = usedPluginDesc(pluginClassName);
+            List<PluginDesc<?>> ignoredPlugins = new ArrayList<>(e.getValue());
+            ignoredPlugins.remove(usedPluginDesc);
+            log.error("Detected multiple plugins contain '{}'; using {} and 
ignoring {}", pluginClassName, usedPluginDesc, ignoredPlugins);

Review comment:
       Since this is an error, it probably would be good to clarify the error 
message and provide an action. WDYT about:
   ```suggestion
               log.error("Detected multiple plugins contain '{}'; using plugin 
{} and ignoring {} plugins ({}). "
                       + "Check the installation and remove duplicate plugins 
from all workers.",
                       pluginClassName, usedPluginDesc, ignoredPlugins.size(), 
ignoredPlugins);
   ```




-- 
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:
us...@infra.apache.org


Reply via email to