Skip to content

fix(QTDI-2567) prevent CacheMap eviction issue#1181

Open
thboileau wants to merge 1 commit intomasterfrom
tboileau/QTDI-2567_cacheMap_eviction_fix
Open

fix(QTDI-2567) prevent CacheMap eviction issue#1181
thboileau wants to merge 1 commit intomasterfrom
tboileau/QTDI-2567_cacheMap_eviction_fix

Conversation

@thboileau
Copy link
Contributor

@thboileau thboileau commented Mar 23, 2026

Requirements

  • Any code change adding any logic MUST be tested through a unit test executed with the default build
  • Any API addition MUST be done with a documentation update if relevant

Why this PR is needed?

Component-server maintains two maps with a size limit. The code that should take care of this limit throws an exception when called

What does this PR adds (design/code thoughts)?

It prevents the exception, and a test case is also provided.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a runtime exception in MapCache.evictIfNeeded() when trimming concurrent caches used by component-server resources, and adds unit tests to validate eviction behavior.

Changes:

  • Fix eviction logic to remove entries safely via iterator next() + remove(), and treat maxSize <= 0 as “clear cache”.
  • Add JUnit 5 parameterized tests covering emptying, trimming, and no-op scenarios for evictIfNeeded().
  • Minor comment typo fixes in MapCache.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
component-server-parent/component-server/src/main/java/org/talend/sdk/component/server/lang/MapCache.java Fixes eviction implementation to avoid iterator misuse exceptions when enforcing max cache size.
component-server-parent/component-server/src/test/java/org/talend/sdk/component/server/lang/MapCacheTest.java Adds unit tests for eviction behavior (empty, trimmed, unchanged).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +34 to +38
final Iterator<A> it = cache.keySet().iterator();
int excess = cache.size() - maxSize;
while (excess-- > 0 && it.hasNext()) {
it.next();
it.remove();
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

java.util.Map is still imported in this class, but after switching to a keySet() iterator the type is no longer referenced. In Java, unused imports are compilation errors, so this will break the build; please remove the unused Map import (and any other now-unused imports).

Copilot uses AI. Check for mistakes.
Comment on lines +55 to +58
return Stream.of(
Arguments.of(new ConcurrentHashMap<>(Map.of("1", "2", "3", "4", "5", "6")), 1),
Arguments.of(new ConcurrentHashMap<>(Map.of("1", "2", "3", "4", "5", "6")), 2)
);
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Map.of("1","2","3","4","5","6") creates a map with 3 entries (pairs), not 6. This makes the eviction test much weaker than intended and can mask regressions; please build a map with 6 entries (e.g., Map.ofEntries(...) or explicit puts) so the starting size is actually > maxSize by a larger margin.

Copilot uses AI. Check for mistakes.
Comment on lines +70 to +73
return Stream.of(
Arguments.of(new ConcurrentHashMap<>(Map.of("1", "2", "3", "4", "5", "6")), 3),
Arguments.of(new ConcurrentHashMap<>(Map.of("1", "2", "3", "4", "5", "6")), 4)
);
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issue here: Map.of("1","2","3","4","5","6") only creates 3 entries. If the intent is to validate that the cache is not modified when maxSize is comfortably above the current size, consider using a map with 6 actual entries and choosing maxSize accordingly to make this test assert what its name suggests.

Copilot uses AI. Check for mistakes.
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.

2 participants