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 ea4c2e3ad9b IGNITE-29082 Fixed repeated iterations over local 
partitions in cache metrics (#13624)
ea4c2e3ad9b is described below

commit ea4c2e3ad9b5b1641a9f7dc013b6133a7271ac9e
Author: Nikita Amelchev <[email protected]>
AuthorDate: Fri Oct 2 10:40:58 2026 +0300

    IGNITE-29082 Fixed repeated iterations over local partitions in cache 
metrics (#13624)
---
 docs/_docs/monitoring-metrics/new-metrics.adoc     |  2 +-
 .../java/org/apache/ignite/cache/CacheMetrics.java | 10 ++---
 .../processors/cache/CacheMetricsImpl.java         | 35 +++++++---------
 .../processors/cluster/CacheMetricsMessage.java    | 48 +++++++++++++++-------
 .../Apache.Ignite.Core/Cache/ICacheMetrics.cs      | 10 ++---
 5 files changed, 59 insertions(+), 46 deletions(-)

diff --git a/docs/_docs/monitoring-metrics/new-metrics.adoc 
b/docs/_docs/monitoring-metrics/new-metrics.adoc
index f7f7c3222d5..7482933ed43 100644
--- a/docs/_docs/monitoring-metrics/new-metrics.adoc
+++ b/docs/_docs/monitoring-metrics/new-metrics.adoc
@@ -108,7 +108,7 @@ Register name: `cache.{cache_name}.{near}`
 |CacheMisses | long | A miss is a get request that is not satisfied.
 |CachePuts | long | The total number of puts to the cache.
 |CacheRemovals | long | The total number of removals from the cache.
-|CacheSize | long | Local cache size.
+|CacheSize | long | Local cache size, the number of primary copies of entries 
on the node.
 |CacheTxCommits | long | Total number of transaction commits.
 |CacheTxRollbacks | long | Total number of transaction rollbacks.
 |CommitTime | histogram | Commit time in nanoseconds.
diff --git 
a/modules/core/src/main/java/org/apache/ignite/cache/CacheMetrics.java 
b/modules/core/src/main/java/org/apache/ignite/cache/CacheMetrics.java
index 5ede7786373..0ac911b8acb 100644
--- a/modules/core/src/main/java/org/apache/ignite/cache/CacheMetrics.java
+++ b/modules/core/src/main/java/org/apache/ignite/cache/CacheMetrics.java
@@ -317,25 +317,23 @@ public interface CacheMetrics {
     public long getOffHeapAllocatedSize();
 
     /**
-     * Gets number of non-{@code null} values in the cache.
-     * Note this method will always return {@code 0}
+     * Gets number of entries in the cache, the same as {@link 
#getCacheSize()}.
      *
-     * @return Number of non-{@code null} values in the cache.
+     * @return Number of entries in the cache.
      * @deprecated Can overflow. Use {@link CacheMetrics#getCacheSize()} 
instead.
      */
     @Deprecated
     public int getSize();
 
     /**
-     * Cache size.
+     * Cache size. Only primary copies of the cache entries are counted, 
backup copies are not.
      *
      * @return Cache size.
      */
     public long getCacheSize();
 
     /**
-     * Gets number of keys in the cache, possibly with {@code null} values.
-     * Note this method will always return {@code 0}
+     * Gets number of keys in the cache, the same as {@link #getCacheSize()}.
      *
      * @return Number of keys in the cache.
      * @deprecated Can overflow. Use {@link CacheMetrics#getCacheSize()} 
instead.
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheMetricsImpl.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheMetricsImpl.java
index 20f566a0733..59a3272cd9a 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheMetricsImpl.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheMetricsImpl.java
@@ -442,7 +442,8 @@ public class CacheMetricsImpl implements CacheMetrics {
 
         mreg.register("HeapEntriesCount", this::getHeapEntriesCount, "Onheap 
entries count.");
 
-        mreg.register("CacheSize", this::getCacheSize, "Local cache size.");
+        mreg.register("CacheSize", this::getCacheSize,
+            "Local cache size, the number of primary copies of entries on the 
node.");
 
         idxRebuildKeyProcessed = 
mreg.longAdderMetric("IndexRebuildKeyProcessed",
             "Number of keys processed during the index rebuilding.");
@@ -1390,9 +1391,6 @@ public class CacheMetricsImpl implements CacheMetrics {
         long offHeapPrimaryEntriesCnt = 0L;
         long offHeapBackupEntriesCnt = 0L;
         long heapEntriesCnt = 0L;
-        int size = 0;
-        long sizeLong = 0L;
-        boolean isEmpty;
 
         try {
             AffinityTopologyVersion topVer = 
cctx.affinity().affinityTopologyVersion();
@@ -1402,13 +1400,6 @@ public class CacheMetricsImpl implements CacheMetrics {
 
             final GridCacheAdapter<?, ?> cache = cctx.cache();
 
-            if (cache != null) {
-                offHeapEntriesCnt = cache.offHeapEntriesCount();
-
-                size = cache.localSize(null);
-                sizeLong = cache.localSizeLong(null);
-            }
-
             IntSet primaries = 
ImmutableIntSet.wrap(cctx.affinity().primaryPartitions(cctx.localNodeId(), 
topVer));
             IntSet backups = 
ImmutableIntSet.wrap(cctx.affinity().backupPartitions(cctx.localNodeId(), 
topVer));
 
@@ -1429,13 +1420,18 @@ public class CacheMetricsImpl implements CacheMetrics {
                 if (cache == null)
                     continue;
 
-                long cacheSize = part.dataStore().cacheSize(cctx.cacheId());
+                long offHeapCnt = part.dataStore().cacheSize(cctx.cacheId());
+
+                // All local partitions, not only primary and backup ones 
(e.g. renting too),
+                // the same as IgniteInternalCache#offHeapEntriesCount().
+                offHeapEntriesCnt += offHeapCnt;
 
                 if (primaries.contains(part.id()))
-                    offHeapPrimaryEntriesCnt += cacheSize;
+                    offHeapPrimaryEntriesCnt += offHeapCnt;
                 else if (backups.contains(part.id()))
-                    offHeapBackupEntriesCnt += cacheSize;
+                    offHeapBackupEntriesCnt += offHeapCnt;
 
+                // Onheap entries count.
                 heapEntriesCnt += part.publicSize(cctx.cacheId());
             }
         }
@@ -1443,18 +1439,17 @@ public class CacheMetricsImpl implements CacheMetrics {
             return unknownEntriesStat();
         }
 
-        isEmpty = (offHeapEntriesCnt == 0);
-
         EntriesStatMetrics stat = new EntriesStatMetrics();
 
         stat.offHeapEntriesCount(offHeapEntriesCnt);
         stat.offHeapPrimaryEntriesCount(offHeapPrimaryEntriesCnt);
         stat.offHeapBackupEntriesCount(offHeapBackupEntriesCnt);
         stat.heapEntriesCount(heapEntriesCnt);
-        stat.size(size);
-        stat.cacheSize(sizeLong);
-        stat.keySize(size);
-        stat.isEmpty(isEmpty);
+        // Local cache size counts primary copies of entries, see 
GridDistributedCacheAdapter#localSizeLong.
+        stat.size((int)offHeapPrimaryEntriesCnt);
+        stat.cacheSize(offHeapPrimaryEntriesCnt);
+        stat.keySize((int)offHeapPrimaryEntriesCnt);
+        stat.isEmpty(offHeapEntriesCnt == 0);
         stat.totalPartitionsCount(owningPartCnt + movingPartCnt);
         stat.rebalancingPartitionsCount(movingPartCnt);
 
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cluster/CacheMetricsMessage.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cluster/CacheMetricsMessage.java
index c700b639506..e41aa35ff24 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cluster/CacheMetricsMessage.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cluster/CacheMetricsMessage.java
@@ -20,6 +20,7 @@ package org.apache.ignite.internal.processors.cluster;
 import java.util.Collection;
 import org.apache.ignite.cache.CacheMetrics;
 import org.apache.ignite.internal.Order;
+import org.apache.ignite.internal.processors.cache.CacheMetricsImpl;
 import org.apache.ignite.internal.util.typedef.internal.S;
 import org.apache.ignite.plugin.extensions.communication.Message;
 import org.apache.ignite.plugin.extensions.communication.MessageFactory;
@@ -188,7 +189,7 @@ public class CacheMetricsMessage implements Message {
     @Order(35)
     public long offHeapAllocatedSize;
 
-    /** Number of non-{@code null} values in the cache. */
+    /** Number of entries in the cache. */
     @Order(36)
     public int size;
 
@@ -196,7 +197,7 @@ public class CacheMetricsMessage implements Message {
     @Order(37)
     public long cacheSize;
 
-    /** Number of keys in the cache, possibly with {@code null} values. */
+    /** Number of keys in the cache. */
     @Order(38)
     public int keySize;
 
@@ -441,17 +442,39 @@ public class CacheMetricsMessage implements Message {
         offHeapHits = m.getOffHeapHits();
         offHeapMisses = m.getOffHeapMisses();
 
-        offHeapEntriesCnt = m.getOffHeapEntriesCount();
-        heapEntriesCnt = m.getHeapEntriesCount();
-        offHeapPrimaryEntriesCnt = m.getOffHeapPrimaryEntriesCount();
-        offHeapBackupEntriesCnt = m.getOffHeapBackupEntriesCount();
+        if (m instanceof CacheMetricsImpl) {
+            // Each of the getters below iterates over all local partitions, 
collect them in one pass instead.
+            CacheMetricsImpl.EntriesStatMetrics entriesStat = 
((CacheMetricsImpl)m).getEntriesStat();
 
-        offHeapAllocatedSize = m.getOffHeapAllocatedSize();
+            offHeapEntriesCnt = entriesStat.offHeapEntriesCount();
+            heapEntriesCnt = entriesStat.heapEntriesCount();
+            offHeapPrimaryEntriesCnt = 
entriesStat.offHeapPrimaryEntriesCount();
+            offHeapBackupEntriesCnt = entriesStat.offHeapBackupEntriesCount();
+
+            cacheSize = entriesStat.cacheSize();
+            keySize = entriesStat.keySize();
+            size = entriesStat.size();
+            empty = entriesStat.isEmpty();
 
-        cacheSize = m.getCacheSize();
-        keySize = m.getKeySize();
-        size = m.getSize();
-        empty = m.isEmpty();
+            totalPartitionsCnt = entriesStat.totalPartitionsCount();
+            rebalancingPartitionsCnt = 
entriesStat.rebalancingPartitionsCount();
+        }
+        else {
+            offHeapEntriesCnt = m.getOffHeapEntriesCount();
+            heapEntriesCnt = m.getHeapEntriesCount();
+            offHeapPrimaryEntriesCnt = m.getOffHeapPrimaryEntriesCount();
+            offHeapBackupEntriesCnt = m.getOffHeapBackupEntriesCount();
+
+            cacheSize = m.getCacheSize();
+            keySize = m.getKeySize();
+            size = m.getSize();
+            empty = m.isEmpty();
+
+            totalPartitionsCnt = m.getTotalPartitionsCount();
+            rebalancingPartitionsCnt = m.getRebalancingPartitionsCount();
+        }
+
+        offHeapAllocatedSize = m.getOffHeapAllocatedSize();
 
         dhtEvictQueueCurrSize = m.getDhtEvictQueueCurrentSize();
         txThreadMapSize = m.getTxThreadMapSize();
@@ -488,9 +511,6 @@ public class CacheMetricsMessage implements Message {
         validForReading = m.isValidForReading();
         validForWriting = m.isValidForWriting();
 
-        totalPartitionsCnt = m.getTotalPartitionsCount();
-        rebalancingPartitionsCnt = m.getRebalancingPartitionsCount();
-
         rebalancedKeys = m.getRebalancedKeys();
         estimatedRebalancingKeys = m.getEstimatedRebalancingKeys();
         keysToRebalanceLeft = m.getKeysToRebalanceLeft();
diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Cache/ICacheMetrics.cs 
b/modules/platforms/dotnet/Apache.Ignite.Core/Cache/ICacheMetrics.cs
index 53be9efc0ad..ea8169f44d3 100644
--- a/modules/platforms/dotnet/Apache.Ignite.Core/Cache/ICacheMetrics.cs
+++ b/modules/platforms/dotnet/Apache.Ignite.Core/Cache/ICacheMetrics.cs
@@ -249,23 +249,23 @@ namespace Apache.Ignite.Core.Cache
         long OffHeapAllocatedSize { get; }
 
         /// <summary>
-        /// Gets number of non-null values in the cache.
+        /// Gets number of entries in the cache, the same as <see 
cref="CacheSize"/>.
         /// </summary>
         /// <returns>
-        /// Number of non-null values in the cache.
+        /// Number of entries in the cache.
         /// </returns>
         int Size { get; }
 
         /// <summary>
-        /// Gets number of non-null values in the cache.
+        /// Gets cache size. Only primary copies of the cache entries are 
counted, backup copies are not.
         /// </summary>
         /// <returns>
-        /// Number of non-null values in the cache.
+        /// Cache size.
         /// </returns>
         long CacheSize { get; }
 
         /// <summary>
-        /// Gets number of keys in the cache, possibly with null values.
+        /// Gets number of keys in the cache, the same as <see 
cref="CacheSize"/>.
         /// </summary>
         /// <returns>
         /// Number of keys in the cache.

Reply via email to