1   /*
2    * Copyright 2011 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.regionserver;
22  
23  import java.io.IOException;
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.hadoop.conf.Configuration;
30  import org.apache.hadoop.hbase.HBaseTestingUtility;
31  import org.apache.hadoop.hbase.ipc.HBaseRpcMetrics;
32  import org.apache.hadoop.metrics.ContextFactory;
33  import org.apache.hadoop.metrics.MetricsContext;
34  import org.apache.hadoop.metrics.MetricsUtil;
35  import org.apache.hadoop.metrics.spi.AbstractMetricsContext;
36  import org.apache.hadoop.metrics.spi.OutputRecord;
37  import org.junit.AfterClass;
38  import org.junit.BeforeClass;
39  import org.junit.Test;
40  
41  import static org.junit.Assert.*;
42  
43  public class TestRpcMetrics {
44    /**
45     * Defines test methods to register with HBaseRpcMetrics
46     */
47    public interface TestMetrics {
48      public void test();
49    }
50  
51    /**
52     * HRegionServer sub-class to register custom metrics
53     */
54    public static class TestRegionServer extends HRegionServer {
55  
56      public TestRegionServer(Configuration conf)
57          throws IOException, InterruptedException {
58        super(conf);
59  
60        // register custom metrics interface
61        getRpcMetrics().createMetrics(new Class[]{TestMetrics.class}, true);
62      }
63  
64      public void incTest(int amt) {
65        HBaseRpcMetrics metrics = getRpcMetrics();
66        // force an increment so we have something to check for
67        metrics.inc(metrics.getMetricName(TestMetrics.class, "test"), amt);
68      }
69    }
70  
71    /**
72     * Dummy metrics context to allow retrieval of values
73     */
74    public static class MockMetricsContext extends AbstractMetricsContext {
75  
76      public MockMetricsContext() {
77        // update every 1 sec.
78        setPeriod(1);
79      }
80  
81      @Override
82      protected void emitRecord(String contextName, String recordName,
83          OutputRecord outputRecord) throws IOException {
84        for (String name : outputRecord.getMetricNames()) {
85          Number val = outputRecord.getMetric(name);
86          if (val != null && val.intValue() > 0) {
87            METRICS.put(name, Boolean.TRUE);
88            LOG.debug("Set metric "+name+" to "+val);
89          }
90        }
91      }
92    }
93    private static Map<String,Boolean> METRICS = new HashMap<String,Boolean>();
94  
95    private static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
96    private static Log LOG = LogFactory.getLog(TestRpcMetrics.class);
97  
98    @BeforeClass
99    public static void setupBeforeClass() throws Exception {
100     // set custom metrics context
101     ContextFactory factory = ContextFactory.getFactory();
102     factory.setAttribute("rpc.class", MockMetricsContext.class.getName());
103     // make sure metrics context is setup, otherwise updating won't start
104     MetricsContext ctx = MetricsUtil.getContext("rpc");
105     assertTrue("Wrong MetricContext implementation class",
106         (ctx instanceof MockMetricsContext));
107 
108     TEST_UTIL.startMiniZKCluster();
109   }
110 
111   @AfterClass
112   public static void tearDownAfterClass() throws Exception {
113     TEST_UTIL.shutdownMiniZKCluster();
114   }
115 
116   @Test
117   public void testCustomMetrics() throws Exception {
118 
119     TestRegionServer rs = new TestRegionServer(TEST_UTIL.getConfiguration());
120     rs.incTest(5);
121 
122     // wait for metrics context update
123     Thread.sleep(1000);
124 
125     String metricName = HBaseRpcMetrics.getMetricName(TestMetrics.class, "test");
126     assertTrue("Metric should have set incremented for "+metricName,
127         wasSet(metricName + "_num_ops"));
128   }
129 
130   public boolean wasSet(String name) {
131     return METRICS.get(name) != null ? METRICS.get(name) : false;
132   }
133 }