Skip to content

Fix: Prevent static cache pollution affecting decimal.format across concurrent connectors - #309

Open
kotapullarao wants to merge 2 commits into
Aiven-Open:mainfrom
kotapullarao:fix/decimal-format-static-cache-pollution
Open

kotapullarao wants to merge 2 commits into
Aiven-Open:mainfrom
kotapullarao:fix/decimal-format-static-cache-pollution

Conversation

@kotapullarao

Copy link
Copy Markdown

Summary

Fixes converter cache sharing between HTTP sink connector instances running in the same Kafka Connect worker.

RecordValueConverter previously used a static cache keyed only by the runtime record value class. This could allow one connector instance to reuse a converter initialized by another connector instance with a different decimal.format configuration.

This PR changes the converter cache to be instance-scoped, so each RecordValueConverter keeps converters created from its own connector configuration.

Fixes #308.

Problem

decimal.format is a connector-level configuration. A connector configured with:

decimal.format=NUMERIC

should serialize Avro decimal logical types as JSON numbers in the HTTP request body.

However, when multiple HTTP sink connector instances run in the same Kafka Connect worker with different decimal.format values, the configured behavior can leak between connector instances.

Example:

  • Connector A uses the default decimal.format=BASE64
  • Connector B uses decimal.format=NUMERIC
  • Both connectors process structured Kafka Connect values, such as Avro records represented as Struct

Because the converter cache was static and keyed only by runtime value class, a converter cached for Struct.class by one connector could be reused by the other connector, regardless of its own decimal.format.

Root Cause

In RecordValueConverter, converters were cached in a static map:

private static final ConcurrentHashMap<Class<?>, Converter> RUNTIME_CLASS_TO_CONVERTER_CACHE =
    new ConcurrentHashMap<>();

The cache key was only:

record.value().getClass()

This means connector-specific conversion settings, such as decimal.format, were not part of the cache identity.

The clear() call in create() reduced stale state after task restarts, but it did not provide proper isolation between concurrently running connector instances in the same worker JVM.

Solution

Changed the converter cache from static/global to instance-level:

private final ConcurrentHashMap<Class<?>, Converter> runtimeClassToConverterCache =
    new ConcurrentHashMap<>();

Each RecordValueConverter instance now maintains its own cache, using converters created from its own HttpSinkConfig.

Changes Made

  • Converted RUNTIME_CLASS_TO_CONVERTER_CACHE from a static field to an instance field.
  • Renamed it to runtimeClassToConverterCache to follow instance field naming conventions.
  • Removed the static cache clear() call from create(), since each converter instance now starts with its own empty cache.
  • Updated cache access to use the instance-scoped cache.

Impact

  • Prevents converter configuration leakage between connector instances.
  • Ensures each connector uses its own decimal.format configuration consistently.
  • Preserves converter caching within each RecordValueConverter instance.
  • Maintains backward-compatible behavior for single-connector deployments.

Testing

  • Added a regression test in RecordValueConverterTest:
    • creates one RecordValueConverter with decimal.format=BASE64
    • creates another RecordValueConverter with decimal.format=NUMERIC
    • converts the same Avro decimal Struct value with both converters
    • verifies each converter uses its own configured decimal format
  • This test would fail with the previous static cache implementation if the BASE64 converter cached the Struct converter first.
  • Ran the focused test class locally:
./gradlew test --tests io.aiven.kafka.connect.http.converter.RecordValueConverterTest

Result:

BUILD SUCCESSFUL

Related Issues/PRs

@kotapullarao
kotapullarao requested a review from a team as a code owner August 14, 2026 13:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Static cache pollution affects decimal.format configuration across concurrent connectors

1 participant