Repository navigation
fix(artifacts): ensure thread safety in InMemoryArtifactService under concurrent access - #1916
Amansingh0807 wants to merge 1 commit into
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
|
Hi @Amansingh0807, We appreciate your contribution and the effort you put into this pull request. It is currently under review by our team. We will notify you if any additional details are needed. |
kvmilos
left a comment
There was a problem hiding this comment.
Thanks for fixing this. Making the service thread-safe is the right call, but a few things need to change before we can pull it. Details are inline.
One more point that can't be attached inline because the line isn't in the diff: now that this class is safe to share across threads, could the class Javadoc (InMemoryArtifactService.java:37) say so? Then callers know they can rely on it, and later changes know to keep it that way.
f2756dc to
8e32ddd
Compare
|
Thanks for the thorough review, @kvmilos, I have rebased onto
All 14 tests in |
… concurrent access
8e32ddd to
d8aec29
Compare
Please ensure you have read the contribution guide before creating a pull request.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
2. Or, if no issue exists, describe the change:
Problem:
InMemoryArtifactServicestored its multi-level mapping (appName -> userId -> sessionId -> filename -> List<Part>) using unsynchronizedjava.util.HashMapinstances (private final Map<String, Map<String, Map<String, Map<String, List<Part>>>>> artifacts;) and version histories using a standardjava.util.ArrayList.Under concurrent multi-agent executions (such as
ParallelAgentfan-out branches executing across parallel RxJava worker threads, background agent reactive streams, or concurrent HTTP requests in ADK Dev Server / Spring Boot / A2A server), concurrent invocations ofsaveArtifact(),loadArtifact(),listArtifactKeys(), anddeleteArtifact()triggered:ConcurrentModificationExceptionduring bucket rehashing or traversal incomputeIfAbsent().HashMapbucket pointers under high concurrency.ArrayList.add(artifact), causing concurrent versions to overwrite each other during internal array resizing, resulting in duplicate or corrupted version indices being returned viaSingle.just(versions.size() - 1).In contrast,
InMemorySessionServiceexplicitly usesConcurrentHashMapacross all dimensions (ConcurrentMap<String, ConcurrentMap<String, ConcurrentMap<String, Session>>>) for thread safety.InMemoryArtifactServicelacked equivalent guarantees.Solution:
artifactsand all intermediate nested sub-maps inInMemoryArtifactServicetoConcurrentMapbacked byConcurrentHashMap:ArrayListwithCopyOnWriteArrayListfor artifact version storage, eliminating lock contention for concurrent reads (loadArtifact()).InMemoryArtifactServicedirectly with the concurrency model established byInMemorySessionService.Testing Plan
Unit Tests:
Summary of Passed Java Test Results (
mvnw test -pl core -Dtest=InMemoryArtifactServiceTest):Manual End-to-End (E2E) Tests:
Added two dedicated high-concurrency stress tests in
InMemoryArtifactServiceTest.java:saveArtifact_concurrentAccess_isThreadSafe:ExecutorServicewith 16 parallel threads coordinated viaCountDownLatch.ConcurrentModificationExceptionor runtime failures under high thread contention.saveArtifact_concurrentAppendsToSameFile_preservesDistinctMonotonicVersions:CountDownLatch.ConcurrentSkipListSet.[0..19]are allocated strictly, uniquely, and monotonically without skips, duplicates, or races.Checklist
Additional context
f2756dcf fix(artifacts): ensure thread safety in InMemoryArtifactService under concurrent access) strictly meeting the single-commit CI requirement.cc @MiloszSobczyk, @damianmomotgoogle, @sherryfox and @hemasekhar-p