-
Notifications
You must be signed in to change notification settings - Fork 0
Clone kafka 18894 #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
563f347
08dff59
9e221e3
1dd8f41
204c8a9
96fd5a6
6ef4001
44ecfdb
e26a06f
27d9a55
b92a2bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.kafka.common.KafkaException; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.kafka.common.config.provider.ConfigProvider; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.kafka.common.config.types.Password; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.kafka.common.internals.Plugin; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.kafka.common.utils.Utils; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.slf4j.Logger; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -546,16 +547,16 @@ private Map<String, String> extractPotentialVariables(Map<?, ?> configMap) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| configProperties = configProviderProps; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| classNameFilter = ignored -> true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Map<String, ConfigProvider> providers = instantiateConfigProviders(providerConfigString, configProperties, classNameFilter); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Map<String, Plugin<ConfigProvider>> providerPlugins = instantiateConfigProviders(providerConfigString, configProperties, classNameFilter); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!providers.isEmpty()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ConfigTransformer configTransformer = new ConfigTransformer(providers); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!providerPlugins.isEmpty()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ConfigTransformer configTransformer = new ConfigTransformer(providerPlugins); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ConfigTransformerResult result = configTransformer.transform(indirectVariables); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!result.data().isEmpty()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| resolvedOriginals.putAll(result.data()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| providers.values().forEach(x -> Utils.closeQuietly(x, "config provider")); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| providerPlugins.values().forEach(x -> Utils.closeQuietly(x, "config provider plugin")); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return new ResolvingMap<>(resolvedOriginals, originals); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -594,7 +595,7 @@ private Map<String, Object> configProviderProperties(String configProviderPrefix | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @param classNameFilter Filter for config provider class names | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @return map of config provider name and its instance. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private Map<String, ConfigProvider> instantiateConfigProviders( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private Map<String, Plugin<ConfigProvider>> instantiateConfigProviders( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Map<String, String> indirectConfigs, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Map<String, ?> providerConfigProperties, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Predicate<String> classNameFilter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -620,21 +621,22 @@ private Map<String, ConfigProvider> instantiateConfigProviders( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Instantiate Config Providers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Map<String, ConfigProvider> configProviderInstances = new HashMap<>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Map<String, Plugin<ConfigProvider>> configProviderPluginInstances = new HashMap<>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (Map.Entry<String, String> entry : providerMap.entrySet()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String prefix = CONFIG_PROVIDERS_CONFIG + "." + entry.getKey() + CONFIG_PROVIDERS_PARAM; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Map<String, ?> configProperties = configProviderProperties(prefix, providerConfigProperties); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ConfigProvider provider = Utils.newInstance(entry.getValue(), ConfigProvider.class); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| provider.configure(configProperties); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| configProviderInstances.put(entry.getKey(), provider); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Plugin<ConfigProvider> providerPlugin = Plugin.wrapInstance(provider, null, CONFIG_PROVIDERS_CONFIG); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| configProviderPluginInstances.put(entry.getKey(), providerPlugin); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (ClassNotFoundException e) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| log.error("Could not load config provider class {}", entry.getValue(), e); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new ConfigException(providerClassProperty(entry.getKey()), entry.getValue(), "Could not load config provider class or one of its dependencies"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return configProviderInstances; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return configProviderPluginInstances; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+624
to
640
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Avoid resource leaks when instantiation/configuration fails; improve plugin tag key Close already-constructed plugins on failure and include the provider name in the plugin tag key for better metrics attribution. - Map<String, Plugin<ConfigProvider>> configProviderPluginInstances = new HashMap<>();
+ Map<String, Plugin<ConfigProvider>> configProviderPluginInstances = new HashMap<>();
for (Map.Entry<String, String> entry : providerMap.entrySet()) {
try {
String prefix = CONFIG_PROVIDERS_CONFIG + "." + entry.getKey() + CONFIG_PROVIDERS_PARAM;
Map<String, ?> configProperties = configProviderProperties(prefix, providerConfigProperties);
ConfigProvider provider = Utils.newInstance(entry.getValue(), ConfigProvider.class);
provider.configure(configProperties);
- Plugin<ConfigProvider> providerPlugin = Plugin.wrapInstance(provider, null, CONFIG_PROVIDERS_CONFIG);
+ Plugin<ConfigProvider> providerPlugin = Plugin.wrapInstance(provider, null, CONFIG_PROVIDERS_CONFIG + "." + entry.getKey());
configProviderPluginInstances.put(entry.getKey(), providerPlugin);
} catch (ClassNotFoundException e) {
log.error("Could not load config provider class {}", entry.getValue(), e);
+ // Close any providers constructed so far to avoid leaks
+ configProviderPluginInstances.values().forEach(p -> Utils.closeQuietly(p, "config provider plugin"));
throw new ConfigException(providerClassProperty(entry.getKey()), entry.getValue(), "Could not load config provider class or one of its dependencies");
+ } catch (RuntimeException e) {
+ // Close any providers constructed so far to avoid leaks
+ configProviderPluginInstances.values().forEach(p -> Utils.closeQuietly(p, "config provider plugin"));
+ throw e;
}
}
return configProviderPluginInstances;📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private static String providerClassProperty(String providerName) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |||||||
|
|
||||||||
| import org.apache.kafka.common.config.provider.ConfigProvider; | ||||||||
| import org.apache.kafka.common.config.provider.FileConfigProvider; | ||||||||
| import org.apache.kafka.common.internals.Plugin; | ||||||||
|
|
||||||||
| import java.util.ArrayList; | ||||||||
| import java.util.HashMap; | ||||||||
|
|
@@ -56,15 +57,15 @@ public class ConfigTransformer { | |||||||
| public static final Pattern DEFAULT_PATTERN = Pattern.compile("\\$\\{([^}]*?):(([^}]*?):)?([^}]*?)\\}"); | ||||||||
| private static final String EMPTY_PATH = ""; | ||||||||
|
|
||||||||
| private final Map<String, ConfigProvider> configProviders; | ||||||||
| private final Map<String, Plugin<ConfigProvider>> configProviderPlugins; | ||||||||
|
|
||||||||
| /** | ||||||||
| * Creates a ConfigTransformer with the default pattern, of the form <code>${provider:[path:]key}</code>. | ||||||||
| * | ||||||||
| * @param configProviders a Map of provider names and {@link ConfigProvider} instances. | ||||||||
| * @param configProviderPlugins a Map of provider names and {@link ConfigProvider} instances. | ||||||||
| */ | ||||||||
| public ConfigTransformer(Map<String, ConfigProvider> configProviders) { | ||||||||
| this.configProviders = configProviders; | ||||||||
| public ConfigTransformer(Map<String, Plugin<ConfigProvider>> configProviderPlugins) { | ||||||||
| this.configProviderPlugins = configProviderPlugins; | ||||||||
| } | ||||||||
|
|
||||||||
| /** | ||||||||
|
|
@@ -94,13 +95,13 @@ public ConfigTransformerResult transform(Map<String, String> configs) { | |||||||
| Map<String, Long> ttls = new HashMap<>(); | ||||||||
| for (Map.Entry<String, Map<String, Set<String>>> entry : keysByProvider.entrySet()) { | ||||||||
| String providerName = entry.getKey(); | ||||||||
| ConfigProvider provider = configProviders.get(providerName); | ||||||||
| Plugin<ConfigProvider> providerPlugin = configProviderPlugins.get(providerName); | ||||||||
| Map<String, Set<String>> keysByPath = entry.getValue(); | ||||||||
| if (provider != null && keysByPath != null) { | ||||||||
| if (providerPlugin != null && keysByPath != null) { | ||||||||
| for (Map.Entry<String, Set<String>> pathWithKeys : keysByPath.entrySet()) { | ||||||||
| String path = pathWithKeys.getKey(); | ||||||||
| Set<String> keys = new HashSet<>(pathWithKeys.getValue()); | ||||||||
| ConfigData configData = provider.get(path, keys); | ||||||||
| ConfigData configData = providerPlugin.get().get(path, keys); | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Resource Leak RiskThe Plugin wrapper adds an additional layer of object access with providerPlugin.get().get(path, keys). This double method call pattern creates potential resource leaks if the plugin instance isn't properly closed. The ConfigProvider resources are now wrapped in Plugin objects but the close() method implementation doesn't account for this new structure.
Suggested change
Standards
|
||||||||
| Map<String, String> data = configData.data(); | ||||||||
| Long ttl = configData.ttl(); | ||||||||
| if (ttl != null && ttl >= 0) { | ||||||||
|
|
||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * 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.config.provider; | ||
|
|
||
| import org.apache.kafka.common.MetricName; | ||
| import org.apache.kafka.common.config.ConfigData; | ||
| import org.apache.kafka.common.metrics.Measurable; | ||
| import org.apache.kafka.common.metrics.Monitorable; | ||
| import org.apache.kafka.common.metrics.PluginMetrics; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| public class MonitorableConfigProvider implements ConfigProvider, Monitorable { | ||
| public static final String NAME = "name"; | ||
| public static final String DESCRIPTION = "description"; | ||
| protected boolean configured = false; | ||
|
|
||
| @Override | ||
| public void withPluginMetrics(PluginMetrics metrics) { | ||
| MetricName metricName = metrics.metricName(NAME, DESCRIPTION, Map.of()); | ||
| metrics.addMetric(metricName, (Measurable) (config, now) -> 123); | ||
| } | ||
|
|
||
| @Override | ||
| public ConfigData get(String path) { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public ConfigData get(String path, Set<String> keys) { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws IOException { | ||
| } | ||
|
|
||
| @Override | ||
| public void configure(Map<String, ?> configs) { | ||
| configured = true; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.kafka.common.config.ConfigDef.Type; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.kafka.common.config.ConfigTransformer; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.kafka.common.config.provider.ConfigProvider; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.kafka.common.internals.Plugin; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.kafka.common.security.auth.SecurityProtocol; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.kafka.common.utils.Utils; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.kafka.connect.runtime.WorkerConfig; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -269,18 +270,19 @@ List<String> configProviders() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Map<String, String> transform(Map<String, String> props) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // transform worker config according to config.providers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| List<String> providerNames = configProviders(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Map<String, ConfigProvider> providers = new HashMap<>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Map<String, Plugin<ConfigProvider>> providerPlugins = new HashMap<>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (String name : providerNames) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ConfigProvider configProvider = plugins.newConfigProvider( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Plugin<ConfigProvider> configProviderPlugin = plugins.newConfigProvider( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| CONFIG_PROVIDERS_CONFIG + "." + name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Plugins.ClassLoaderUsage.PLUGINS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Plugins.ClassLoaderUsage.PLUGINS, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| null | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| providers.put(name, configProvider); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| providerPlugins.put(name, configProviderPlugin); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ConfigTransformer transformer = new ConfigTransformer(providers); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ConfigTransformer transformer = new ConfigTransformer(providerPlugins); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Map<String, String> transformed = transformer.transform(props).data(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| providers.values().forEach(x -> Utils.closeQuietly(x, "config provider")); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| providerPlugins.values().forEach(x -> Utils.closeQuietly(x, "config provider plugin")); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return transformed; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+273
to
286
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Guard against null providers and ensure plugins are closed on exceptions.
Proposed fix: skip nulls and move closing into a - Map<String, Plugin<ConfigProvider>> providerPlugins = new HashMap<>();
- for (String name : providerNames) {
- Plugin<ConfigProvider> configProviderPlugin = plugins.newConfigProvider(
- this,
- name,
- Plugins.ClassLoaderUsage.PLUGINS,
- null
- );
- providerPlugins.put(name, configProviderPlugin);
- }
- ConfigTransformer transformer = new ConfigTransformer(providerPlugins);
- Map<String, String> transformed = transformer.transform(props).data();
- providerPlugins.values().forEach(x -> Utils.closeQuietly(x, "config provider plugin"));
- return transformed;
+ Map<String, Plugin<ConfigProvider>> providerPlugins = new HashMap<>();
+ try {
+ for (String name : providerNames) {
+ Plugin<ConfigProvider> configProviderPlugin = plugins.newConfigProvider(
+ this,
+ name,
+ Plugins.ClassLoaderUsage.PLUGINS,
+ null
+ );
+ if (configProviderPlugin != null) {
+ providerPlugins.put(name, configProviderPlugin);
+ }
+ }
+ ConfigTransformer transformer = new ConfigTransformer(providerPlugins);
+ return transformer.transform(props).data();
+ } finally {
+ providerPlugins.values().forEach(x -> Utils.closeQuietly(x, "config provider plugin"));
+ }📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |||||
| import org.apache.kafka.common.config.ConfigTransformer; | ||||||
| import org.apache.kafka.common.config.ConfigTransformerResult; | ||||||
| import org.apache.kafka.common.config.provider.ConfigProvider; | ||||||
| import org.apache.kafka.common.internals.Plugin; | ||||||
| import org.apache.kafka.common.utils.Utils; | ||||||
| import org.apache.kafka.connect.runtime.Herder.ConfigReloadAction; | ||||||
| import org.apache.kafka.connect.util.Callback; | ||||||
|
|
@@ -42,12 +43,12 @@ public class WorkerConfigTransformer implements AutoCloseable { | |||||
| private final Worker worker; | ||||||
| private final ConfigTransformer configTransformer; | ||||||
| private final ConcurrentMap<String, Map<String, HerderRequest>> requests = new ConcurrentHashMap<>(); | ||||||
| private final Map<String, ConfigProvider> configProviders; | ||||||
| private final Map<String, Plugin<ConfigProvider>> configProviderPlugins; | ||||||
|
|
||||||
| public WorkerConfigTransformer(Worker worker, Map<String, ConfigProvider> configProviders) { | ||||||
| public WorkerConfigTransformer(Worker worker, Map<String, Plugin<ConfigProvider>> configProviderPlugins) { | ||||||
| this.worker = worker; | ||||||
| this.configProviders = configProviders; | ||||||
| this.configTransformer = new ConfigTransformer(configProviders); | ||||||
| this.configProviderPlugins = configProviderPlugins; | ||||||
| this.configTransformer = new ConfigTransformer(configProviderPlugins); | ||||||
| } | ||||||
|
|
||||||
| public Map<String, String> transform(Map<String, String> configs) { | ||||||
|
|
@@ -97,6 +98,6 @@ private void scheduleReload(String connectorName, String path, long ttl) { | |||||
|
|
||||||
| @Override | ||||||
| public void close() { | ||||||
| configProviders.values().forEach(x -> Utils.closeQuietly(x, "config provider")); | ||||||
| configProviderPlugins.values().forEach(x -> Utils.closeQuietly(x, "config provider plugin")); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unchecked Plugin ClosureClosing Plugin directly instead of its wrapped instance. The Plugin wrapper isn't Closeable, but the wrapped ConfigProvider is. This could prevent proper resource cleanup.
Suggested change
Standards
|
||||||
| } | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unchecked Plugin Closure
Closing Plugin directly instead of its wrapped instance. The Plugin wrapper itself isn't Closeable, but its wrapped ConfigProvider is. This could prevent proper resource cleanup.
Standards