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.NotServingRegionException;
24  import org.apache.hadoop.hbase.RegionTooBusyException;
25  import org.apache.hadoop.hbase.ServerName;
26  import org.apache.hadoop.hbase.exceptions.OutOfOrderScannerNextException;
27  import org.apache.hadoop.hbase.exceptions.RegionMovedException;
28  import org.apache.hadoop.hbase.testclassification.SmallTests;
29  import org.apache.hadoop.hbase.test.MetricsAssertHelper;
30  import org.junit.Test;
31  import org.junit.experimental.categories.Category;
32  
33  
34  import static org.junit.Assert.*;
35  
36  @Category(SmallTests.class)
37  public class TestRpcMetrics {
38    public MetricsAssertHelper HELPER = CompatibilityFactory.getInstance(MetricsAssertHelper.class);
39  
40    @Test
41    public void testFactory() {
42      MetricsHBaseServer masterMetrics = new MetricsHBaseServer("HMaster", new MetricsHBaseServerWrapperStub());
43      MetricsHBaseServerSource masterSource = masterMetrics.getMetricsSource();
44  
45      MetricsHBaseServer rsMetrics = new MetricsHBaseServer("HRegionServer", new MetricsHBaseServerWrapperStub());
46      MetricsHBaseServerSource rsSource = rsMetrics.getMetricsSource();
47  
48  
49      assertEquals("master", masterSource.getMetricsContext());
50      assertEquals("regionserver", rsSource.getMetricsContext());
51  
52      assertEquals("Master,sub=IPC", masterSource.getMetricsJmxContext());
53      assertEquals("RegionServer,sub=IPC", rsSource.getMetricsJmxContext());
54  
55      assertEquals("Master", masterSource.getMetricsName());
56      assertEquals("RegionServer", rsSource.getMetricsName());
57    }
58  
59    /**
60     * This test makes sure that the numbers from a MetricsHBaseServerWrapper are correctly exported
61     * to hadoop metrics 2 system.
62     */
63    @Test
64    public void testWrapperSource() {
65      MetricsHBaseServer mrpc = new MetricsHBaseServer("HMaster", new MetricsHBaseServerWrapperStub());
66      MetricsHBaseServerSource serverSource = mrpc.getMetricsSource();
67      HELPER.assertGauge("queueSize", 101, serverSource);
68      HELPER.assertGauge("numCallsInGeneralQueue", 102, serverSource);
69      HELPER.assertGauge("numCallsInReplicationQueue", 103, serverSource);
70      HELPER.assertGauge("numCallsInPriorityQueue", 104, serverSource);
71      HELPER.assertGauge("numOpenConnections", 105, serverSource);
72      HELPER.assertGauge("numActiveHandler", 106, serverSource);
73    }
74  
75    /**
76     * Test to make sure that all the actively called method on MetricsHBaseServer work.
77     */
78    @Test
79    public void testSourceMethods() {
80      MetricsHBaseServer mrpc = new MetricsHBaseServer("HMaster", new MetricsHBaseServerWrapperStub());
81      MetricsHBaseServerSource serverSource = mrpc.getMetricsSource();
82  
83      for (int i=0; i < 12; i++) {
84        mrpc.authenticationFailure();
85      }
86      for (int i=0; i < 13; i++) {
87        mrpc.authenticationSuccess();
88      }
89      HELPER.assertCounter("authenticationFailures", 12, serverSource);
90      HELPER.assertCounter("authenticationSuccesses", 13, serverSource);
91  
92  
93  
94      for (int i=0; i < 14; i++) {
95        mrpc.authorizationSuccess();
96      }
97      for (int i=0; i < 15; i++) {
98        mrpc.authorizationFailure();
99      }
100     HELPER.assertCounter("authorizationSuccesses", 14, serverSource);
101     HELPER.assertCounter("authorizationFailures", 15, serverSource);
102 
103 
104     mrpc.dequeuedCall(100);
105     mrpc.processedCall(101);
106     mrpc.totalCall(102);
107     HELPER.assertCounter("queueCallTime_NumOps", 1, serverSource);
108     HELPER.assertCounter("processCallTime_NumOps", 1, serverSource);
109     HELPER.assertCounter("totalCallTime_NumOps", 1, serverSource);
110 
111     mrpc.sentBytes(103);
112     mrpc.sentBytes(103);
113     mrpc.sentBytes(103);
114 
115     mrpc.receivedBytes(104);
116     mrpc.receivedBytes(104);
117 
118     HELPER.assertCounter("sentBytes", 309, serverSource);
119     HELPER.assertCounter("receivedBytes", 208, serverSource);
120 
121     mrpc.receivedRequest(105);
122     mrpc.sentResponse(106);
123     HELPER.assertCounter("requestSize_NumOps", 1, serverSource);
124     HELPER.assertCounter("responseSize_NumOps", 1, serverSource);
125 
126     mrpc.exception(null);
127     HELPER.assertCounter("exceptions", 1, serverSource);
128 
129     mrpc.exception(new RegionMovedException(ServerName.parseServerName("localhost:60020"), 100));
130     mrpc.exception(new RegionTooBusyException());
131     mrpc.exception(new OutOfOrderScannerNextException());
132     mrpc.exception(new NotServingRegionException());
133     HELPER.assertCounter("exceptions.RegionMovedException", 1, serverSource);
134     HELPER.assertCounter("exceptions.RegionTooBusyException", 1, serverSource);
135     HELPER.assertCounter("exceptions.OutOfOrderScannerNextException", 1, serverSource);
136     HELPER.assertCounter("exceptions.NotServingRegionException", 1, serverSource);
137     HELPER.assertCounter("exceptions", 5, serverSource);
138   }
139 
140 }
141