You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by da...@apache.org on 2026/03/12 13:19:03 UTC

(doris) branch feature/cloud-replica-memory-opt created (now 0d10541ed9a)

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

dataroaring pushed a change to branch feature/cloud-replica-memory-opt
in repository https://gitbox.apache.org/repos/asf/doris.git


      at 0d10541ed9a [opt](memory) Intern cluster ID strings in CloudReplica

This branch includes the following new commits:

     new 7f44a9d89ff [opt](memory) Lazy-init ConcurrentHashMaps in CloudReplica
     new 0d10541ed9a [opt](memory) Intern cluster ID strings in CloudReplica

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


(doris) 02/02: [opt](memory) Intern cluster ID strings in CloudReplica

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

dataroaring pushed a commit to branch feature/cloud-replica-memory-opt
in repository https://gitbox.apache.org/repos/asf/doris.git

commit 0d10541ed9a4cb873c33e8f46085271a14f361ca
Author: Yongqiang YANG <da...@gmail.com>
AuthorDate: Thu Mar 12 06:18:16 2026 -0700

    [opt](memory) Intern cluster ID strings in CloudReplica
    
    Add a static intern pool for cluster ID strings to eliminate millions
    of duplicate String instances across CloudReplica objects. Each replica
    stores cluster IDs as map keys; without interning, Gson deserialization
    creates a separate String instance per replica (~40-70 bytes each).
    
    - Add ConcurrentHashMap-based intern pool (heap-safe, unlike String.intern)
    - Intern strings at all write points (updateClusterToPrimaryBe/Secondary)
    - Intern keys during gsonPostProcess for deserialized maps
    - For 1M replicas with 3 clusters: saves ~40-70 MB of duplicate Strings
    
    Co-Authored-By: Claude Opus 4.6 <no...@anthropic.com>
---
 .../apache/doris/cloud/catalog/CloudReplica.java   | 27 +++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)

