Comments (6)
As NonBlockingHashSet
delegates to NonBlockingHashMap
, the problem is also reproduced in NonBlockingHashMap
NonBlockingHashMap<MyEntry, String> map = new NonBlockingHashMap<>();
map.put(entry1, "a");
map.remove(entry1);
map.put(entry2, "a");
// entry1 is revived.
assert map.keySet().toArray()[0] == entry1;
from jctools.
The issue here is that:
- NBHM retains deleted keys (values in the set). This is a potential GC issue, but is inherent to the implementation.
MyEntry
is not a good key
Your code asserts that 2 entries are equal if the value
is the same, ignoring updatedAt
. NBHM therefore treats them as such and will not replace the reference to the first key with the second.
from jctools.
I'm closing, please comment if you feel this is breaking the Map
API somehow.
from jctools.
please comment if you feel this is breaking the Map API somehow.
Yes, I think so. It is unexpected behavior and causes bugs in our project.
-
Background
MyEntry
is an intentional example to reproduce the problem. We provideEndpoint
that represents a remote host and port. In addition to host and port,Endpoint
also has weight property for client-side load balancing.
Endpoint
excludes the additional property - weight fromequals
andhashCode
for better comparison of remote peers.
https://github.com/line/armeria/blob/bcb621e46c26f0ac84b4b820687383a99b509019/core/src/main/java/com/linecorp/armeria/client/Endpoint.java#L666-L668 -
Problem
We usedNonBlockingHashSet
for maintaining healthy endpoints. When a weight of a endpoint is changed, we need to remove the old endpoint and add the new endpoint.
However,NonBlockingHashSet
does not replace the old endpoint with the new endpoint even though I calledremove
andput
explicitly.
https://github.com/line/armeria/pull/3927/files#diff-4229d073a6969fa85c1d6fe69c6c3d1c957a6a240dd77e3e397b3bf4c96792f0
As a workaround, I decided to useConcurrentHashMap
.
- final Set<Endpoint> healthyEndpoints = new NonBlockingHashSet<>();
+ final Set<Endpoint> healthyEndpoints = ConcurrentHashMap.newKeySet();
from jctools.
@ikhoon I understand your frustration. I don't think this is an issue that we can easily fix within the confines of the current implementation however.
- NBHM hangs on to deleted keys until resize.
- NBHM will not overwrite key (deleted or otherwise) with a new key which is equal to it on
put
.
Changing this latter behaviour will slow down all operations on map by adding more code to a critical path which is already crowded. It can be done (by specialising behaviour on previous value being TOMBSTONE
and key being equal), but I would need a strong argument from Map
/Set
documentation which clearly shows this behaviour to be wrong.
from jctools.
Thanks for your consideration. As I knew the expected behavior of NBHM, I can carefully use it. Mentioning this behavior in Javadoc would be super helpful for users. 🙇♂️
from jctools.
Related Issues (20)
- Unexpected jctools-core 3.3.1-ea release HOT 2
- O(1) insertion, fully non-blocking data structure HOT 4
- Publish test jar as part of the release
- any plans on fitting virtual threads HOT 1
- Missing `Automatic-Module-Name` in 4.0.1 (regression) HOT 2
- Add a parallel `forEach()` method in `NonBlockingHashMap` HOT 2
- Counters probe can be negative (and shouldn't?) HOT 3
- Add copyright information HOT 1
- MpscBlockingConsumerArrayQueue need override clear() method to avoid blocking when queue is empty HOT 2
- NonBlockingHashMap's computeIfAbsentc has a bug in a multithreaded environment HOT 11
- about spmc broadcast problem HOT 2
- the way of MpscBlockingConsumerArrayQueue provide offerFirst? HOT 1
- BaseMpscLinkedArrayQueue.peek() causes 100% CPU util [problem in caller code] HOT 2
- (cIndex - pIndex) / 2 == 0 return null;If it is being expanded, wait a little while for the expansion to succeed, there will be a JUMP, and should not return null HOT 5
- Question: "Emulating" Guava's Queues.drain on SpscLinkedQueue HOT 3
- New release to include JPMS module changes
- src/main/java/org/jctools/maps/NonBlockingHashMap.java:1415: error: missing reference label HOT 2
- Maven central 4.0.2 jar is compiled to Java 8, no longer supporting java 7 HOT 2
- unexpected MpscBlockingConsumerArrayQueue polling timeout behavior
- Unpadded variants could avoid using Unsafe HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jctools.