BloomFilter multithread problem: put() NOT atomic
See original GitHub issueHello team!
I just found a problem using com.google.common.hash.BloomFilter
with more than one thread. Long story short: put()
is not atomic so I am able to reproduce a false negative.
This is the config in pom.xml
:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>24.0-jre</version>
</dependency>
I created a simple Test.java class that proves the point. The fix is also included in the file: just uncomment lines 56 and 58 (make put()
synchronized).
Looking at the BloomFilter
javadoc I see:
* As of Guava 23.0, this class is thread-safe and lock-free. It internally uses atomics and
* compare-and-swap to ensure correctness when multiple threads are used to access it.
So I suppose that I’m not doing anything “wrong”. Can you please confirm the issue or tell me where I’m using the library wrong?
Thanks!
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Concurrent bloom filters - Chris Stucchio
Concurrent reads are no problem, but writes are trickier - and reading while writing is also not straightforward.
Read more >thread-safe bloomfilter - java - Stack Overflow
I'm imagining the effects of replacing the long[] with an AtomicLongArray , and I'm pretty sure the semantics you end up with are...
Read more >Java Multithreading (Hard) Question. Any idea? - LeetCode
Where will the concurrent writes by the background thread go? To a new list so that it does not affect the current list....
Read more >BloomFilter (Guava: Google Core Libraries for Java 23.0 API)
The false positive probability ( FPP ) of a Bloom filter is defined as the ... return true for an object that has...
Read more >On Adapting the Bloom Filter to Multithreaded Environments
To ensure consistency and data access ordering, all k values need to be updated atomically. This is not possible with atomic CPU operations...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
My understanding was that
thread-safe
andlock-free
in the JavaDoc does not guarantee that the entire operation is atomic. This allowed the small design change in #2748 to be racy, assuming that the results were overall okay.The alternative that I proposed was more invasive, by using a Bloom-1 design. If redesigning the implementation to target concurrency, then I think it would be better to adopt that. But the goal was to expand usage to allow best effort concurrency, so the implementation change seemed reasonable as is.
I’m not sure I see a thread-safe, lock-free solution that returns a meaningful value from
put
. We might be in a position of being forced to break one of those API specifications.