You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@bookkeeper.apache.org by ay...@apache.org on 2022/03/10 00:02:08 UTC

[bookkeeper] branch master updated: Add sizeInBytes interface for ConcurrentLong map and set

This is an automated email from the ASF dual-hosted git repository.

ayegorov pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/bookkeeper.git


The following commit(s) were added to refs/heads/master by this push:
     new df92e0c  Add sizeInBytes interface for ConcurrentLong map and set
df92e0c is described below

commit df92e0c6bb5c5f7117d7b1dce9cd7a110e142581
Author: Hang Chen <ch...@apache.org>
AuthorDate: Thu Mar 10 08:02:00 2022 +0800

    Add sizeInBytes interface for ConcurrentLong map and set
    
    ### Motivation
    We provide some concurrent maps and sets for specific usage, and provide size() and capacity() interface for user to get the real item number and the max item number.
    
    However, if user want to monitor how much memory those current maps and set allocated, there is not interface to expose this metric.
    
    ### Changes
    Add `sizeInBytes()` interface to expose the memory size has been allocated for those concurrent maps and sets.
    
    
    
    Reviewers: Enrico Olivelli <eo...@gmail.com>, Nicolò Boschi <bo...@gmail.com>
    
    This closes #3068 from hangc0276/chenhang/add_sizeInBytes_interface_for_concurrent_maps
---
 .../util/collections/ConcurrentLongHashSet.java      |  8 ++++++++
 .../util/collections/ConcurrentLongLongHashMap.java  |  8 ++++++++
 .../collections/ConcurrentLongLongPairHashMap.java   |  8 ++++++++
 .../util/collections/ConcurrentLongHashSetTest.java  | 20 ++++++++++++++++++++
 .../collections/ConcurrentLongLongHashMapTest.java   | 20 ++++++++++++++++++++
 .../ConcurrentLongLongPairHashMapTest.java           | 20 ++++++++++++++++++++
 6 files changed, 84 insertions(+)

diff --git a/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/collections/ConcurrentLongHashSet.java b/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/collections/ConcurrentLongHashSet.java
index 5627f8b..8243f3e 100644
--- a/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/collections/ConcurrentLongHashSet.java
+++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/collections/ConcurrentLongHashSet.java
@@ -167,6 +167,14 @@ public class ConcurrentLongHashSet {
         return size;
     }
 
