This is an automated email from the ASF dual-hosted git repository.
NSAmelchev pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ignite.git
The following commit(s) were added to refs/heads/master by this push:
new 6fb70e4c2f6 IGNITE-29169 Use CopyOnWriteIntMap for rarely updated maps
on cache hot paths (#13706)
6fb70e4c2f6 is described below
commit 6fb70e4c2f6597db909cfaba5f853fa49e18ea38
Author: Nikita Amelchev <[email protected]>
AuthorDate: Mon Oct 26 16:20:21 2026 +0300
IGNITE-29169 Use CopyOnWriteIntMap for rarely updated maps on cache hot
paths (#13706)
---
.../cache/GridCacheConcurrentMapImpl.java | 8 +--
.../processors/cache/GridCacheContext.java | 12 -----
.../cache/GridCacheLocalConcurrentMap.java | 2 +-
.../cache/IgniteCacheOffheapManagerImpl.java | 6 ++-
.../binary/CacheObjectBinaryProcessorImpl.java | 4 +-
.../dht/topology/GridDhtLocalPartition.java | 10 ++--
.../cache/persistence/DataRegionMetricsImpl.java | 59 +++++-----------------
.../util/collection/CopyOnWriteIntMap.java | 9 +++-
.../util/collection/CopyOnWriteIntMapTest.java | 7 +++
9 files changed, 45 insertions(+), 72 deletions(-)
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheConcurrentMapImpl.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheConcurrentMapImpl.java
index 471a17ecfb5..f860030414f 100644
---
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheConcurrentMapImpl.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheConcurrentMapImpl.java
@@ -53,7 +53,7 @@ public abstract class GridCacheConcurrentMapImpl implements
GridCacheConcurrentM
/** {@inheritDoc} */
@Nullable @Override public GridCacheMapEntry getEntry(GridCacheContext
ctx, KeyCacheObject key) {
- CacheMapHolder hld = entriesMapIfExists(ctx.cacheIdBoxed());
+ CacheMapHolder hld = entriesMapIfExists(ctx.cacheId());
key =
(KeyCacheObject)ctx.kernalContext().cacheObjects().prepareForCache(key, ctx);
@@ -87,7 +87,7 @@ public abstract class GridCacheConcurrentMapImpl implements
GridCacheConcurrentM
final boolean skipReserve
) {
if (hld == null)
- hld = entriesMapIfExists(ctx.cacheIdBoxed());
+ hld = entriesMapIfExists(ctx.cacheId());
GridCacheMapEntry cur = null;
GridCacheMapEntry created = null;
@@ -247,7 +247,7 @@ public abstract class GridCacheConcurrentMapImpl implements
GridCacheConcurrentM
* @param cacheId Cache ID.
* @return Map for given cache ID.
*/
- @Nullable protected abstract CacheMapHolder entriesMapIfExists(Integer
cacheId);
+ @Nullable protected abstract CacheMapHolder entriesMapIfExists(int
cacheId);
/**
*
@@ -279,7 +279,7 @@ public abstract class GridCacheConcurrentMapImpl implements
GridCacheConcurrentM
@Override public boolean removeEntry(final GridCacheEntryEx entry) {
GridCacheContext ctx = entry.context();
- CacheMapHolder hld = entriesMapIfExists(ctx.cacheIdBoxed());
+ CacheMapHolder hld = entriesMapIfExists(ctx.cacheId());
boolean rmv = hld != null && hld.map.remove(entry.key(), entry);
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheContext.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheContext.java
index 90893e6e098..68843a88050 100644
---
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheContext.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheContext.java
@@ -223,9 +223,6 @@ public class GridCacheContext<K, V> implements
Externalizable {
/** Cache ID. */
private int cacheId;
- /** Cache ID. */
- private Integer cacheIdBoxed;
-
/** Cache type. */
private CacheType cacheType;
@@ -404,8 +401,6 @@ public class GridCacheContext<K, V> implements
Externalizable {
cacheId = CU.cacheId(cacheName);
- cacheIdBoxed = cacheId;
-
plc = cacheType.ioPolicy();
Factory<ExpiryPolicy> factory = cacheCfg.getExpiryPolicyFactory();
@@ -596,13 +591,6 @@ public class GridCacheContext<K, V> implements
Externalizable {
return cacheId;
}
- /**
- * @return Cache ID.
- */
- public Integer cacheIdBoxed() {
- return cacheIdBoxed;
- }
-
/**
* @return {@code True} if should use system transactions which are
isolated from user transactions.
*/
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheLocalConcurrentMap.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheLocalConcurrentMap.java
index b4c93cf9db1..2438d816304 100644
---
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheLocalConcurrentMap.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheLocalConcurrentMap.java
@@ -55,7 +55,7 @@ public class GridCacheLocalConcurrentMap extends
GridCacheConcurrentMapImpl {
}
/** {@inheritDoc} */
- @Nullable @Override protected CacheMapHolder entriesMapIfExists(Integer
cacheId) {
+ @Nullable @Override protected CacheMapHolder entriesMapIfExists(int
cacheId) {
return entryMap;
}
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheOffheapManagerImpl.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheOffheapManagerImpl.java
index 78d8b651359..31a01344897 100644
---
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheOffheapManagerImpl.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheOffheapManagerImpl.java
@@ -75,9 +75,9 @@ import
org.apache.ignite.internal.util.GridEmptyCloseableIterator;
import org.apache.ignite.internal.util.GridLongList;
import org.apache.ignite.internal.util.GridSpinBusyLock;
import org.apache.ignite.internal.util.GridStripedLock;
+import org.apache.ignite.internal.util.collection.CopyOnWriteIntMap;
import org.apache.ignite.internal.util.collection.ImmutableIntSet;
import org.apache.ignite.internal.util.collection.IntMap;
-import org.apache.ignite.internal.util.collection.IntRWHashMap;
import org.apache.ignite.internal.util.collection.IntSet;
import org.apache.ignite.internal.util.lang.GridCloseableIterator;
import org.apache.ignite.internal.util.lang.GridCursor;
@@ -1224,7 +1224,7 @@ public class IgniteCacheOffheapManagerImpl implements
IgniteCacheOffheapManager
private final LongAdder storageSize = new LongAdder();
/** */
- private final IntMap<AtomicLong> cacheSizes = new IntRWHashMap<>();
+ private final IntMap<AtomicLong> cacheSizes;
/** */
private final IgniteLogger log;
@@ -1260,6 +1260,8 @@ public class IgniteCacheOffheapManagerImpl implements
IgniteCacheOffheapManager
this.busyLock = busyLock;
this.log = log;
+ cacheSizes = grp.sharedGroup() ? new CopyOnWriteIntMap<>() : null;
+
PartitionUpdateCounter delegate = !grp.persistenceEnabled() ||
grp.hasAtomicCaches() ?
new PartitionUpdateCounterVolatileImpl(grp) :
new PartitionUpdateCounterTrackingImpl(grp);
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/binary/CacheObjectBinaryProcessorImpl.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/binary/CacheObjectBinaryProcessorImpl.java
index 1d2dd384531..cab90a3f30d 100644
---
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/binary/CacheObjectBinaryProcessorImpl.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/binary/CacheObjectBinaryProcessorImpl.java
@@ -86,6 +86,8 @@ import
org.apache.ignite.internal.processors.datastructures.CollocatedSetItemKey
import org.apache.ignite.internal.processors.query.QueryUtils;
import org.apache.ignite.internal.systemview.BinaryMetadataViewWalker;
import org.apache.ignite.internal.util.MutableSingletonList;
+import org.apache.ignite.internal.util.collection.CopyOnWriteIntMap;
+import org.apache.ignite.internal.util.collection.IntMap;
import org.apache.ignite.internal.util.future.GridFutureAdapter;
import org.apache.ignite.internal.util.lang.GridMapEntry;
import org.apache.ignite.internal.util.tostring.GridToStringExclude;
@@ -168,7 +170,7 @@ public class CacheObjectBinaryProcessorImpl extends
GridProcessorAdapter impleme
private BinaryMetadataTransport transport;
/** Cached affinity key field names. */
- private final ConcurrentHashMap<Integer, T1<BinaryField>> affKeyFields =
new ConcurrentHashMap<>();
+ private final IntMap<T1<BinaryField>> affKeyFields = new
CopyOnWriteIntMap<>();
/** Dummy {@code CacheObjectValueContext} used for mocking. */
private CacheObjectValueContext fakeCacheObjCtx;
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/topology/GridDhtLocalPartition.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/topology/GridDhtLocalPartition.java
index 6fa941cfa9a..803b806aecf 100644
---
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/topology/GridDhtLocalPartition.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/topology/GridDhtLocalPartition.java
@@ -60,8 +60,8 @@ import
org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx
import org.apache.ignite.internal.processors.cache.transactions.TxCounters;
import org.apache.ignite.internal.processors.cache.version.GridCacheVersion;
import org.apache.ignite.internal.util.GridLongList;
+import org.apache.ignite.internal.util.collection.CopyOnWriteIntMap;
import org.apache.ignite.internal.util.collection.IntMap;
-import org.apache.ignite.internal.util.collection.IntRWHashMap;
import org.apache.ignite.internal.util.future.GridFinishedFuture;
import org.apache.ignite.internal.util.future.GridFutureAdapter;
import org.apache.ignite.internal.util.lang.GridIterator;
@@ -198,7 +198,7 @@ public class GridDhtLocalPartition extends
GridCacheConcurrentMapImpl implements
if (grp.sharedGroup()) {
singleCacheEntryMap = null;
- cacheMaps = new IntRWHashMap<>();
+ cacheMaps = new CopyOnWriteIntMap<>();
}
else {
GridCacheContext cctx = grp.singleCacheContext();
@@ -276,7 +276,7 @@ public class GridDhtLocalPartition extends
GridCacheConcurrentMapImpl implements
}
/** {@inheritDoc} */
- @Nullable @Override protected CacheMapHolder entriesMapIfExists(Integer
cacheId) {
+ @Nullable @Override protected CacheMapHolder entriesMapIfExists(int
cacheId) {
return grp.sharedGroup() ? cacheMaps.get(cacheId) :
singleCacheEntryMap;
}
@@ -287,7 +287,7 @@ public class GridDhtLocalPartition extends
GridCacheConcurrentMapImpl implements
private CacheMapHolder cacheMapHolder(GridCacheContext cctx) {
assert grp.sharedGroup();
- CacheMapHolder hld = cacheMaps.get(cctx.cacheIdBoxed());
+ CacheMapHolder hld = cacheMaps.get(cctx.cacheId());
if (hld != null)
return hld;
@@ -295,7 +295,7 @@ public class GridDhtLocalPartition extends
GridCacheConcurrentMapImpl implements
if (cctx.isNear())
cctx = cctx.near().dht().context();
- CacheMapHolder old = cacheMaps.putIfAbsent(cctx.cacheIdBoxed(), hld =
ctx.kernalContext().resource().resolve(
+ CacheMapHolder old = cacheMaps.putIfAbsent(cctx.cacheId(), hld =
ctx.kernalContext().resource().resolve(
new CacheMapHolder(cctx, createEntriesMap())));
if (old != null)
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/DataRegionMetricsImpl.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/DataRegionMetricsImpl.java
index 3302e260808..40e938ae707 100644
---
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/DataRegionMetricsImpl.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/DataRegionMetricsImpl.java
@@ -36,7 +36,7 @@ import
org.apache.ignite.internal.processors.metric.impl.LongAdderMetric;
import
org.apache.ignite.internal.processors.metric.impl.LongAdderWithDelegateMetric;
import org.apache.ignite.internal.processors.metric.impl.MetricUtils;
import
org.apache.ignite.internal.processors.metric.impl.PeriodicHistogramMetricImpl;
-import org.apache.ignite.internal.util.collection.IntHashMap;
+import org.apache.ignite.internal.util.collection.CopyOnWriteIntMap;
import org.apache.ignite.internal.util.collection.IntMap;
import org.apache.ignite.internal.util.typedef.internal.U;
import org.apache.ignite.lang.IgniteBiTuple;
@@ -95,16 +95,8 @@ public class DataRegionMetricsImpl implements
DataRegionMetrics {
/** */
private final DataRegionMetricsProvider dataRegionMetricsProvider;
- /**
- * Cache group ID -> Cache group memory page metrics.
- * <p>
- * This is effectively a copy-on-write hash map, all write operations
should create a new copy under the
- * {@link #cacheGrpMetricsLock} lock.
- */
- private volatile IntMap<PageMetrics> cacheGrpMetrics = new IntHashMap<>();
-
- /** Lock for the {@link #cacheGrpMetrics} field. */
- private final Object cacheGrpMetricsLock = new Object();
+ /** Cache group ID -> Cache group memory page metrics. */
+ private final IntMap<PageMetrics> cacheGrpMetrics = new
CopyOnWriteIntMap<>();
/** Memory page metrics for the whole region. */
private final PageMetrics dataRegionPageMetrics;
@@ -606,31 +598,14 @@ public class DataRegionMetricsImpl implements
DataRegionMetrics {
if (pageMetrics != null)
return pageMetrics;
- synchronized (cacheGrpMetricsLock) {
- IntMap<PageMetrics> locCacheGrpMetrics = cacheGrpMetrics;
-
- // double check
- PageMetrics doubleCheckPageMetrics =
locCacheGrpMetrics.get(cacheGrpId);
- if (doubleCheckPageMetrics != null)
- return doubleCheckPageMetrics;
-
- IntMap<PageMetrics> copy = new IntHashMap<>(locCacheGrpMetrics);
-
- PageMetrics newMetrics = Optional.of(kernalCtx)
- // both cache and group descriptor can be null
- .map(GridKernalContext::cache)
- .map(cache -> cache.cacheGroupDescriptors().get(cacheGrpId))
- .map(decs ->
createCacheGrpPageMetrics(decs.cacheOrGroupName()))
- // return region-wide metrics for a non-existent cache group
ID. This is needed in some scenarios
- // when per-group data structures are used on a per-region
basis and in tests.
- .orElse(dataRegionPageMetrics);
-
- copy.put(cacheGrpId, newMetrics);
-
- cacheGrpMetrics = copy;
-
- return newMetrics;
- }
+ return cacheGrpMetrics.computeIfAbsent(cacheGrpId, id ->
Optional.of(kernalCtx)
+ // both cache and group descriptor can be null
+ .map(GridKernalContext::cache)
+ .map(cache -> cache.cacheGroupDescriptors().get(id))
+ .map(decs -> createCacheGrpPageMetrics(decs.cacheOrGroupName()))
+ // return region-wide metrics for a non-existent cache group ID.
This is needed in some scenarios
+ // when per-group data structures are used on a per-region basis
and in tests.
+ .orElse(dataRegionPageMetrics));
}
/**
@@ -649,16 +624,8 @@ public class DataRegionMetricsImpl implements
DataRegionMetrics {
/**
* Removes all memory page metrics associated with the given cache group.
*/
- public void removeCacheGrpPageMetrics(Integer grpId) {
- PageMetrics rmvMetrics;
-
- synchronized (cacheGrpMetricsLock) {
- IntMap<PageMetrics> copy = new IntHashMap<>(cacheGrpMetrics);
-
- rmvMetrics = copy.remove(grpId);
-
- cacheGrpMetrics = copy;
- }
+ public void removeCacheGrpPageMetrics(int grpId) {
+ PageMetrics rmvMetrics = cacheGrpMetrics.remove(grpId);
// we don't decrease the total pages counter for historical reasons
// (it hasn't been done in previous implementations)
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/util/collection/CopyOnWriteIntMap.java
b/modules/core/src/main/java/org/apache/ignite/internal/util/collection/CopyOnWriteIntMap.java
index c648942fa5e..d1d536cfd24 100644
---
a/modules/core/src/main/java/org/apache/ignite/internal/util/collection/CopyOnWriteIntMap.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/util/collection/CopyOnWriteIntMap.java
@@ -107,7 +107,14 @@ public class CopyOnWriteIntMap<V> implements IntMap<V> {
}
/** {@inheritDoc} */
- @Override public synchronized @Nullable V computeIfAbsent(int key,
IntFunction<? extends V> mappingFunction) {
+ @Override public @Nullable V computeIfAbsent(int key, IntFunction<?
extends V> mappingFunction) {
+ V val = get(key);
+
+ return val != null ? val : computeIfAbsentUnderLock(key,
mappingFunction);
+ }
+
+ /** Slow path of {@link #computeIfAbsent}: the mapping function runs under
the map lock. */
+ private synchronized V computeIfAbsentUnderLock(int key, IntFunction<?
extends V> mappingFunction) {
// The default implementation is a lookup followed by a put, the lock
makes the pair atomic.
return IntMap.super.computeIfAbsent(key, mappingFunction);
}
diff --git
a/modules/core/src/test/java/org/apache/ignite/internal/util/collection/CopyOnWriteIntMapTest.java
b/modules/core/src/test/java/org/apache/ignite/internal/util/collection/CopyOnWriteIntMapTest.java
index b001befcf28..642baf55cf5 100644
---
a/modules/core/src/test/java/org/apache/ignite/internal/util/collection/CopyOnWriteIntMapTest.java
+++
b/modules/core/src/test/java/org/apache/ignite/internal/util/collection/CopyOnWriteIntMapTest.java
@@ -78,6 +78,13 @@ public class CopyOnWriteIntMapTest {
assertEquals("one", map.computeIfAbsent(1, k -> "computed"));
assertEquals("2", map.computeIfAbsent(2, String::valueOf));
assertEquals("2", map.get(2));
+
+ assertNull(map.computeIfAbsent(3, k -> null));
+ assertFalse(map.containsKey(3));
+
+ map.put(3, null);
+
+ assertEquals("3", map.computeIfAbsent(3, String::valueOf));
}
/** */