View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   * http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.ipc;
21  
22  import org.apache.hadoop.hbase.CompatibilityFactory;
23  import org.apache.hadoop.hbase.SmallTests;
24  import org.apache.hadoop.hbase.test.MetricsAssertHelper;
25  import org.junit.Test;
26  import org.junit.experimental.categories.Category;
27  
28  
29  import static org.junit.Assert.*;
30  
31  @Category(SmallTests.class)
32  public class TestRpcMetrics {
33    public MetricsAssertHelper HELPER = CompatibilityFactory.getInstance(MetricsAssertHelper.class);
34  
35    @Test
36    public void testFactory() {
37      MetricsHBaseServer masterMetrics = new MetricsHBaseServer("HMaster", new MetricsHBaseServerWrapperStub());
38      MetricsHBaseServerSource masterSource = masterMetrics.getMetricsSource();
39  
40      MetricsHBaseServer rsMetrics = new MetricsHBaseServer("HRegionServer", new MetricsHBaseServerWrapperStub());
41      MetricsHBaseServerSource rsSource = rsMetrics.getMetricsSource();
42  
43  
44      assertEquals("master", masterSource.getMetricsContext());
45      assertEquals("regionserver", rsSource.getMetricsContext());
46  
47      assertEquals("Master,sub=IPC", masterSource.getMetricsJmxContext());
48      assertEquals("RegionServer,sub=IPC", rsSource.getMetricsJmxContext());
49  
50      assertEquals("IPC", masterSource.getMetricsName());
51      assertEquals("IPC", rsSource.getMetricsName());
52    }
53  
54    /**
55     * This test makes sure that the numbers from a MetricsHBaseServerWrapper are correctly exported
56     * to hadoop metrics 2 system.
57     */
58    @Test
59    public void testWrapperSource() {
60      MetricsHBaseServer mrpc = new MetricsHBaseServer("HMaster", new MetricsHBaseServerWrapperStub());
61      MetricsHBaseServerSource serverSource = mrpc.getMetricsSource();
62      HELPER.assertGauge("queueSize", 101, serverSource);
63      HELPER.assertGauge("numCallsInGeneralQueue", 102, serverSource);
64      HELPER.assertGauge("numCallsInReplicationQueue", 103, serverSource);
65      HELPER.assertGauge("numCallsInPriorityQueue", 104, serverSource);
66      HELPER.assertGauge("numOpenConnections", 105, serverSource);
67    }
68  
69    /**
70     * Test to make sure that all the actively called method on MetricsHBaseServer work.
71     */
72    @Test
73    public void testSourceMethods() {
74      MetricsHBaseServer mrpc = new MetricsHBaseServer("HMaster", new MetricsHBaseServerWrapperStub());
75      MetricsHBaseServerSource serverSource = mrpc.getMetricsSource();
76  
77      for (int i=0; i < 12; i++) {
78        mrpc.authenticationFailure();
79      }
80      for (int i=0; i < 13; i++) {
81        mrpc.authenticationSuccess();
82      }
83      HELPER.assertCounter("authenticationFailures", 12, serverSource);
84      HELPER.assertCounter("authenticationSuccesses", 13, serverSource);
85  
86  
87  
88      for (int i=0; i < 14; i++) {
89        mrpc.authorizationSuccess();
90      }
91      for (int i=0; i < 15; i++) {
92        mrpc.authorizationFailure();
93      }
94      HELPER.assertCounter("authorizationSuccesses", 14, serverSource);
95      HELPER.assertCounter("authorizationFailures", 15, serverSource);
96  
97  
98      mrpc.dequeuedCall(100);
99      mrpc.processedCall(101);
100     HELPER.assertCounter("queueCallTime_NumOps", 1, serverSource);
101     HELPER.assertCounter("processCallTime_NumOps", 1, serverSource);
102 
103     mrpc.sentBytes(103);
104     mrpc.sentBytes(103);
105     mrpc.sentBytes(103);
106 
107     mrpc.receivedBytes(104);
108     mrpc.receivedBytes(104);
109 
110     HELPER.assertCounter("sentBytes", 309, serverSource);
111     HELPER.assertCounter("receivedBytes", 208, serverSource);
112   }
113 
114 }
115