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

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


The following commit(s) were added to refs/heads/master by this push:
     new e0e30a7be [apache/helix] --> Update logic for metric calculation when 
replica is set to ANY_LIVEINSTANCE (#2804)
e0e30a7be is described below

commit e0e30a7bea7a7d838b9311ff34c7aed5eaa79fff
Author: Charanya Sudharsanan <[email protected]>
AuthorDate: Tue Jun 4 15:37:16 2024 -0700

    [apache/helix] --> Update logic for metric calculation when replica is set 
to ANY_LIVEINSTANCE (#2804)
    
    This PR updates logic for metric calculation when replica is set to 
ANY_LIVEINSTANCE to avoid NFE.
---
 .../helix/monitoring/mbeans/ResourceMonitor.java    | 14 +++-----------
 .../monitoring/mbeans/TestResourceMonitor.java      | 21 +++++++++++++++++++++
 2 files changed, 24 insertions(+), 11 deletions(-)

diff --git 
a/helix-core/src/main/java/org/apache/helix/monitoring/mbeans/ResourceMonitor.java
 
b/helix-core/src/main/java/org/apache/helix/monitoring/mbeans/ResourceMonitor.java
index 5064c6481..012513ffc 100644
--- 
a/helix-core/src/main/java/org/apache/helix/monitoring/mbeans/ResourceMonitor.java
+++ 
b/helix-core/src/main/java/org/apache/helix/monitoring/mbeans/ResourceMonitor.java
@@ -272,17 +272,9 @@ public class ResourceMonitor extends DynamicMBeanProvider {
     long numOfPartitionWithTopState = 0;
 
     Set<String> partitions = idealState.getPartitionSet();
-    int replica;
-    try {
-      replica = Integer.valueOf(idealState.getReplicas());
-    } catch (NumberFormatException e) {
-      _logger.info("Unspecified replica count for {}, skip updating the 
ResourceMonitor Mbean: {}", _resourceName,
-          idealState.getReplicas());
-      return;
-    } catch (Exception ex) {
-      _logger.warn("Failed to get replica count for {}, cannot update the 
ResourceMonitor Mbean.", _resourceName);
-      return;
-    }
+
+    // returns -1 when replica is set to ANY_LIVEINSTANCE.
+    int replica = idealState.getReplicaCount(-1);
 
     int minActiveReplica = idealState.getMinActiveReplicas();
     minActiveReplica = (minActiveReplica >= 0) ? minActiveReplica : replica;
diff --git 
a/helix-core/src/test/java/org/apache/helix/monitoring/mbeans/TestResourceMonitor.java
 
b/helix-core/src/test/java/org/apache/helix/monitoring/mbeans/TestResourceMonitor.java
index 0fb0e0937..355cad450 100644
--- 
a/helix-core/src/test/java/org/apache/helix/monitoring/mbeans/TestResourceMonitor.java
+++ 
b/helix-core/src/test/java/org/apache/helix/monitoring/mbeans/TestResourceMonitor.java
@@ -37,6 +37,7 @@ import javax.management.ReflectionException;
 
 import com.google.common.collect.ImmutableMap;
 import org.apache.helix.TestHelper;
+import org.apache.helix.model.ResourceConfig;
 import org.apache.helix.zookeeper.datamodel.ZNRecord;
 import org.apache.helix.model.BuiltInStateModelDefinitions;
 import org.apache.helix.model.ExternalView;
@@ -220,6 +221,26 @@ public class TestResourceMonitor {
       
monitor.setRebalanceState(ResourceMonitor.RebalanceStatus.INTERMEDIATE_STATE_CAL_FAILED);
       Assert.assertEquals(monitor.getRebalanceState(),
           
ResourceMonitor.RebalanceStatus.INTERMEDIATE_STATE_CAL_FAILED.name());
+
+      // test when replica is set to ANY_LIVEINSTANCE and all instances are 
taken offline.
+      
idealState.setReplicas(ResourceConfig.ResourceConfigConstants.ANY_LIVEINSTANCE.name());
+
+      for (int i = 0; i < _partitions; i++) {
+        String partition = _dbName + "_" + i;
+        Map<String, String> externalViewStateMap = 
externalView.getStateMap(partition);
+        for (String key : externalViewStateMap.keySet()) {
+          if (externalViewStateMap.get(key).equalsIgnoreCase("MASTER")) {
+            externalViewStateMap.put(key, "OFFLINE");
+          }
+        }
+        externalView.setStateMap(partition, externalViewStateMap);
+      }
+
+      monitor.updateResourceState(externalView, idealState, stateModelDef);
+
+      Assert.assertEquals(monitor.getMissingTopStatePartitionGauge(), 
_partitions);
+      Assert.assertEquals(monitor.getMissingReplicaPartitionGauge(), 0);
+      Assert.assertEquals(monitor.getMissingMinActiveReplicaPartitionGauge(), 
0);
     } finally {
       // Has to unregister this monitor to clean up. Otherwise, later tests 
may be affected and fail.
       monitor.unregister();

Reply via email to