You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@helix.apache.org by jx...@apache.org on 2021/04/28 20:18:23 UTC

[helix] branch master updated: Change MessageQueueMonitor from static to dynamic metric (#1715)

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 af3ee1c  Change MessageQueueMonitor from static to dynamic metric (#1715)
af3ee1c is described below

commit af3ee1cf5845e7d853f20a2cee1e461689d799df
Author: Ramin Bashizade <ra...@linkedin.com>
AuthorDate: Wed Apr 28 13:18:10 2021 -0700

    Change MessageQueueMonitor from static to dynamic metric (#1715)
    
    * Change MessageQueueMonitor from static to dynamic metric
    
    This commit modifies the MessageQueueMonitor class to use dynamic
    metrics by extending the DynamicMBeanProvider class. It also
    deprecates the MessageQueueMonitorMBean interface because it is
    no longer used..
    
    After converting MessageQueueMonitor from static to dynamic metric, the
    MessageQueueMonitorMBean interface is no longer used in the code. Since the
    goal is to remove all direct references to the MBean class, this interface
    is deleted.
---
 .../monitoring/mbeans/MessageQueueMonitor.java     | 63 +++++++++-------------
 .../mbeans/MessageQueueMonitorMBean.java           | 30 -----------
 2 files changed, 25 insertions(+), 68 deletions(-)

diff --git a/helix-core/src/main/java/org/apache/helix/monitoring/mbeans/MessageQueueMonitor.java b/helix-core/src/main/java/org/apache/helix/monitoring/mbeans/MessageQueueMonitor.java
index faa8e30..c898fa4 100644
--- a/helix-core/src/main/java/org/apache/helix/monitoring/mbeans/MessageQueueMonitor.java
+++ b/helix-core/src/main/java/org/apache/helix/monitoring/mbeans/MessageQueueMonitor.java
@@ -20,26 +20,33 @@ package org.apache.helix.monitoring.mbeans;
  */
 
 import java.lang.management.ManagementFactory;
+import java.util.ArrayList;
+import java.util.List;
+import javax.management.JMException;
 import javax.management.MBeanServer;
 import javax.management.MalformedObjectNameException;
 import javax.management.ObjectName;
 
+import org.apache.helix.monitoring.mbeans.dynamicMBeans.DynamicMBeanProvider;
+import org.apache.helix.monitoring.mbeans.dynamicMBeans.DynamicMetric;
+import org.apache.helix.monitoring.mbeans.dynamicMBeans.SimpleDynamicMetric;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class MessageQueueMonitor implements MessageQueueMonitorMBean {
+public class MessageQueueMonitor extends DynamicMBeanProvider {
+  private static final String MBEAN_DESCRIPTION = "Message Queue Monitor";
   private static final Logger LOG = LoggerFactory.getLogger(MessageQueueMonitor.class);
 
   private final String _clusterName;
   private final String _instanceName;
   private final MBeanServer _beanServer;
-  private long _messageQueueBacklog;
+  private SimpleDynamicMetric<Long> _messageQueueBacklog;
 
   public MessageQueueMonitor(String clusterName, String instanceName) {
     _clusterName = clusterName;
     _instanceName = instanceName;
     _beanServer = ManagementFactory.getPlatformMBeanServer();
-    _messageQueueBacklog = 0;
+    _messageQueueBacklog = new SimpleDynamicMetric("MessageQueueBacklog", 0L);
   }
 
   /**
@@ -47,12 +54,7 @@ public class MessageQueueMonitor implements MessageQueueMonitorMBean {
    * @param size the message queue size
    */
   public void setMessageQueueBacklog(long size) {
-    _messageQueueBacklog = size;
-  }
-
-  @Override
-  public long getMessageQueueBacklog() {
-    return _messageQueueBacklog;
+    _messageQueueBacklog.updateValue(size);
   }
 
   /**
@@ -60,7 +62,7 @@ public class MessageQueueMonitor implements MessageQueueMonitorMBean {
    */
   public void init() {
     try {
-      register(this, getObjectName(getBeanName()));
+      register();
     } catch (Exception e) {
       LOG.error("Fail to register MessageQueueMonitor", e);
     }
@@ -70,9 +72,9 @@ public class MessageQueueMonitor implements MessageQueueMonitorMBean {
    * Remove this bean from the server
    */
   public void reset() {
-    _messageQueueBacklog = 0;
+    _messageQueueBacklog.updateValue(0L);
     try {
-      unregister(getObjectName(getBeanName()));
+      unregister();
     } catch (Exception e) {
       LOG.error("Fail to register MessageQueueMonitor", e);
     }
@@ -83,32 +85,17 @@ public class MessageQueueMonitor implements MessageQueueMonitorMBean {
     return ClusterStatusMonitor.MESSAGE_QUEUE_STATUS_KEY + "." + _clusterName;
   }
 
-  private void register(Object bean, ObjectName name) {
-    try {
-      if (_beanServer.isRegistered(name)) {
-        _beanServer.unregisterMBean(name);
-      }
-    } catch (Exception e) {
-      // OK
-    }
-
-    try {
-      LOG.info("Register MBean: " + name);
-      _beanServer.registerMBean(bean, name);
-    } catch (Exception e) {
-      LOG.warn("Could not register MBean: " + name, e);
-    }
-  }
-
-  private void unregister(ObjectName name) {
-    try {
-      if (_beanServer.isRegistered(name)) {
-        LOG.info("Unregistering " + name.toString());
-        _beanServer.unregisterMBean(name);
-      }
-    } catch (Exception e) {
-      LOG.warn("Could not unregister MBean: " + name, e);
-    }
+  /**
+   * This method registers the dynamic metrics.
+   * @return
+   * @throws JMException
+   */
+  @Override
+  public DynamicMBeanProvider register() throws JMException {
+    List<DynamicMetric<?, ?>> attributeList = new ArrayList<>();
+    attributeList.add(_messageQueueBacklog);
+    doRegister(attributeList, MBEAN_DESCRIPTION, getObjectName(getBeanName()));
+    return this;
   }
 
   private String getClusterBeanName() {
diff --git a/helix-core/src/main/java/org/apache/helix/monitoring/mbeans/MessageQueueMonitorMBean.java b/helix-core/src/main/java/org/apache/helix/monitoring/mbeans/MessageQueueMonitorMBean.java
deleted file mode 100644
index 075c48e..0000000
--- a/helix-core/src/main/java/org/apache/helix/monitoring/mbeans/MessageQueueMonitorMBean.java
+++ /dev/null
@@ -1,30 +0,0 @@
-package org.apache.helix.monitoring.mbeans;
-
-/*
- * 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 org.apache.helix.monitoring.SensorNameProvider;
-
-public interface MessageQueueMonitorMBean extends SensorNameProvider {
-  /**
-   * Get the message queue size
-   * @return
-   */
-  public long getMessageQueueBacklog();
-}