github-actions[bot] commented on code in PR #62117:
URL: https://github.com/apache/doris/pull/62117#discussion_r3110532203
##########
fe/fe-foundation/src/main/java/org/apache/doris/foundation/util/ConcurrentLong2LongHashMap.java:
##########
@@ -99,10 +109,19 @@ private Segment segmentFor(long key) {
return segments[(int) (mix(key) >>> (64 - segmentBits)) & segmentMask];
}
+ private void checkNotInCallback() {
Review Comment:
Using `inCallback.get()` as the guard here has an unfortunate side effect
because the field was created with `ThreadLocal.withInitial(() ->
Boolean.FALSE)`: the first normal read/write on any thread installs a
`Boolean.FALSE` entry in that thread's `ThreadLocalMap`, even when no callback
is active. Since `checkNotInCallback()` now runs on every public method, each
long-lived FE thread that touches this map keeps one thread-local slot per map
instance for the lifetime of the thread/map. That is a pretty large memory
regression for a utility whose whole purpose is reducing heap usage. A plain
`new ThreadLocal<>()` plus `Boolean.TRUE.equals(inCallback.get())` avoids
creating per-thread state outside active callbacks.
##########
fe/fe-foundation/src/main/java/org/apache/doris/foundation/util/ConcurrentLong2LongHashMap.java:
##########
@@ -422,7 +497,13 @@ public long mergeLong(long key, long value,
LongBinaryOperator remappingFunction
Review Comment:
The callback guard is still incomplete here. `compute*` / `merge*` callbacks
can still call `keySet()`, `keyList()`, `values()`, `long2LongEntrySet()` /
inherited `entrySet()`, and this `forEach()` because none of those snapshot
helpers call `checkNotInCallback()`. Two threads can still deadlock with
`compute(kA, ...) -> map.keySet()` / `compute(kB, ...) -> map.keySet()`: each
holds one segment's write lock and then tries to take read locks on the other
segment while building the snapshot. Since the new contract says callbacks must
not access the map at all, these helpers need the same guard and a regression
test.
##########
fe/fe-foundation/src/main/java/org/apache/doris/foundation/util/ConcurrentLong2ObjectHashMap.java:
##########
@@ -367,13 +427,19 @@ public V compute(long key, BiFunction<? super Long, ?
super V, ? extends V> rema
}
public V merge(long key, V value, BiFunction<? super V, ? super V, ?
extends V> remappingFunction) {
+ checkNotInCallback();
Segment<V> seg = segmentFor(key);
seg.lock.writeLock().lock();
try {
V oldValue = seg.map.get(key);
V newValue;
if (oldValue != null || seg.map.containsKey(key)) {
- newValue = remappingFunction.apply(oldValue, value);
+ inCallback.set(Boolean.TRUE);
+ try {
+ newValue = remappingFunction.apply(oldValue, value);
Review Comment:
The same callback-deadlock hole is still open in this class for the snapshot
helpers. `compute*` / `merge*` callbacks can still call `keySet()`,
`keyList()`, `values()`, `long2ObjectEntrySet()` / inherited `entrySet()`, and
this `forEach()` because those methods do not call `checkNotInCallback()`. Two
threads doing `compute(kA, ...) -> map.keySet()` / `compute(kB, ...) ->
map.keySet()` can still deadlock while each holds one segment write lock and
waits on the other's read lock. If callbacks are supposed to treat the map as
completely off-limits, these helpers need the same guard and a regression test.
##########
fe/fe-foundation/src/main/java/org/apache/doris/foundation/util/ConcurrentLong2ObjectHashMap.java:
##########
@@ -95,10 +108,19 @@ private Segment<V> segmentFor(long key) {
return segments[(int) (mix(key) >>> (64 - segmentBits)) & segmentMask];
}
+ private void checkNotInCallback() {
Review Comment:
Using `inCallback.get()` as the guard here has the same memory side effect
as in the long-long map. Because the field was created with
`ThreadLocal.withInitial(() -> Boolean.FALSE)`, the first normal access from
any thread installs a `Boolean.FALSE` entry in that thread's `ThreadLocalMap`
even when no callback is active. With long-lived FE worker threads, that
becomes one thread-local slot per map instance for the life of the thread/map.
A plain `new ThreadLocal<>()` plus `Boolean.TRUE.equals(inCallback.get())`
avoids creating thread-local state outside active callbacks.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]