diff --git a/fe/fe-core/src/main/java/org/apache/doris/cloud/catalog/CloudReplica.java b/fe/fe-core/src/main/java/org/apache/doris/cloud/catalog/CloudReplica.java
index b5f119cc803..6948ef2f49c 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/cloud/catalog/CloudReplica.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/cloud/catalog/CloudReplica.java
@@ -73,6 +73,18 @@ public class CloudReplica extends Replica implements GsonPostProcessable {
 
     private static final Random rand = new Random();
 
+    // Intern pool for cluster ID strings to avoid millions of duplicate String instances.
+    // Typically only a handful of distinct cluster IDs exist in the system.
+    private static final ConcurrentHashMap<String, String> CLUSTER_ID_POOL = new ConcurrentHashMap<>();
+
+    private static String internClusterId(String clusterId) {
+        if (clusterId == null) {
+            return null;
+        }
+        String existing = CLUSTER_ID_POOL.putIfAbsent(clusterId, clusterId);
+        return existing != null ? existing : clusterId;
+    }
+
     private Map<String, List<Long>> memClusterToBackends = null;
 
     // clusterId, secondaryBe, changeTimestamp
@@ -608,7 +620,7 @@ public class CloudReplica extends Replica implements GsonPostProcessable {
     }
 
     public void updateClusterToPrimaryBe(String cluster, long beId) {
-        getOrCreatePrimaryMap().put(cluster, beId);
+        getOrCreatePrimaryMap().put(internClusterId(cluster), beId);
         Map<String, Pair<Long, Long>> secMap = secondaryClusterToBackends;
         if (secMap != null) {
             secMap.remove(cluster);
@@ -626,7 +638,7 @@ public class CloudReplica extends Replica implements GsonPostProcessable {
             LOG.debug("add to secondary clusterId {}, beId {}, changeTimestamp {}, replica info {}",
                     cluster, beId, changeTimestamp, this);
         }
-        getOrCreateSecondaryMap().put(cluster, Pair.of(beId, changeTimestamp));
+        getOrCreateSecondaryMap().put(internClusterId(cluster), Pair.of(beId, changeTimestamp));
     }
 
     public void clearClusterToBe(String cluster) {
@@ -709,10 +721,19 @@ public class CloudReplica extends Replica implements GsonPostProcessable {
                 String clusterId = entry.getKey();
                 List<Long> beIds = entry.getValue();
                 if (beIds != null && !beIds.isEmpty()) {
-                    map.put(clusterId, beIds.get(0));
+                    map.put(internClusterId(clusterId), beIds.get(0));
                 }
             }
             this.primaryClusterToBackends = null;
         }
+        // Intern cluster ID keys in deserialized primary map to share String instances
+        // across millions of CloudReplica objects
+        if (primaryClusterToBackend != null) {
+            ConcurrentHashMap<String, Long> interned = new ConcurrentHashMap<>(primaryClusterToBackend.size());
+            for (Map.Entry<String, Long> entry : primaryClusterToBackend.entrySet()) {
+                interned.put(internClusterId(entry.getKey()), entry.getValue());
+            }
+            primaryClusterToBackend = interned;
+        }
     }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


(doris) 01/02: [opt](memory) Lazy-init ConcurrentHashMaps in CloudReplica

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

dataroaring pushed a commit to branch feature/cloud-replica-memory-opt
in repository https://gitbox.apache.org/repos/asf/doris.git

commit 7f44a9d89fff117b4455ad7332007697422f2d1a
Author: Yongqiang YANG <da...@gmail.com>
AuthorDate: Thu Mar 12 06:06:46 2026 -0700

    [opt](memory) Lazy-init ConcurrentHashMaps in CloudReplica
    
    Replace eager ConcurrentHashMap initialization with null defaults and
    lazy allocation via double-checked locking. For millions of CloudReplica
    instances, this saves ~120 bytes per replica when maps are empty.
    
    - primaryClusterToBackend: null by default, allocated on first put
    - secondaryClusterToBackends: null by default, allocated on first put
    - Add volatile for thread-safe lazy initialization
    - Add null-safe access patterns throughout all methods
    - Use initial capacity 2 (vs default 16) for small map optimization
    
    Co-Authored-By: Claude Opus 4.6 <no...@anthropic.com>
---
 .../apache/doris/cloud/catalog/CloudReplica.java   | 92 +++++++++++++++++-----
 1 file changed, 71 insertions(+), 21 deletions(-)

diff --git a/fe/fe-core/src/main/java/org/apache/doris/cloud/catalog/CloudReplica.java b/fe/fe-core/src/main/java/org/apache/doris/cloud/catalog/CloudReplica.java
index afd2fd2501f..b5f119cc803 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/cloud/catalog/CloudReplica.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/cloud/catalog/CloudReplica.java
@@ -56,7 +56,7 @@ public class CloudReplica extends Replica implements GsonPostProcessable {
     @SerializedName(value = "bes")
     private ConcurrentHashMap<String, List<Long>> primaryClusterToBackends = null;
     @SerializedName(value = "be")
-    private ConcurrentHashMap<String, Long> primaryClusterToBackend = new ConcurrentHashMap<>();
+    private volatile ConcurrentHashMap<String, Long> primaryClusterToBackend;
     @SerializedName(value = "dbId")
     private long dbId = -1;
     @SerializedName(value = "tableId")
@@ -76,8 +76,7 @@ public class CloudReplica extends Replica implements GsonPostProcessable {
     private Map<String, List<Long>> memClusterToBackends = null;
 
     // clusterId, secondaryBe, changeTimestamp
-    private Map<String, Pair<Long, Long>> secondaryClusterToBackends
-            = new ConcurrentHashMap<String, Pair<Long, Long>>();
+    private volatile Map<String, Pair<Long, Long>> secondaryClusterToBackends;
 
     public CloudReplica() {
     }
@@ -99,6 +98,34 @@ public class CloudReplica extends Replica implements GsonPostProcessable {
         this.idx = idx;
     }
 
+    private ConcurrentHashMap<String, Long> getOrCreatePrimaryMap() {
+        ConcurrentHashMap<String, Long> map = primaryClusterToBackend;
+        if (map == null) {
+            synchronized (this) {
+                map = primaryClusterToBackend;
+                if (map == null) {
+                    map = new ConcurrentHashMap<>(2);
+                    primaryClusterToBackend = map;
+                }
+            }
+        }
+        return map;
+    }
+
+    private Map<String, Pair<Long, Long>> getOrCreateSecondaryMap() {
+        Map<String, Pair<Long, Long>> map = secondaryClusterToBackends;
+        if (map == null) {
+            synchronized (this) {
+                map = secondaryClusterToBackends;
+                if (map == null) {
+                    map = new ConcurrentHashMap<>(2);
+                    secondaryClusterToBackends = map;
+                }
+            }
+        }
+        return map;
+    }
+
     private boolean isColocated() {
         return Env.getCurrentColocateIndex().isColocateTableNoLock(tableId);
     }
@@ -206,7 +233,8 @@ public class CloudReplica extends Replica implements GsonPostProcessable {
             }
         }
 
-        return primaryClusterToBackend.getOrDefault(clusterId, -1L);
+        ConcurrentHashMap<String, Long> map = primaryClusterToBackend;
+        return map != null ? map.getOrDefault(clusterId, -1L) : -1L;
     }
 
     private String getCurrentClusterId() throws ComputeGroupException {
@@ -316,8 +344,9 @@ public class CloudReplica extends Replica implements GsonPostProcessable {
                 backendId = memClusterToBackends.get(clusterId).get(indexRand);
             }
 
-            if (!replicaEnough && !allowColdRead && primaryClusterToBackend.containsKey(clusterId)) {
-                backendId = primaryClusterToBackend.get(clusterId);
+            ConcurrentHashMap<String, Long> priMap = primaryClusterToBackend;
+            if (!replicaEnough && !allowColdRead && priMap != null && priMap.containsKey(clusterId)) {
+                backendId = priMap.get(clusterId);
             }
 
             if (backendId > 0) {
@@ -393,7 +422,11 @@ public class CloudReplica extends Replica implements GsonPostProcessable {
     }
 
     public Backend getSecondaryBackend(String clusterId) {
-        Pair<Long, Long> secondBeAndChangeTimestamp = secondaryClusterToBackends.get(clusterId);
+        Map<String, Pair<Long, Long>> secMap = secondaryClusterToBackends;
+        if (secMap == null) {
+            return null;
+        }
+        Pair<Long, Long> secondBeAndChangeTimestamp = secMap.get(clusterId);
         if (secondBeAndChangeTimestamp == null) {
             return null;
         }
@@ -575,8 +608,11 @@ public class CloudReplica extends Replica implements GsonPostProcessable {
     }
 
     public void updateClusterToPrimaryBe(String cluster, long beId) {
-        primaryClusterToBackend.put(cluster, beId);
-        secondaryClusterToBackends.remove(cluster);
+        getOrCreatePrimaryMap().put(cluster, beId);
+        Map<String, Pair<Long, Long>> secMap = secondaryClusterToBackends;
+        if (secMap != null) {
+            secMap.remove(cluster);
+        }
     }
 
     /**
@@ -590,19 +626,29 @@ public class CloudReplica extends Replica implements GsonPostProcessable {
             LOG.debug("add to secondary clusterId {}, beId {}, changeTimestamp {}, replica info {}",
                     cluster, beId, changeTimestamp, this);
         }
-        secondaryClusterToBackends.put(cluster, Pair.of(beId, changeTimestamp));
+        getOrCreateSecondaryMap().put(cluster, Pair.of(beId, changeTimestamp));
     }
 
     public void clearClusterToBe(String cluster) {
-        primaryClusterToBackend.remove(cluster);
-        secondaryClusterToBackends.remove(cluster);
+        ConcurrentHashMap<String, Long> priMap = primaryClusterToBackend;
+        if (priMap != null) {
+            priMap.remove(cluster);
+        }
+        Map<String, Pair<Long, Long>> secMap = secondaryClusterToBackends;
+        if (secMap != null) {
+            secMap.remove(cluster);
+        }
     }
 
     // ATTN: This func is only used by redundant tablet report clean in bes.
     // Only the master node will do the diff logic,
     // so just only need to clean up secondaryClusterToBackends on the master node.
     public void checkAndClearSecondaryClusterToBe(String clusterId, long expireTimestamp) {
-        Pair<Long, Long> secondBeAndChangeTimestamp = secondaryClusterToBackends.get(clusterId);
+        Map<String, Pair<Long, Long>> secMap = secondaryClusterToBackends;
+        if (secMap == null) {
+            return;
+        }
+        Pair<Long, Long> secondBeAndChangeTimestamp = secMap.get(clusterId);
         if (secondBeAndChangeTimestamp == null) {
             return;
         }
@@ -612,19 +658,22 @@ public class CloudReplica extends Replica implements GsonPostProcessable {
         if (changeTimestamp < expireTimestamp) {
             LOG.debug("remove clusterId {} secondary beId {} changeTimestamp {} expireTimestamp {} replica info {}",
                     clusterId, beId, changeTimestamp, expireTimestamp, this);
-            secondaryClusterToBackends.remove(clusterId);
+            secMap.remove(clusterId);
             return;
         }
     }
 
     public List<Backend> getAllPrimaryBes() {
         List<Backend> result = new ArrayList<Backend>();
-        primaryClusterToBackend.forEach((clusterId, beId) -> {
-            if (beId != -1) {
-                Backend backend = Env.getCurrentSystemInfo().getBackend(beId);
-                result.add(backend);
-            }
-        });
+        ConcurrentHashMap<String, Long> map = primaryClusterToBackend;
+        if (map != null) {
+            map.forEach((clusterId, beId) -> {
+                if (beId != -1) {
+                    Backend backend = Env.getCurrentSystemInfo().getBackend(beId);
+                    result.add(backend);
+                }
+            });
+        }
         return result;
     }
 
@@ -655,11 +704,12 @@ public class CloudReplica extends Replica implements GsonPostProcessable {
                     this.getId(), this.primaryClusterToBackends, this.primaryClusterToBackend);
         }
         if (primaryClusterToBackends != null) {
+            ConcurrentHashMap<String, Long> map = getOrCreatePrimaryMap();
             for (Map.Entry<String, List<Long>> entry : primaryClusterToBackends.entrySet()) {
                 String clusterId = entry.getKey();
                 List<Long> beIds = entry.getValue();
                 if (beIds != null && !beIds.isEmpty()) {
-                    primaryClusterToBackend.put(clusterId, beIds.get(0));
+                    map.put(clusterId, beIds.get(0));
                 }
             }
             this.primaryClusterToBackends = null;


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org