pkuwm commented on a change in pull request #1059:
URL: https://github.com/apache/helix/pull/1059#discussion_r439097213



##########
File path: 
helix-core/src/test/java/org/apache/helix/controller/rebalancer/constraint/TestAbnormalStatesResolverMonitor.java
##########
@@ -0,0 +1,89 @@
+package org.apache.helix.controller.rebalancer.constraint;
+
+/*
+ * 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.
+ */
+
+import java.lang.management.ManagementFactory;
+import java.util.Random;
+import javax.management.AttributeNotFoundException;
+import javax.management.InstanceNotFoundException;
+import javax.management.MBeanException;
+import javax.management.MBeanServer;
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectName;
+import javax.management.ReflectionException;
+
+import org.apache.helix.model.MasterSlaveSMD;
+import org.apache.helix.monitoring.mbeans.MonitorDomainNames;
+import org.apache.helix.monitoring.metrics.AbnormalStatesMetricCollector;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+public class TestAbnormalStatesResolverMonitor {
+  private static final MBeanServer beanServer = 
ManagementFactory.getPlatformMBeanServer();

Review comment:
       CONSTANT `MBEAN_SERVER`?

##########
File path: 
helix-core/src/test/java/org/apache/helix/controller/rebalancer/constraint/TestAbnormalStatesResolverMonitor.java
##########
@@ -0,0 +1,89 @@
+package org.apache.helix.controller.rebalancer.constraint;
+
+/*
+ * 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.
+ */
+
+import java.lang.management.ManagementFactory;
+import java.util.Random;
+import javax.management.AttributeNotFoundException;
+import javax.management.InstanceNotFoundException;
+import javax.management.MBeanException;
+import javax.management.MBeanServer;
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectName;
+import javax.management.ReflectionException;
+
+import org.apache.helix.model.MasterSlaveSMD;
+import org.apache.helix.monitoring.mbeans.MonitorDomainNames;
+import org.apache.helix.monitoring.metrics.AbnormalStatesMetricCollector;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+public class TestAbnormalStatesResolverMonitor {
+  private static final MBeanServer beanServer = 
ManagementFactory.getPlatformMBeanServer();
+
+  @Test
+  public void testMonitorResolver()
+      throws MalformedObjectNameException, AttributeNotFoundException, 
MBeanException,
+      ReflectionException, InstanceNotFoundException {
+    final String clusterName = "TestCluster";
+
+    final String TEST_RESOLVER_MONITOR_MBEAN_NAME = String
+        .format("%s:%s=%s, %s=%s.%s", MonitorDomainNames.Rebalancer, 
"ClusterName", clusterName,
+            "EntityName", "AbnormalStates", MasterSlaveSMD.name);
+    final ObjectName TEST_RESOLVER_MONITOR_MBEAN_OBJECT_NAME =
+        new ObjectName(TEST_RESOLVER_MONITOR_MBEAN_NAME);
+
+    
Assert.assertFalse(beanServer.isRegistered(TEST_RESOLVER_MONITOR_MBEAN_OBJECT_NAME));
+
+    // Update the resolver configuration for MasterSlave state model.
+    MonitoredAbnormalResolver monitoredAbnormalResolver =
+        new MonitoredAbnormalResolver(new MockAbnormalStateResolver(), 
clusterName,
+            MasterSlaveSMD.name);
+
+    // Validate if the MBean has been registered
+    
Assert.assertTrue(beanServer.isRegistered(TEST_RESOLVER_MONITOR_MBEAN_OBJECT_NAME));
+    
Assert.assertEquals(beanServer.getAttribute(TEST_RESOLVER_MONITOR_MBEAN_OBJECT_NAME,
+        
AbnormalStatesMetricCollector.AbnormalStatesMetricNames.AbnormalStatePartitionCounter.name()),
+        0L);
+    
Assert.assertEquals(beanServer.getAttribute(TEST_RESOLVER_MONITOR_MBEAN_OBJECT_NAME,
+        
AbnormalStatesMetricCollector.AbnormalStatesMetricNames.RecoveryAttemptCounter.name()),
 0L);
+    // Validate if the metrics recording methods work as expected
+    Random ran = new Random(System.currentTimeMillis());
+    Long expectation = 1L + ran.nextInt(10);

Review comment:
       `long`?

##########
File path: 
helix-core/src/main/java/org/apache/helix/monitoring/metrics/AbnormalStatesMetricCollector.java
##########
@@ -0,0 +1,40 @@
+package org.apache.helix.monitoring.metrics;
+
+import javax.management.JMException;
+
+import org.apache.helix.HelixException;
+import org.apache.helix.monitoring.mbeans.MonitorDomainNames;
+import org.apache.helix.monitoring.metrics.implementation.RebalanceCounter;
+import org.apache.helix.monitoring.metrics.model.CountMetric;
+
+public class AbnormalStatesMetricCollector extends MetricCollector {
+  private static final String ABNORMAL_STATES_ENTITY_NAME = "AbnormalStates";
+
+  public enum AbnormalStatesMetricNames {
+    AbnormalStatePartitionCounter, RecoveryAttemptCounter
+  }
+
+  public AbnormalStatesMetricCollector(String clusterName, String 
stateModelDef) {
+    super(MonitorDomainNames.Rebalancer.name(), clusterName,
+        String.format("%s.%s", ABNORMAL_STATES_ENTITY_NAME, stateModelDef));
+    createMetrics();
+    if (clusterName != null) {
+      try {
+        register();
+      } catch (JMException e) {
+        throw new HelixException("Failed to register MBean for the 
AbnormalStatesMetricCollector",

Review comment:
       Hardcoded `AbnormalStatesMetricCollector` -> 
AbnormalStatesMetricCollector.class.getSimpleName()? Benefit is  when 
refactoring the class name we wouldn't miss this.

##########
File path: 
helix-core/src/main/java/org/apache/helix/monitoring/metrics/AbnormalStatesMetricCollector.java
##########
@@ -0,0 +1,40 @@
+package org.apache.helix.monitoring.metrics;
+

Review comment:
       Apache lisence?

##########
File path: 
helix-core/src/main/java/org/apache/helix/monitoring/metrics/AbnormalStatesMetricCollector.java
##########
@@ -0,0 +1,40 @@
+package org.apache.helix.monitoring.metrics;
+
+import javax.management.JMException;
+
+import org.apache.helix.HelixException;
+import org.apache.helix.monitoring.mbeans.MonitorDomainNames;
+import org.apache.helix.monitoring.metrics.implementation.RebalanceCounter;
+import org.apache.helix.monitoring.metrics.model.CountMetric;
+
+public class AbnormalStatesMetricCollector extends MetricCollector {

Review comment:
       Maybe some java doc for the public class to explain this monitor?

##########
File path: 
helix-core/src/main/java/org/apache/helix/controller/rebalancer/constraint/MonitoredAbnormalResolver.java
##########
@@ -0,0 +1,117 @@
+package org.apache.helix.controller.rebalancer.constraint;
+
+/*
+ * 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.
+ */
+
+import java.util.List;
+import java.util.Map;
+
+import org.apache.helix.api.rebalancer.constraint.AbnormalStateResolver;
+import org.apache.helix.controller.stages.CurrentStateOutput;
+import org.apache.helix.model.Partition;
+import org.apache.helix.model.StateModelDefinition;
+import org.apache.helix.monitoring.metrics.AbnormalStatesMetricCollector;
+import org.apache.helix.monitoring.metrics.model.CountMetric;
+
+/**
+ * A wrap class to add monitor functionality into an AbnormalStateResolver 
implementation.
+ */
+public class MonitoredAbnormalResolver implements AbnormalStateResolver {
+  private final AbnormalStateResolver _resolver;
+  private final AbnormalStatesMetricCollector _metricCollector;
+
+  /**
+   * A placeholder which will be used when the resolver is not specified.
+   * This is a dummy class that does not really functional.
+   */
+  public static MonitoredAbnormalResolver DUMMY_STATE_RESOLVER =

Review comment:
       static final?
   




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to