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      HELPER.assertGauge("numActiveHandler", 106, serverSource);
68    }
69  
70    /**
71     * Test to make sure that all the actively called method on MetricsHBaseServer work.
72     */
73    @Test
74    public void testSourceMethods() {
75      MetricsHBaseServer mrpc = new MetricsHBaseServer("HMaster", new MetricsHBaseServerWrapperStub());
76      MetricsHBaseServerSource serverSource = mrpc.getMetricsSource();
77  
78      for (int i=0; i < 12; i++) {
79        mrpc.authenticationFailure();
80      }
81      for (int i=0; i < 13; i++) {
82        mrpc.authenticationSuccess();
83      }
84      HELPER.assertCounter("authenticationFailures", 12, serverSource);
85      HELPER.assertCounter("authenticationSuccesses", 13, serverSource);
86  
87  
88  
89      for (int i=0; i < 14; i++) {
90        mrpc.authorizationSuccess();
91      }
92      for (int i=0; i < 15; i++) {
93        mrpc.authorizationFailure();
94      }
95      HELPER.assertCounter("authorizationSuccesses", 14, serverSource);
96      HELPER.assertCounter("authorizationFailures", 15, serverSource);
97  
98  
99      mrpc.dequeuedCall(100);
100     mrpc.processedCall(101);
101     HELPER.assertCounter("queueCallTime_NumOps", 1, serverSource);
102     HELPER.assertCounter("processCallTime_NumOps", 1, serverSource);
103 
104     mrpc.sentBytes(103);
105     mrpc.sentBytes(103);
106     mrpc.sentBytes(103);
107 
108     mrpc.receivedBytes(104);
109     mrpc.receivedBytes(104);
110 
111     HELPER.assertCounter("sentBytes", 309, serverSource);
112     HELPER.assertCounter("receivedBytes", 208, serverSource);
113   }
114 
115 }
116