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



##########
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:
       done

##########
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:
       done

##########
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:
       done




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