Skip to content
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

fix: Kafka initialization occasionally failed due to concurrent injection of OpenTelemetryMetricsReporter (to #12538) #12583

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,24 @@ public static class ConstructorMapAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(
@Advice.Argument(value = 0, readOnly = false) Map<String, Object> config) {
// ensure config is a mutable map
if (config.getClass() != HashMap.class) {
config = new HashMap<>(config);
}

// In versions of spring-kafka prior to 2.5.0.RC1, when the `ProducerPerThread`
// of DefaultKafkaProducerFactory is set to true, the `config` object entering
// this advice block can be shared across multiple threads. Directly modifying
// `config` could lead to unexpected item loss due to race conditions, where
// some entries might be lost as different threads attempt to modify it
// concurrently.
//
// To prevent such issues, a copy of the `config` should be created here before
// any modifications are made. This ensures that each thread operates on its
// own independent copy of the configuration, thereby eliminating the risk of
// configurations corruption.
//
// More detailed information:
// https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/12538

// ensure config is a mutable map and avoid concurrency conflicts
config = new HashMap<>(config);
enhanceConfig(config);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,24 @@ public static class ConstructorMapAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(
@Advice.Argument(value = 0, readOnly = false) Map<String, Object> config) {
// ensure config is a mutable map
if (config.getClass() != HashMap.class) {
config = new HashMap<>(config);
}

// In versions of spring-kafka prior to 2.5.0.RC1, when the `ProducerPerThread`
// of DefaultKafkaProducerFactory is set to true, the `config` object entering
// this advice block can be shared across multiple threads. Directly modifying
// `config` could lead to unexpected item loss due to race conditions, where
// some entries might be lost as different threads attempt to modify it
// concurrently.
//
// To prevent such issues, a copy of the `config` should be created here before
// any modifications are made. This ensures that each thread operates on its
// own independent copy of the configuration, thereby eliminating the risk of
// configurations corruption.
//
// More detailed information:
// https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/12538

// ensure config is a mutable map and avoid concurrency conflicts
config = new HashMap<>(config);
enhanceConfig(config);
}
}
Expand Down
Loading