From 8e4b077101ffbd076583b9467d94d9ee05702792 Mon Sep 17 00:00:00 2001 From: jabolina Date: Tue, 13 Sep 2022 15:30:03 -0300 Subject: [PATCH] ISPN-14129 Memory used by a node and cache in the distribution endpoint --- .../json/rest_full_cache_data_distribution.json | 6 ++++-- .../src/main/asciidoc/topics/ref_rest_caches.adoc | 1 + server/rest/proto.lock | 11 +++++++++++ .../rest/distribution/CacheDistributionInfo.java | 15 ++++++++++++--- .../rest/resources/CacheResourceV2Test.java | 13 ++++++++++++- 5 files changed, 40 insertions(+), 6 deletions(-) diff --git a/documentation/src/main/asciidoc/topics/json/rest_full_cache_data_distribution.json b/documentation/src/main/asciidoc/topics/json/rest_full_cache_data_distribution.json index 7f19184e343f..59a0e7bcc600 100644 --- a/documentation/src/main/asciidoc/topics/json/rest_full_cache_data_distribution.json +++ b/documentation/src/main/asciidoc/topics/json/rest_full_cache_data_distribution.json @@ -5,7 +5,8 @@ "127.0.0.1:44175" ], "memory_entries": 0, - "total_entries": 0 + "total_entries": 0, + "memory_used": 528512 }, { "node_name":"NodeB", @@ -13,6 +14,7 @@ "127.0.0.1:44187" ], "memory_entries": 0, - "total_entries": 0 + "total_entries": 0, + "memory_used": 528512 } ] \ No newline at end of file diff --git a/documentation/src/main/asciidoc/topics/ref_rest_caches.adoc b/documentation/src/main/asciidoc/topics/ref_rest_caches.adoc index 75f50ebcde15..256e0cc45d5f 100644 --- a/documentation/src/main/asciidoc/topics/ref_rest_caches.adoc +++ b/documentation/src/main/asciidoc/topics/ref_rest_caches.adoc @@ -177,6 +177,7 @@ Each element in the list represents a node. The properties are: * `node_addresses` is a list with all the node's physical addresses. * `memory_entries` the number of entries the node holds in memory belonging to the cache. * `total_entries` the number of entries the node has in memory and disk belonging to the cache. +* `memory_used` the value in bytes the eviction algorithm estimates the cache occupies. Returns -1 if eviction is not enabled. [id='rest_v2_cache_mutable_attributes'] = Retrieving all mutable cache configuration attributes diff --git a/server/rest/proto.lock b/server/rest/proto.lock index 3133fecb6b98..d04eb6f8dd44 100644 --- a/server/rest/proto.lock +++ b/server/rest/proto.lock @@ -77,6 +77,17 @@ "value": "0" } ] + }, + { + "id": 5, + "name": "memoryUsed", + "type": "int64", + "options": [ + { + "name": "default", + "value": "0" + } + ] } ] }, diff --git a/server/rest/src/main/java/org/infinispan/rest/distribution/CacheDistributionInfo.java b/server/rest/src/main/java/org/infinispan/rest/distribution/CacheDistributionInfo.java index c963bbcdeccb..ac2e891accfe 100644 --- a/server/rest/src/main/java/org/infinispan/rest/distribution/CacheDistributionInfo.java +++ b/server/rest/src/main/java/org/infinispan/rest/distribution/CacheDistributionInfo.java @@ -20,13 +20,15 @@ public class CacheDistributionInfo implements JsonSerialization, NodeDataDistrib private final List addresses; private final long memoryEntries; private final long totalEntries; + private final long memoryUsed; @ProtoFactory - public CacheDistributionInfo(String name, List addresses, long memoryEntries, long totalEntries) { + public CacheDistributionInfo(String name, List addresses, long memoryEntries, long totalEntries, long memoryUsed) { this.name = name; this.addresses = addresses; this.memoryEntries = memoryEntries; this.totalEntries = totalEntries; + this.memoryUsed = memoryUsed; } @ProtoField(1) @@ -51,13 +53,19 @@ public long totalEntries() { return totalEntries; } + @ProtoField(value = 5, defaultValue = "0") + public long memoryUsed() { + return memoryUsed; + } + @Override public Json toJson() { return Json.object() .set("node_name", name) .set("node_addresses", Json.array(addresses.toArray())) .set("memory_entries", memoryEntries) - .set("total_entries", totalEntries); + .set("total_entries", totalEntries) + .set("memory_used", memoryUsed); } public static CacheDistributionInfo resolve(AdvancedCache cache) { @@ -65,6 +73,7 @@ public static CacheDistributionInfo resolve(AdvancedCache cache) { final CacheManagerInfo manager = cache.getCacheManager().getCacheManagerInfo(); long inMemory = stats.getApproximateEntriesInMemory(); long total = stats.getApproximateEntries(); - return new CacheDistributionInfo(manager.getNodeName(), manager.getPhysicalAddressesRaw(), inMemory, total); + return new CacheDistributionInfo(manager.getNodeName(), manager.getPhysicalAddressesRaw(), inMemory, total, + stats.getDataMemoryUsed()); } } diff --git a/server/rest/src/test/java/org/infinispan/rest/resources/CacheResourceV2Test.java b/server/rest/src/test/java/org/infinispan/rest/resources/CacheResourceV2Test.java index 987b2e4da965..c1129b28fb17 100644 --- a/server/rest/src/test/java/org/infinispan/rest/resources/CacheResourceV2Test.java +++ b/server/rest/src/test/java/org/infinispan/rest/resources/CacheResourceV2Test.java @@ -422,7 +422,8 @@ public void testCacheV2Stats() { @Test public void testCacheV2Distribution() { - String cacheJson = "{ \"distributed-cache\" : { \"statistics\":true } }"; + String cacheJson = "{ \"distributed-cache\" : { \"statistics\":true, \"memory\" : {" + + "\"storage\": \"OFF_HEAP\", \"max-size\": \"1MB\" } } }"; RestCacheClient cacheClient = client.cache("distributionCache"); RestEntity jsonEntity = RestEntity.create(APPLICATION_JSON, cacheJson); @@ -441,11 +442,15 @@ public void testCacheV2Distribution() { assertEquals(NUM_SERVERS, jsons.size()); Pattern pattern = Pattern.compile(this.getClass().getSimpleName() + "-Node[a-zA-Z]$"); + Map previousSizes = new HashMap<>(); for (Json node : jsons) { assertEquals(node.at("memory_entries").asInteger(), 2); assertEquals(node.at("total_entries").asInteger(), 2); assertEquals(node.at("node_addresses").asJsonList().size(), 1); assertTrue(pattern.matcher(node.at("node_name").asString()).matches()); + assertTrue(node.at("memory_used").asLong() > 0); + + previousSizes.put(node.at("node_name").asString(), node.at("memory_used").asLong()); } response = cacheClient.clear(); @@ -461,6 +466,12 @@ public void testCacheV2Distribution() { for (Json node : jsons) { assertEquals(node.at("memory_entries").asInteger(), 0); assertEquals(node.at("total_entries").asInteger(), 0); + + // Even though the cache was cleared, it still occupies some space. + assertTrue(node.at("memory_used").asLong() > 0); + + // But less space than before. + assertTrue(node.at("memory_used").asLong() < previousSizes.get(node.at("node_name").asString())); } }