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