You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@helix.apache.org by hu...@apache.org on 2019/06/24 23:49:34 UTC

[helix] 12/15: Enable default Jersey server metric reporting

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

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

commit 0616e972b318c66fdd8f5ce787fc8670aa9459dd
Author: Junkai Xue <jx...@linkedin.com>
AuthorDate: Wed Jun 12 18:39:50 2019 -0700

    Enable default Jersey server metric reporting
    
    For monitoring Helix REST, we can support both REST server monitoring and customized logic monitoring.
    In this rb, we enable the Jersey server monitoring metrics and adding testing for that.
    
    RB=1701238
    BUG=HELIX-1963
    G=helix-reviewers
    A=ywang4
    
    Signed-off-by: Hunter Lee <hu...@linkedin.com>
---
 .../apache/helix/rest/server/HelixRestServer.java  |  3 +
 .../rest/server/TestDefaultMonitoringMbeans.java   | 71 ++++++++++++++++++++++
 2 files changed, 74 insertions(+)

diff --git a/helix-rest/src/main/java/org/apache/helix/rest/server/HelixRestServer.java b/helix-rest/src/main/java/org/apache/helix/rest/server/HelixRestServer.java
index eea4501..af49395 100644
--- a/helix-rest/src/main/java/org/apache/helix/rest/server/HelixRestServer.java
+++ b/helix-rest/src/main/java/org/apache/helix/rest/server/HelixRestServer.java
@@ -43,6 +43,7 @@ import org.eclipse.jetty.servlet.ServletContextHandler;
 import org.eclipse.jetty.servlet.ServletHolder;
 import org.eclipse.jetty.util.ssl.SslContextFactory;
 import org.glassfish.jersey.server.ResourceConfig;
+import org.glassfish.jersey.server.ServerProperties;
 import org.glassfish.jersey.servlet.ServletContainer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -140,6 +141,8 @@ public class HelixRestServer {
     ResourceConfig cfg = new ResourceConfig();
     cfg.packages(type.getServletPackageArray());
 
+    // Enable the default statistical monitoring MBean for Jersey server
+    cfg.property(ServerProperties.MONITORING_STATISTICS_MBEANS_ENABLED, true);
     cfg.property(ContextPropertyKeys.SERVER_CONTEXT.name(),
         new ServerContext(namespace.getMetadataStoreAddress()));
     if (type == ServletType.DEFAULT_SERVLET) {
diff --git a/helix-rest/src/test/java/org/apache/helix/rest/server/TestDefaultMonitoringMbeans.java b/helix-rest/src/test/java/org/apache/helix/rest/server/TestDefaultMonitoringMbeans.java
new file mode 100644
index 0000000..90bdf17
--- /dev/null
+++ b/helix-rest/src/test/java/org/apache/helix/rest/server/TestDefaultMonitoringMbeans.java
@@ -0,0 +1,71 @@
+package org.apache.helix.rest.server;
+
+/*
+ * 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.ObjectName;
+import javax.management.ReflectionException;
+import javax.ws.rs.core.Response;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+public class TestDefaultMonitoringMbeans extends AbstractTestClass {
+
+  // For entire testing environment, we could have 2 - 4 rest server during the testing. So we dont
+  // know which REST server got the request and report number. So we have to loop all of them to
+  // report data.
+  @Test
+  public void testDefaultMonitoringMbeans()
+      throws MBeanException, ReflectionException, InstanceNotFoundException, InterruptedException {
+    int listClusters = new Random().nextInt(10);
+    for (int i = 0; i < listClusters; i++) {
+      get("clusters", null, Response.Status.OK.getStatusCode(), true);
+    }
+
+    MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
+    boolean correctReports = false;
+
+    // It may take couple milisecond to propagate the data to MBeanServer
+    while (!correctReports) {
+      for (ObjectName objectName : beanServer.queryNames(null, null)) {
+        if (objectName.toString().contains("getClusters")) {
+          // The object name is complicated, so we get the matched one and try to find out whether
+          // they have the expected attributes and value matched our expectation.
+          try {
+            if (beanServer.getAttribute(objectName, "RequestCount_total")
+                .equals(Long.valueOf(listClusters))) {
+              correctReports = true;
+            }
+          } catch (AttributeNotFoundException e) {
+
+          }
+        }
+      }
+      Thread.sleep(50);
+    }
+
+    Assert.assertTrue(correctReports);
+  }
+}