This is an automated email from the ASF dual-hosted git repository. dataroaring pushed a commit to branch opt/cloud-replica-central-index in repository https://gitbox.apache.org/repos/asf/doris.git
commit 0053632b0d29df236c36000172f33a60e7f037bf Author: Yongqiang YANG <[email protected]> AuthorDate: Mon Mar 30 21:27:09 2026 -0700 [opt](cloud) Reduce CloudReplica memory by centralizing cluster-to-BE mapping Each CloudReplica stored two ConcurrentHashMap instances (primaryClusterToBackend and secondaryClusterToBackends) costing ~384 bytes each even when empty/single-entry. At 10M tablets this wastes ~3.8 GB. Centralize these mappings in CloudTabletInvertedIndex using ConcurrentLong2LongHashMap (primitive long→long, no boxing), cutting per-replica overhead to near zero. Backward-compatible: checkpoint JSON format ("be": {"cluster1": 10001}) is preserved using GsonPreProcessable/GsonPostProcessable hooks. Safe for upgrade, downgrade, and rolling upgrade. Co-Authored-By: Claude Opus 4.6 <[email protected]> --- .../apache/doris/cloud/catalog/CloudReplica.java | 123 ++++++++-- .../cloud/catalog/CloudTabletInvertedIndex.java | 116 +++++++++- .../catalog/CloudReplicaCentralIndexTest.java | 255 +++++++++++++++++++++ 3 files changed, 468 insertions(+), 26 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 22f2464e48d..bb88e4ae5a7 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 @@ -26,9 +26,9 @@ import org.apache.doris.cloud.qe.ComputeGroupException; import org.apache.doris.cloud.system.CloudSystemInfoService; import org.apache.doris.common.Config; import org.apache.doris.common.DdlException; -import org.apache.doris.common.Pair; import org.apache.doris.common.util.DebugPointUtil; import org.apache.doris.persist.gson.GsonPostProcessable; +import org.apache.doris.persist.gson.GsonPreProcessable; import org.apache.doris.qe.ConnectContext; import org.apache.doris.system.Backend; @@ -44,21 +44,26 @@ import org.apache.logging.log4j.Logger; import java.io.IOException; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Random; import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; -public class CloudReplica extends Replica implements GsonPostProcessable { +public class CloudReplica extends Replica implements GsonPostProcessable, GsonPreProcessable { private static final Logger LOG = LogManager.getLogger(CloudReplica.class); // a replica is mapped to one BE in a cluster, use primaryClusterToBackend instead of primaryClusterToBackends @Deprecated @SerializedName(value = "bes") private ConcurrentHashMap<String, List<Long>> primaryClusterToBackends = null; + // Serialize-only: populated by gsonPreProcess() before checkpoint, nulled after gsonPostProcess(). + // TODO: can be removed once downgrade to pre-optimization version is no longer needed. @SerializedName(value = "be") - private ConcurrentHashMap<String, Long> primaryClusterToBackend = new ConcurrentHashMap<>(); + private HashMap<String, Long> primaryClusterToBackend = null; + @SerializedName(value = "dbId") + private long dbId = -1; @SerializedName(value = "tableId") private long tableId = -1; @SerializedName(value = "partitionId") @@ -84,9 +89,9 @@ public class CloudReplica extends Replica implements GsonPostProcessable { private static final Random rand = new Random(); - // clusterId, secondaryBe, changeTimestamp - private Map<String, Pair<Long, Long>> secondaryClusterToBackends - = new ConcurrentHashMap<String, Pair<Long, Long>>(); + private long segmentCount = 0L; + + private long rowsetCount = 1L; // [0-1] rowset public CloudReplica() { } @@ -106,6 +111,10 @@ public class CloudReplica extends Replica implements GsonPostProcessable { this.idx = idx; } + private CloudTabletInvertedIndex getCloudInvertedIndex() { + return (CloudTabletInvertedIndex) Env.getCurrentInvertedIndex(); + } + private boolean isColocated() { return Env.getCurrentColocateIndex().isColocateTableNoLock(tableId); } @@ -213,7 +222,7 @@ public class CloudReplica extends Replica implements GsonPostProcessable { } } - return primaryClusterToBackend.getOrDefault(clusterId, -1L); + return getCloudInvertedIndex().getPrimaryBeId(clusterId, getId()); } private String getCurrentClusterId() throws ComputeGroupException { @@ -312,6 +321,26 @@ public class CloudReplica extends Replica implements GsonPostProcessable { if (Config.enable_cloud_multi_replica) { int indexRand = rand.nextInt(Config.cloud_replica_num); + int coldReadRand = rand.nextInt(100); + boolean allowColdRead = coldReadRand < Config.cloud_cold_read_percent; + + long backendId = -1; + if (!allowColdRead) { + long primaryBe = getCloudInvertedIndex().getPrimaryBeId(clusterId, getId()); + if (primaryBe != -1L) { + backendId = primaryBe; + } + } + + if (backendId > 0) { + Backend be = Env.getCurrentSystemInfo().getBackend(backendId); + if (be != null && be.isQueryAvailable()) { + if (LOG.isDebugEnabled()) { + LOG.debug("backendId={} ", backendId); + } + return backendId; + } + } List<Long> res = hashReplicaToBes(clusterId, false, Config.cloud_replica_num); if (res.size() < indexRand + 1) { @@ -376,17 +405,17 @@ public class CloudReplica extends Replica implements GsonPostProcessable { } public Backend getSecondaryBackend(String clusterId) { - Pair<Long, Long> secondBeAndChangeTimestamp = secondaryClusterToBackends.get(clusterId); - if (secondBeAndChangeTimestamp == null) { + CloudTabletInvertedIndex idx = getCloudInvertedIndex(); + long beId = idx.getSecondaryBeId(clusterId, getId()); + if (beId == -1L) { return null; } - long beId = secondBeAndChangeTimestamp.key(); - long changeTimestamp = secondBeAndChangeTimestamp.value(); + long changeTimestamp = idx.getSecondaryTimestamp(clusterId, getId()); if (LOG.isDebugEnabled()) { LOG.debug("in secondaryClusterToBackends clusterId {}, beId {}, changeTimestamp {}, replica info {}", clusterId, beId, changeTimestamp, this); } - return Env.getCurrentSystemInfo().getBackend(secondBeAndChangeTimestamp.first); + return Env.getCurrentSystemInfo().getBackend(beId); } public long hashReplicaToBe(String clusterId, boolean isBackGround) throws ComputeGroupException { @@ -536,8 +565,9 @@ public class CloudReplica extends Replica implements GsonPostProcessable { } public void updateClusterToPrimaryBe(String cluster, long beId) { - primaryClusterToBackend.put(cluster, beId); - secondaryClusterToBackends.remove(cluster); + CloudTabletInvertedIndex idx = getCloudInvertedIndex(); + idx.setPrimaryBeId(cluster, getId(), beId); + idx.removeSecondaryBe(cluster, getId()); } /** @@ -551,37 +581,38 @@ 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)); + getCloudInvertedIndex().setSecondaryBe(cluster, getId(), beId, changeTimestamp); } public void clearClusterToBe(String cluster) { - primaryClusterToBackend.remove(cluster); - secondaryClusterToBackends.remove(cluster); + CloudTabletInvertedIndex idx = getCloudInvertedIndex(); + idx.removePrimaryBeId(cluster, getId()); + idx.removeSecondaryBe(cluster, getId()); } // 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); - if (secondBeAndChangeTimestamp == null) { + CloudTabletInvertedIndex idx = getCloudInvertedIndex(); + long beId = idx.getSecondaryBeId(clusterId, getId()); + if (beId == -1L) { return; } - long beId = secondBeAndChangeTimestamp.key(); - long changeTimestamp = secondBeAndChangeTimestamp.value(); + long changeTimestamp = idx.getSecondaryTimestamp(clusterId, getId()); if (changeTimestamp < expireTimestamp) { LOG.debug("remove clusterId {} secondary beId {} changeTimestamp {} expireTimestamp {} replica info {}", clusterId, beId, changeTimestamp, expireTimestamp, this); - secondaryClusterToBackends.remove(clusterId); + idx.removeSecondaryBe(clusterId, getId()); return; } } public List<Backend> getAllPrimaryBes() { List<Backend> result = new ArrayList<Backend>(); - primaryClusterToBackend.forEach((clusterId, beId) -> { - if (beId != -1) { + getCloudInvertedIndex().getAllPrimaryClusterBeIds(getId()).forEach((clusterId, beId) -> { + if (beId != -1L) { Backend backend = Env.getCurrentSystemInfo().getBackend(beId); result.add(backend); } @@ -589,13 +620,47 @@ public class CloudReplica extends Replica implements GsonPostProcessable { return result; } + @Override + public long getSegmentCount() { + return segmentCount; + } + + @Override + public void setSegmentCount(long segmentCount) { + this.segmentCount = segmentCount; + } + + @Override + public long getRowsetCount() { + return rowsetCount; + } + + @Override + public void setRowsetCount(long rowsetCount) { + this.rowsetCount = rowsetCount; + } + + @Override + public void gsonPreProcess() throws IOException { + CloudTabletInvertedIndex idx = getCloudInvertedIndex(); + if (idx == null) { + return; + } + Map<String, Long> snapshot = idx.getAllPrimaryClusterBeIds(getId()); + this.primaryClusterToBackend = snapshot.isEmpty() ? null : new HashMap<>(snapshot); + } + @Override public void gsonPostProcess() throws IOException { if (LOG.isDebugEnabled()) { LOG.debug("convert CloudReplica: {}, primaryClusterToBackends: {}, primaryClusterToBackend: {}", this.getId(), this.primaryClusterToBackends, this.primaryClusterToBackend); } + // Legacy "bes" format migration if (primaryClusterToBackends != null) { + if (primaryClusterToBackend == null) { + primaryClusterToBackend = new HashMap<>(); + } for (Map.Entry<String, List<Long>> entry : primaryClusterToBackends.entrySet()) { String clusterId = entry.getKey(); List<Long> beIds = entry.getValue(); @@ -605,5 +670,15 @@ public class CloudReplica extends Replica implements GsonPostProcessable { } this.primaryClusterToBackends = null; } + // Populate central index from deserialized primaryClusterToBackend, then null it out + if (primaryClusterToBackend != null) { + CloudTabletInvertedIndex idx = getCloudInvertedIndex(); + if (idx != null) { + for (Map.Entry<String, Long> entry : primaryClusterToBackend.entrySet()) { + idx.setPrimaryBeId(entry.getKey(), getId(), entry.getValue()); + } + } + this.primaryClusterToBackend = null; + } } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/cloud/catalog/CloudTabletInvertedIndex.java b/fe/fe-core/src/main/java/org/apache/doris/cloud/catalog/CloudTabletInvertedIndex.java index 6c68429f9c0..a5755f0f5e2 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/cloud/catalog/CloudTabletInvertedIndex.java +++ b/fe/fe-core/src/main/java/org/apache/doris/cloud/catalog/CloudTabletInvertedIndex.java @@ -19,6 +19,7 @@ package org.apache.doris.cloud.catalog; import org.apache.doris.catalog.Replica; import org.apache.doris.catalog.TabletInvertedIndex; +import org.apache.doris.foundation.util.ConcurrentLong2LongHashMap; import com.google.common.base.Preconditions; import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; @@ -26,7 +27,10 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; public class CloudTabletInvertedIndex extends TabletInvertedIndex { private static final Logger LOG = LogManager.getLogger(CloudTabletInvertedIndex.class); @@ -35,6 +39,15 @@ public class CloudTabletInvertedIndex extends TabletInvertedIndex { // for cloud mode, no need to know the replica's backend private Long2ObjectOpenHashMap<Replica> replicaMetaMap = new Long2ObjectOpenHashMap<>(); + // Centralized cluster-to-BE mappings (moved from per-CloudReplica ConcurrentHashMaps). + // Outer key: clusterId (typically 1-3 clusters). Inner key: replicaId. Value: beId or timestamp. + private final ConcurrentHashMap<String, ConcurrentLong2LongHashMap> clusterPrimaryBeMap + = new ConcurrentHashMap<>(); + private final ConcurrentHashMap<String, ConcurrentLong2LongHashMap> clusterSecondaryBeMap + = new ConcurrentHashMap<>(); + private final ConcurrentHashMap<String, ConcurrentLong2LongHashMap> clusterSecondaryTsMap + = new ConcurrentHashMap<>(); + public CloudTabletInvertedIndex() { super(); } @@ -56,7 +69,12 @@ public class CloudTabletInvertedIndex extends TabletInvertedIndex { public void deleteTablet(long tabletId) { long stamp = writeLock(); try { - replicaMetaMap.remove(tabletId); + Replica replica = replicaMetaMap.remove(tabletId); + if (replica != null) { + long replicaId = replica.getId(); + clearPrimaryBeForReplica(replicaId); + clearSecondaryBeForReplica(replicaId); + } tabletMetaMap.remove(tabletId); if (LOG.isDebugEnabled()) { LOG.debug("delete tablet: {}", tabletId); @@ -88,8 +106,11 @@ public class CloudTabletInvertedIndex extends TabletInvertedIndex { Preconditions.checkState(tabletMetaMap.containsKey(tabletId), "tablet " + tabletId + " not exists"); if (replicaMetaMap.containsKey(tabletId)) { Replica replica = replicaMetaMap.remove(tabletId); + long replicaId = replica.getId(); + clearPrimaryBeForReplica(replicaId); + clearSecondaryBeForReplica(replicaId); if (LOG.isDebugEnabled()) { - LOG.debug("delete replica {} of tablet {}", replica.getId(), tabletId); + LOG.debug("delete replica {} of tablet {}", replicaId, tabletId); } } else { // this may happen when fe restart after tablet is empty(bug cause) @@ -128,5 +149,96 @@ public class CloudTabletInvertedIndex extends TabletInvertedIndex { @Override protected void innerClear() { replicaMetaMap.clear(); + clusterPrimaryBeMap.clear(); + clusterSecondaryBeMap.clear(); + clusterSecondaryTsMap.clear(); + } + + // ---- Central cluster-to-BE mapping accessors ---- + + private ConcurrentLong2LongHashMap getOrCreateClusterMap( + ConcurrentHashMap<String, ConcurrentLong2LongHashMap> outer, String clusterId) { + return outer.computeIfAbsent(clusterId, k -> new ConcurrentLong2LongHashMap()); + } + + // -- Primary BE -- + + public long getPrimaryBeId(String clusterId, long replicaId) { + ConcurrentLong2LongHashMap inner = clusterPrimaryBeMap.get(clusterId); + if (inner == null) { + return -1L; + } + return inner.getOrDefault(replicaId, -1L); + } + + public void setPrimaryBeId(String clusterId, long replicaId, long beId) { + getOrCreateClusterMap(clusterPrimaryBeMap, clusterId).put(replicaId, beId); + } + + public void removePrimaryBeId(String clusterId, long replicaId) { + ConcurrentLong2LongHashMap inner = clusterPrimaryBeMap.get(clusterId); + if (inner != null) { + inner.remove(replicaId); + } + } + + public void clearPrimaryBeForReplica(long replicaId) { + for (ConcurrentLong2LongHashMap inner : clusterPrimaryBeMap.values()) { + inner.remove(replicaId); + } + } + + public Map<String, Long> getAllPrimaryClusterBeIds(long replicaId) { + Map<String, Long> result = new HashMap<>(); + for (Map.Entry<String, ConcurrentLong2LongHashMap> entry : clusterPrimaryBeMap.entrySet()) { + long beId = entry.getValue().getOrDefault(replicaId, -1L); + if (beId != -1L) { + result.put(entry.getKey(), beId); + } + } + return result; + } + + // -- Secondary BE -- + + public long getSecondaryBeId(String clusterId, long replicaId) { + ConcurrentLong2LongHashMap inner = clusterSecondaryBeMap.get(clusterId); + if (inner == null) { + return -1L; + } + return inner.getOrDefault(replicaId, -1L); + } + + public long getSecondaryTimestamp(String clusterId, long replicaId) { + ConcurrentLong2LongHashMap inner = clusterSecondaryTsMap.get(clusterId); + if (inner == null) { + return -1L; + } + return inner.getOrDefault(replicaId, -1L); + } + + public void setSecondaryBe(String clusterId, long replicaId, long beId, long timestamp) { + getOrCreateClusterMap(clusterSecondaryBeMap, clusterId).put(replicaId, beId); + getOrCreateClusterMap(clusterSecondaryTsMap, clusterId).put(replicaId, timestamp); + } + + public void removeSecondaryBe(String clusterId, long replicaId) { + ConcurrentLong2LongHashMap beMap = clusterSecondaryBeMap.get(clusterId); + if (beMap != null) { + beMap.remove(replicaId); + } + ConcurrentLong2LongHashMap tsMap = clusterSecondaryTsMap.get(clusterId); + if (tsMap != null) { + tsMap.remove(replicaId); + } + } + + public void clearSecondaryBeForReplica(long replicaId) { + for (ConcurrentLong2LongHashMap inner : clusterSecondaryBeMap.values()) { + inner.remove(replicaId); + } + for (ConcurrentLong2LongHashMap inner : clusterSecondaryTsMap.values()) { + inner.remove(replicaId); + } } } diff --git a/fe/fe-core/src/test/java/org/apache/doris/cloud/catalog/CloudReplicaCentralIndexTest.java b/fe/fe-core/src/test/java/org/apache/doris/cloud/catalog/CloudReplicaCentralIndexTest.java new file mode 100644 index 00000000000..5233ebed273 --- /dev/null +++ b/fe/fe-core/src/test/java/org/apache/doris/cloud/catalog/CloudReplicaCentralIndexTest.java @@ -0,0 +1,255 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.cloud.catalog; + +import org.apache.doris.catalog.Env; +import org.apache.doris.catalog.Replica; +import org.apache.doris.catalog.TabletMeta; +import org.apache.doris.catalog.TabletInvertedIndex; +import org.apache.doris.thrift.TStorageMedium; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.MockedStatic; +import org.mockito.Mockito; + +import java.util.Map; + +public class CloudReplicaCentralIndexTest { + + private CloudTabletInvertedIndex index; + private MockedStatic<Env> mockedEnv; + private Env mockEnvInstance; + + @BeforeEach + public void setUp() { + index = new CloudTabletInvertedIndex(); + mockEnvInstance = Mockito.mock(Env.class); + Mockito.when(mockEnvInstance.getTabletInvertedIndex()).thenReturn(index); + + mockedEnv = Mockito.mockStatic(Env.class); + mockedEnv.when(Env::getCurrentEnv).thenReturn(mockEnvInstance); + mockedEnv.when(Env::getCurrentInvertedIndex).thenReturn(index); + } + + @AfterEach + public void tearDown() { + mockedEnv.close(); + } + + @Test + public void testPrimaryBeRoundTrip() { + String cluster = "cluster1"; + long replicaId = 1001L; + long beId = 2001L; + + // Initially not found + Assertions.assertEquals(-1L, index.getPrimaryBeId(cluster, replicaId)); + + // Set and get + index.setPrimaryBeId(cluster, replicaId, beId); + Assertions.assertEquals(beId, index.getPrimaryBeId(cluster, replicaId)); + + // Different cluster returns -1 + Assertions.assertEquals(-1L, index.getPrimaryBeId("cluster2", replicaId)); + + // Remove + index.removePrimaryBeId(cluster, replicaId); + Assertions.assertEquals(-1L, index.getPrimaryBeId(cluster, replicaId)); + } + + @Test + public void testGetAllPrimaryClusterBeIds() { + long replicaId = 1001L; + + index.setPrimaryBeId("cluster1", replicaId, 2001L); + index.setPrimaryBeId("cluster2", replicaId, 2002L); + index.setPrimaryBeId("cluster1", 9999L, 3001L); // different replica + + Map<String, Long> result = index.getAllPrimaryClusterBeIds(replicaId); + Assertions.assertEquals(2, result.size()); + Assertions.assertEquals(2001L, result.get("cluster1")); + Assertions.assertEquals(2002L, result.get("cluster2")); + } + + @Test + public void testSecondaryBeLifecycle() { + String cluster = "cluster1"; + long replicaId = 1001L; + long beId = 2001L; + long timestamp = 123456789L; + + // Initially not found + Assertions.assertEquals(-1L, index.getSecondaryBeId(cluster, replicaId)); + Assertions.assertEquals(-1L, index.getSecondaryTimestamp(cluster, replicaId)); + + // Set and get + index.setSecondaryBe(cluster, replicaId, beId, timestamp); + Assertions.assertEquals(beId, index.getSecondaryBeId(cluster, replicaId)); + Assertions.assertEquals(timestamp, index.getSecondaryTimestamp(cluster, replicaId)); + + // Remove + index.removeSecondaryBe(cluster, replicaId); + Assertions.assertEquals(-1L, index.getSecondaryBeId(cluster, replicaId)); + Assertions.assertEquals(-1L, index.getSecondaryTimestamp(cluster, replicaId)); + } + + @Test + public void testDeleteTabletCleanup() { + long tabletId = 100L; + long replicaId = 1001L; + + // Register tablet and replica + TabletMeta meta = new TabletMeta(1, 2, 3, 4, 0, TStorageMedium.HDD); + index.addTablet(tabletId, meta); + CloudReplica replica = new CloudReplica(replicaId, -1L, + Replica.ReplicaState.NORMAL, 1, 0, 1, 2, 3, 4, 0); + index.addReplica(tabletId, replica); + + // Set primary and secondary + index.setPrimaryBeId("cluster1", replicaId, 2001L); + index.setSecondaryBe("cluster1", replicaId, 2002L, 100L); + + // Delete tablet should clean up both + index.deleteTablet(tabletId); + + Assertions.assertEquals(-1L, index.getPrimaryBeId("cluster1", replicaId)); + Assertions.assertEquals(-1L, index.getSecondaryBeId("cluster1", replicaId)); + } + + @Test + public void testDeleteReplicaCleanup() { + long tabletId = 100L; + long replicaId = 1001L; + + TabletMeta meta = new TabletMeta(1, 2, 3, 4, 0, TStorageMedium.HDD); + index.addTablet(tabletId, meta); + CloudReplica replica = new CloudReplica(replicaId, -1L, + Replica.ReplicaState.NORMAL, 1, 0, 1, 2, 3, 4, 0); + index.addReplica(tabletId, replica); + + index.setPrimaryBeId("cluster1", replicaId, 2001L); + index.setPrimaryBeId("cluster2", replicaId, 2002L); + index.setSecondaryBe("cluster1", replicaId, 3001L, 100L); + + index.deleteReplica(tabletId, -1); + + Assertions.assertEquals(-1L, index.getPrimaryBeId("cluster1", replicaId)); + Assertions.assertEquals(-1L, index.getPrimaryBeId("cluster2", replicaId)); + Assertions.assertEquals(-1L, index.getSecondaryBeId("cluster1", replicaId)); + } + + @Test + public void testClearPrimaryBeForReplica() { + long replicaId = 1001L; + + index.setPrimaryBeId("cluster1", replicaId, 2001L); + index.setPrimaryBeId("cluster2", replicaId, 2002L); + index.setPrimaryBeId("cluster1", 9999L, 3001L); // other replica untouched + + index.clearPrimaryBeForReplica(replicaId); + + Assertions.assertEquals(-1L, index.getPrimaryBeId("cluster1", replicaId)); + Assertions.assertEquals(-1L, index.getPrimaryBeId("cluster2", replicaId)); + Assertions.assertEquals(3001L, index.getPrimaryBeId("cluster1", 9999L)); // untouched + } + + @Test + public void testInnerClearCleansAllMaps() { + long replicaId = 1001L; + + index.setPrimaryBeId("cluster1", replicaId, 2001L); + index.setSecondaryBe("cluster1", replicaId, 3001L, 100L); + + // innerClear is protected, but clear() calls it + index.clear(); + + Assertions.assertEquals(-1L, index.getPrimaryBeId("cluster1", replicaId)); + Assertions.assertEquals(-1L, index.getSecondaryBeId("cluster1", replicaId)); + } + + @Test + public void testGsonPreProcessPopulatesField() throws Exception { + long replicaId = 1001L; + index.setPrimaryBeId("cluster1", replicaId, 2001L); + index.setPrimaryBeId("cluster2", replicaId, 2002L); + + CloudReplica replica = new CloudReplica(replicaId, -1L, + Replica.ReplicaState.NORMAL, 1, 0, 1, 2, 3, 4, 0); + + // gsonPreProcess should populate primaryClusterToBackend for serialization + replica.gsonPreProcess(); + + // Access the private field to verify + java.lang.reflect.Field field = CloudReplica.class.getDeclaredField("primaryClusterToBackend"); + field.setAccessible(true); + @SuppressWarnings("unchecked") + Map<String, Long> beMap = (Map<String, Long>) field.get(replica); + + Assertions.assertNotNull(beMap); + Assertions.assertEquals(2001L, beMap.get("cluster1")); + Assertions.assertEquals(2002L, beMap.get("cluster2")); + } + + @Test + public void testGsonPostProcessPopulatesIndex() throws Exception { + long replicaId = 1001L; + + CloudReplica replica = new CloudReplica(replicaId, -1L, + Replica.ReplicaState.NORMAL, 1, 0, 1, 2, 3, 4, 0); + + // Simulate deserialized state: primaryClusterToBackend has data + java.lang.reflect.Field field = CloudReplica.class.getDeclaredField("primaryClusterToBackend"); + field.setAccessible(true); + java.util.HashMap<String, Long> beMap = new java.util.HashMap<>(); + beMap.put("cluster1", 2001L); + beMap.put("cluster2", 2002L); + field.set(replica, beMap); + + // gsonPostProcess should move data to central index and null out the field + replica.gsonPostProcess(); + + Assertions.assertEquals(2001L, index.getPrimaryBeId("cluster1", replicaId)); + Assertions.assertEquals(2002L, index.getPrimaryBeId("cluster2", replicaId)); + Assertions.assertNull(field.get(replica)); + } + + @Test + public void testGsonPostProcessLegacyBesFormat() throws Exception { + long replicaId = 1001L; + + CloudReplica replica = new CloudReplica(replicaId, -1L, + Replica.ReplicaState.NORMAL, 1, 0, 1, 2, 3, 4, 0); + + // Simulate legacy "bes" format + java.lang.reflect.Field besField = CloudReplica.class.getDeclaredField("primaryClusterToBackends"); + besField.setAccessible(true); + java.util.concurrent.ConcurrentHashMap<String, java.util.List<Long>> besMap + = new java.util.concurrent.ConcurrentHashMap<>(); + besMap.put("cluster1", java.util.List.of(2001L, 2002L)); + besField.set(replica, besMap); + + replica.gsonPostProcess(); + + // Should have migrated to central index using first element + Assertions.assertEquals(2001L, index.getPrimaryBeId("cluster1", replicaId)); + Assertions.assertNull(besField.get(replica)); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
