001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.regionserver;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertFalse;
022import static org.junit.jupiter.api.Assertions.assertNotNull;
023import static org.junit.jupiter.api.Assertions.assertTrue;
024
025import java.net.HttpURLConnection;
026import java.net.URL;
027import java.util.regex.Matcher;
028import java.util.regex.Pattern;
029import org.apache.hadoop.conf.Configuration;
030import org.apache.hadoop.hbase.HBaseTestingUtil;
031import org.apache.hadoop.hbase.HConstants;
032import org.apache.hadoop.hbase.testclassification.MetricsTests;
033import org.apache.hadoop.hbase.testclassification.SmallTests;
034import org.apache.hadoop.hbase.util.Pair;
035import org.apache.http.client.methods.CloseableHttpResponse;
036import org.apache.http.client.methods.HttpGet;
037import org.apache.http.impl.client.CloseableHttpClient;
038import org.apache.http.impl.client.HttpClients;
039import org.apache.http.util.EntityUtils;
040import org.junit.jupiter.api.AfterAll;
041import org.junit.jupiter.api.BeforeAll;
042import org.junit.jupiter.api.Tag;
043import org.junit.jupiter.api.Test;
044
045@Tag(MetricsTests.TAG)
046@Tag(SmallTests.TAG)
047public class TestMetricsJvm {
048
049  private final static HBaseTestingUtil UTIL = new HBaseTestingUtil();
050  private static Configuration conf;
051
052  @BeforeAll
053  public static void before() throws Exception {
054    conf = UTIL.getConfiguration();
055    // The master info server does not run in tests by default.
056    // Set it to ephemeral port so that it will start
057    conf.setInt(HConstants.MASTER_INFO_PORT, 0);
058    UTIL.startMiniCluster();
059  }
060
061  @AfterAll
062  public static void after() throws Exception {
063    UTIL.shutdownMiniCluster();
064  }
065
066  @Test
067  public void testJvmMetrics() throws Exception {
068    final Pair<Integer, String> jmxPage = getJmxPage("?qry=Hadoop:service=HBase,name=JvmMetrics*");
069    assertNotNull(jmxPage);
070
071    final Integer responseCode = jmxPage.getFirst();
072    final String responseBody = jmxPage.getSecond();
073
074    assertEquals(HttpURLConnection.HTTP_OK, responseCode.intValue());
075    assertNotNull(responseBody);
076
077    assertNotFind("\"tag.ProcessName\"\\s*:\\s*\"IO\"", responseBody);
078    assertReFind("\"tag.ProcessName\"\\s*:\\s*\"Master\"", responseBody);
079  }
080
081  private Pair<Integer, String> getJmxPage(String query) throws Exception {
082    URL url = new URL("http://localhost:"
083      + UTIL.getHBaseCluster().getMaster().getInfoServer().getPort() + "/jmx" + query);
084    return getUrlContent(url);
085  }
086
087  private Pair<Integer, String> getUrlContent(URL url) throws Exception {
088    try (CloseableHttpClient client = HttpClients.createDefault()) {
089      CloseableHttpResponse resp = client.execute(new HttpGet(url.toURI()));
090      int code = resp.getStatusLine().getStatusCode();
091      if (code == HttpURLConnection.HTTP_OK) {
092        return new Pair<>(code, EntityUtils.toString(resp.getEntity()));
093      }
094      return new Pair<>(code, null);
095    }
096  }
097
098  private void assertReFind(String re, String value) {
099    Pattern p = Pattern.compile(re);
100    Matcher m = p.matcher(value);
101    assertTrue(m.find(), "'" + p + "' does not match " + value);
102  }
103
104  private void assertNotFind(String re, String value) {
105    Pattern p = Pattern.compile(re);
106    Matcher m = p.matcher(value);
107    assertFalse(m.find(), "'" + p + "' should not match " + value);
108  }
109}