+    public long sizeInBytes() {
+        long size = 0;
+        for (Section s : sections) {
+            size += (long) s.table.length * Long.BYTES;
+        }
+        return size;
+    }
+
     public long capacity() {
         long capacity = 0;
         for (Section s : sections) {
diff --git a/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/collections/ConcurrentLongLongHashMap.java b/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/collections/ConcurrentLongLongHashMap.java
index 2e773b2..ec1d232 100644
--- a/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/collections/ConcurrentLongLongHashMap.java
+++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/collections/ConcurrentLongLongHashMap.java
@@ -185,6 +185,14 @@ public class ConcurrentLongLongHashMap {
         return size;
     }
 
+    public long sizeInBytes() {
+        long size = 0;
+        for (Section s : sections) {
+            size += (long) s.table.length * Long.BYTES;
+        }
+        return size;
+    }
+
     public long capacity() {
         long capacity = 0;
         for (Section s : sections) {
diff --git a/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/collections/ConcurrentLongLongPairHashMap.java b/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/collections/ConcurrentLongLongPairHashMap.java
index 2aee488..a02e51f 100644
--- a/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/collections/ConcurrentLongLongPairHashMap.java
+++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/collections/ConcurrentLongLongPairHashMap.java
@@ -186,6 +186,14 @@ public class ConcurrentLongLongPairHashMap {
         return size;
     }
 
+    public long sizeInBytes() {
+        long size = 0;
+        for (Section s : sections) {
+            size += (long) s.table.length * Long.BYTES;
+        }
+        return size;
+    }
+
     public long capacity() {
         long capacity = 0;
         for (Section s : sections) {
diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/util/collections/ConcurrentLongHashSetTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/util/collections/ConcurrentLongHashSetTest.java
index cce7144..8fa54e4 100644
--- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/util/collections/ConcurrentLongHashSetTest.java
+++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/util/collections/ConcurrentLongHashSetTest.java
@@ -340,4 +340,24 @@ public class ConcurrentLongHashSetTest {
         assertTrue(set.isEmpty());
     }
 
+    @Test
+    public void testSizeInBytes() {
+        ConcurrentLongHashSet set = new ConcurrentLongHashSet(4, 2);
+        assertEquals(64, set.sizeInBytes());
+        set.add(1);
+        assertEquals(64, set.sizeInBytes());
+        set.add(2);
+        assertEquals(64, set.sizeInBytes());
+        set.add(3);
+        assertEquals(64, set.sizeInBytes());
+        set.add(4);
+        assertEquals(96, set.sizeInBytes());
+        set.add(5);
+        assertEquals(96, set.sizeInBytes());
+        set.add(6);
+        assertEquals(128, set.sizeInBytes());
+        set.add(7);
+        assertEquals(128, set.sizeInBytes());
+    }
+
 }
diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/util/collections/ConcurrentLongLongHashMapTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/util/collections/ConcurrentLongLongHashMapTest.java
index 100a1e1..393da29 100644
--- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/util/collections/ConcurrentLongLongHashMapTest.java
+++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/util/collections/ConcurrentLongLongHashMapTest.java
@@ -565,4 +565,24 @@ public class ConcurrentLongLongHashMapTest {
 
         assertEquals(map, lmap.asMap());
     }
+
+    @Test
+    public void testSizeInBytes() {
+        ConcurrentLongLongHashMap lmap = new ConcurrentLongLongHashMap(4, 2);
+        assertEquals(128, lmap.sizeInBytes());
+        lmap.put(1, 1);
+        assertEquals(128, lmap.sizeInBytes());
+        lmap.put(2, 2);
+        assertEquals(128, lmap.sizeInBytes());
+        lmap.put(3, 3);
+        assertEquals(128, lmap.sizeInBytes());
+        lmap.put(4, 4);
+        assertEquals(192, lmap.sizeInBytes());
+        lmap.put(5, 5);
+        assertEquals(192, lmap.sizeInBytes());
+        lmap.put(6, 6);
+        assertEquals(256, lmap.sizeInBytes());
+        lmap.put(7, 7);
+        assertEquals(256, lmap.sizeInBytes());
+    }
 }
diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/util/collections/ConcurrentLongLongPairHashMapTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/util/collections/ConcurrentLongLongPairHashMapTest.java
index b56bd31..7c4cdcb 100644
--- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/util/collections/ConcurrentLongLongPairHashMapTest.java
+++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/util/collections/ConcurrentLongLongPairHashMapTest.java
@@ -427,4 +427,24 @@ public class ConcurrentLongLongPairHashMapTest {
 
         assertEquals(map, lmap.asMap());
     }
+
+    @Test
+    public void testSizeInBytes() {
+        ConcurrentLongLongPairHashMap lmap = new ConcurrentLongLongPairHashMap(4, 2);
+        assertEquals(256, lmap.sizeInBytes());
+        lmap.put(1, 1, 1, 1);
+        assertEquals(256, lmap.sizeInBytes());
+        lmap.put(2, 2, 2, 2);
+        assertEquals(256, lmap.sizeInBytes());
+        lmap.put(3, 3, 3, 3);
+        assertEquals(256, lmap.sizeInBytes());
+        lmap.put(4, 4, 4, 4);
+        assertEquals(256, lmap.sizeInBytes());
+        lmap.put(5, 5, 5, 5);
+        assertEquals(384, lmap.sizeInBytes());
+        lmap.put(6, 6, 6, 6);
+        assertEquals(512, lmap.sizeInBytes());
+        lmap.put(7, 7, 7, 7);
+        assertEquals(512, lmap.sizeInBytes());
+    }
 }