gharris1727 commented on code in PR #13185:
URL: https://github.com/apache/kafka/pull/13185#discussion_r1120864255


##########
connect/runtime/src/main/java/org/apache/kafka/connect/runtime/isolation/IsolatedPlugin.java:
##########
@@ -0,0 +1,122 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.connect.runtime.isolation;
+
+import java.util.Objects;
+import java.util.concurrent.Callable;
+import java.util.function.BiConsumer;
+import java.util.function.BiFunction;
+import java.util.function.Consumer;
+import java.util.function.Function;
+
+public abstract class IsolatedPlugin<P> {
+
+    private final Plugins plugins;
+    private final Class<?> pluginClass;
+    protected final P delegate;
+    private final ClassLoader classLoader;
+    private final PluginType type;
+
+    IsolatedPlugin(Plugins plugins, P delegate, PluginType type) {
+        this.plugins = Objects.requireNonNull(plugins, "plugins must be 
non-null");
+        this.delegate = Objects.requireNonNull(delegate, "delegate plugin must 
be non-null");
+        this.pluginClass = delegate.getClass();
+        ClassLoader classLoader = pluginClass.getClassLoader();
+        this.classLoader = Objects.requireNonNull(classLoader, "delegate 
plugin must not be a boostrap class");
+        this.type = Objects.requireNonNull(type, "plugin type must be 
non-null");
+    }
+
+    public PluginType type() {
+        return type;
+    }
+
+    @SuppressWarnings("unchecked")
+    public Class<? extends P> pluginClass() {
+        return (Class<? extends P>) pluginClass;
+    }
+
+    protected <V> V isolate(Callable<V> callable) throws Exception {
+        try (LoaderSwap loaderSwap = plugins.withClassLoader(classLoader)) {
+            return callable.call();
+        }
+    }
+
+    protected void isolateV(ThrowingRunnable runnable) throws Exception {
+        try (LoaderSwap loaderSwap = plugins.withClassLoader(classLoader)) {
+            runnable.run();
+        }
+    }
+
+    protected <T> void isolateV(Consumer<T> consumer, T t) throws Exception {
+        try (LoaderSwap loaderSwap = plugins.withClassLoader(classLoader)) {
+            consumer.accept(t);
+        }
+    }
+
+    protected <T, U> void isolateV(BiConsumer<T, U> consumer, T t, U u) throws 
Exception {
+        try (LoaderSwap loaderSwap = plugins.withClassLoader(classLoader)) {
+            consumer.accept(t, u);
+        }
+    }
+
+    protected <T, R> R isolate(Function<T, R> function, T t) throws Exception {
+        try (LoaderSwap loaderSwap = plugins.withClassLoader(classLoader)) {
+            return function.apply(t);
+        }
+    }
+
+    protected <T, U, R> R isolate(BiFunction<T, U, R> function, T t, U u) 
throws Exception {
+        try (LoaderSwap loaderSwap = plugins.withClassLoader(classLoader)) {
+            return function.apply(t, u);
+        }
+    }

Review Comment:
   I think this is a question of readability, but it's important to consider 
the readability for operators of these connectors. With the current design that 
uses mostly method references, exceptions have the following stacktrace:
   ```
   org.apache.kafka.connect.errors.ConnectException:
        at 
org.apache.kafka.connect.runtime.isolation.IsolatedPluginTest.lambda$testSinkConnector$0(IsolatedPluginTest.java:43)
           ...
        at 
org.apache.kafka.connect.connector.Connector.initialize(Connector.java:57)
        at 
org.apache.kafka.connect.runtime.isolation.IsolatedPlugin.isolateV(IsolatedPlugin.java:66)
        at 
org.apache.kafka.connect.runtime.isolation.IsolatedConnector.initialize(IsolatedConnector.java:39)
        at 
org.apache.kafka.connect.runtime.isolation.IsolatedPluginTest.testSinkConnector(IsolatedPluginTest.java:46)
        ...
   ```
   
   There's two stack frames associated with the isolation infrastructure that 
did not exist befor, and neither of them are anonymous lambdas (the 
`IsolatedPluginTest.lambda$testSinkConnector$0` is an anonymous lambda in my 
test, not part of the IsolatedPlugin).
   
   Compare that to the style of making everything into a callable:
   ```
   org.apache.kafka.connect.errors.ConnectException:
        at 
org.apache.kafka.connect.runtime.isolation.IsolatedPluginTest.lambda$testSinkConnector$0(IsolatedPluginTest.java:43)
           ...
        at 
org.apache.kafka.connect.connector.Connector.initialize(Connector.java:57)
        at 
org.apache.kafka.connect.runtime.isolation.IsolatedConnector.lambda$initialize$0(IsolatedConnector.java:39)
        at 
org.apache.kafka.connect.runtime.isolation.IsolatedPlugin.isolateV(IsolatedPlugin.java:60)
        at 
org.apache.kafka.connect.runtime.isolation.IsolatedConnector.initialize(IsolatedConnector.java:39)
        at 
org.apache.kafka.connect.runtime.isolation.IsolatedPluginTest.testSinkConnector(IsolatedPluginTest.java:46)
        ...
   ```
   
   This has one additional stacktrace for the lambda mapping from the function 
to a callable, and it has a bit of an ugly name.
   
   Also it doesn't appear in this PR, but I did find some plugin methods which 
cannot be reduced to one of these signatures, and did have to be reduced to a 
Callable (I think it was the converters?).
   So this strategy is not perfect, does not cover all of the cases, but does 
provide slightly cleaner stacktraces in the situations it does cover.
   I think i could go either way on this.



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