showuon commented on code in PR #17511: URL: https://github.com/apache/kafka/pull/17511#discussion_r1810401688
########## clients/src/main/java/org/apache/kafka/common/internals/Plugin.java: ########## @@ -0,0 +1,78 @@ +/* + * 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.common.internals; + +import org.apache.kafka.common.metrics.Metrics; +import org.apache.kafka.common.metrics.Monitorable; +import org.apache.kafka.common.metrics.internals.PluginMetricsImpl; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.function.Supplier; + +public class Plugin<T> implements Supplier<T>, AutoCloseable { + + private final T instance; + private final Optional<PluginMetricsImpl> pluginMetrics; + + private Plugin(T instance, PluginMetricsImpl pluginMetrics) { + this.instance = instance; + this.pluginMetrics = Optional.ofNullable(pluginMetrics); + } + + public static <T> Plugin<T> wrapInstance(T instance, Metrics metrics, String key) { + return wrapInstance(instance, metrics, () -> tags(key, instance)); + } + + private static <T> Map<String, String> tags(String key, T instance) { + Map<String, String> tags = new LinkedHashMap<>(); + tags.put("config", key); + tags.put("class", instance.getClass().getSimpleName()); + return tags; + } + + public static <T> List<Plugin<T>> wrapInstances(List<T> instances, Metrics metrics, String key) { + List<Plugin<T>> plugins = new ArrayList<>(); + for (T instance : instances) { + plugins.add(wrapInstance(instance, metrics, key)); + } + return plugins; + } + + public static <T> Plugin<T> wrapInstance(T instance, Metrics metrics, Supplier<Map<String, String>> tagsSupplier) { + PluginMetricsImpl pluginMetrics = null; + if (instance instanceof Monitorable && metrics != null) { + pluginMetrics = new PluginMetricsImpl(metrics, tagsSupplier.get()); + ((Monitorable) instance).withPluginMetrics(pluginMetrics); + } + return new Plugin<>(instance, pluginMetrics); + } + + @Override + public T get() { + return instance; + } + + @Override + public void close() throws Exception { + if (pluginMetrics.isPresent()) pluginMetrics.get().close(); + if (instance instanceof AutoCloseable) ((AutoCloseable) instance).close(); Review Comment: In the [KIP](https://cwiki.apache.org/confluence/display/KAFKA/KIP-877%3A+Mechanism+for+plugins+and+connectors+to+register+metrics#KIP877:Mechanismforpluginsandconnectorstoregistermetrics-Supportedplugins), we said: `The goal is allow this feature to be used by all plugins that are Closeable, AutoCloseable or have a close() methods.` But here, we only close if it's `Closeable/AutoCloseable` instance. Are we sure all the interfaces having `close()` method is `Closeable/AutoCloseable`? ########## clients/src/test/java/org/apache/kafka/common/internals/PluginTest.java: ########## @@ -0,0 +1,124 @@ +/* + * 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.common.internals; + +import org.apache.kafka.common.metrics.Metrics; +import org.apache.kafka.common.metrics.Monitorable; +import org.apache.kafka.common.metrics.PluginMetrics; + +import org.junit.jupiter.api.Test; + +import java.io.Closeable; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class PluginTest { + + private static final String CONFIG = "some.config"; + private static final Metrics METRICS = new Metrics(); + + static class SomePlugin implements Closeable { + + PluginMetrics pluginMetrics; + boolean closed; + + @Override + public void close() throws IOException { + closed = true; + } + } + + static class SomeMonitorablePlugin extends SomePlugin implements Monitorable { + + @Override + public void withPluginMetrics(PluginMetrics metrics) { + pluginMetrics = metrics; + } + } + + @Test + void testWrapInstance() throws Exception { + SomeMonitorablePlugin someMonitorablePlugin = new SomeMonitorablePlugin(); + Plugin<SomeMonitorablePlugin> pluginMonitorable = Plugin.wrapInstance(someMonitorablePlugin, METRICS, CONFIG); + checkPlugin(pluginMonitorable, someMonitorablePlugin, true); + + someMonitorablePlugin = new SomeMonitorablePlugin(); + assertFalse(someMonitorablePlugin.closed); + pluginMonitorable = Plugin.wrapInstance(someMonitorablePlugin, null, CONFIG); + checkPlugin(pluginMonitorable, someMonitorablePlugin, false); + + SomePlugin somePlugin = new SomePlugin(); + assertFalse(somePlugin.closed); + Plugin<SomePlugin> plugin = Plugin.wrapInstance(somePlugin, null, CONFIG); + assertSame(somePlugin, plugin.get()); + assertNull(somePlugin.pluginMetrics); + plugin.close(); + assertTrue(somePlugin.closed); + } + + @Test + void testWrapInstances() throws Exception { + List<SomeMonitorablePlugin> someMonitorablePlugins = Arrays.asList(new SomeMonitorablePlugin(), new SomeMonitorablePlugin()); + List<Plugin<SomeMonitorablePlugin>> pluginsMonitorable = Plugin.wrapInstances(someMonitorablePlugins, METRICS, CONFIG); + assertEquals(someMonitorablePlugins.size(), pluginsMonitorable.size()); + for (int i = 0; i < pluginsMonitorable.size(); i++) { + Plugin<SomeMonitorablePlugin> plugin = pluginsMonitorable.get(i); + SomeMonitorablePlugin somePlugin = someMonitorablePlugins.get(i); + checkPlugin(plugin, somePlugin, true); + } + + someMonitorablePlugins = Arrays.asList(new SomeMonitorablePlugin(), new SomeMonitorablePlugin()); + pluginsMonitorable = Plugin.wrapInstances(someMonitorablePlugins, null, CONFIG); + assertEquals(someMonitorablePlugins.size(), pluginsMonitorable.size()); + for (int i = 0; i < pluginsMonitorable.size(); i++) { + Plugin<SomeMonitorablePlugin> plugin = pluginsMonitorable.get(i); + SomeMonitorablePlugin somePlugin = someMonitorablePlugins.get(i); + checkPlugin(plugin, somePlugin, false); + } + + List<SomePlugin> somePlugins = Arrays.asList(new SomePlugin(), new SomePlugin()); + List<Plugin<SomePlugin>> plugins = Plugin.wrapInstances(somePlugins, METRICS, CONFIG); + assertEquals(somePlugins.size(), plugins.size()); + for (int i = 0; i < plugins.size(); i++) { + Plugin<SomePlugin> plugin = plugins.get(i); + SomePlugin somePlugin = somePlugins.get(i); + assertSame(somePlugin, plugin.get()); + assertNull(somePlugin.pluginMetrics); + plugin.close(); + assertTrue(somePlugin.closed); + } + } + + private void checkPlugin(Plugin<SomeMonitorablePlugin> plugin, SomeMonitorablePlugin instance, boolean metricsSet) throws Exception { + assertSame(instance, plugin.get()); + if (metricsSet) { + assertNotNull(instance.pluginMetrics); Review Comment: I think we should also make sure the content of the `pluginMetrics` is expected. Ex: we can add some sensors, tags and verify they are added in the pluginMetrics. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
