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      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("Master", masterSource.getMetricsName());
55      assertEquals("RegionServer", 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      HELPER.assertGauge("numActiveWriteHandler", 50, serverSource);
73      HELPER.assertGauge("numActiveReadHandler", 50, serverSource);
74      HELPER.assertGauge("numActiveScanHandler", 6, serverSource);
75      HELPER.assertGauge("numCallsInWriteQueue", 50, serverSource);
76      HELPER.assertGauge("numCallsInReadQueue", 50, serverSource);
77      HELPER.assertGauge("numCallsInScanQueue", 2, serverSource);
78    }
79  
80    /**
81     * Test to make sure that all the actively called method on MetricsHBaseServer work.
82     */
83    @Test
84    public void testSourceMethods() {
85      MetricsHBaseServer mrpc = new MetricsHBaseServer("HMaster", new MetricsHBaseServerWrapperStub());
86      MetricsHBaseServerSource serverSource = mrpc.getMetricsSource();
87  
88      for (int i=0; i < 12; i++) {
89        mrpc.authenticationFailure();
90      }
91      for (int i=0; i < 13; i++) {
92        mrpc.authenticationSuccess();
93      }
94      HELPER.assertCounter("authenticationFailures", 12, serverSource);
95      HELPER.assertCounter("authenticationSuccesses", 13, serverSource);
96  
97  
98  
99      for (int i=0; i < 14; i++) {
100       mrpc.authorizationSuccess();
101     }
102     for (int i=0; i < 15; i++) {
103       mrpc.authorizationFailure();
104     }
105     HELPER.assertCounter("authorizationSuccesses", 14, serverSource);
106     HELPER.assertCounter("authorizationFailures", 15, serverSource);
107 
108 
109     mrpc.dequeuedCall(100);
110     mrpc.processedCall(101);
111     mrpc.totalCall(102);
112     HELPER.assertCounter("queueCallTime_NumOps", 1, serverSource);
113     HELPER.assertCounter("processCallTime_NumOps", 1, serverSource);
114     HELPER.assertCounter("totalCallTime_NumOps", 1, serverSource);
115 
116     mrpc.sentBytes(103);
117     mrpc.sentBytes(103);
118     mrpc.sentBytes(103);
119 
120     mrpc.receivedBytes(104);
121     mrpc.receivedBytes(104);
122 
123     HELPER.assertCounter("sentBytes", 309, serverSource);
124     HELPER.assertCounter("receivedBytes", 208, serverSource);
125 
126     mrpc.receivedRequest(105);
127     mrpc.sentResponse(106);
128     HELPER.assertCounter("requestSize_NumOps", 1, serverSource);
129     HELPER.assertCounter("responseSize_NumOps", 1, serverSource);
130 
131     mrpc.exception(null);
132     HELPER.assertCounter("exceptions", 1, serverSource);
133 
134     mrpc.exception(new RegionMovedException(ServerName.parseServerName("localhost:60020"), 100));
135     mrpc.exception(new RegionTooBusyException());
136     mrpc.exception(new OutOfOrderScannerNextException());
137     mrpc.exception(new NotServingRegionException());
138     HELPER.assertCounter("exceptions.RegionMovedException", 1, serverSource);
139     HELPER.assertCounter("exceptions.RegionTooBusyException", 1, serverSource);
140     HELPER.assertCounter("exceptions.OutOfOrderScannerNextException", 1, serverSource);
141     HELPER.assertCounter("exceptions.NotServingRegionException", 1, serverSource);
142     HELPER.assertCounter("exceptions", 5, serverSource);
143   }
144 
145   @Test
146   public void testServerContextNameWithHostName() {
147     String[] masterServerNames = { "master/node-xyz/10.19.250.253:16020",
148         "master/node-regionserver-xyz/10.19.250.253:16020", "HMaster/node-xyz/10.19.250.253:16020",
149         "HMaster/node-regionserver-xyz/10.19.250.253:16020" };
150 
151     String[] regionServerNames = { "regionserver/node-xyz/10.19.250.253:16020",
152         "regionserver/node-master1-xyz/10.19.250.253:16020",
153         "HRegionserver/node-xyz/10.19.250.253:16020",
154         "HRegionserver/node-master1-xyz/10.19.250.253:16020" };
155 
156     MetricsHBaseServerSource masterSource = null;
157     for (String serverName : masterServerNames) {
158       masterSource = new MetricsHBaseServer(serverName, new MetricsHBaseServerWrapperStub())
159           .getMetricsSource();
160       assertEquals("master", masterSource.getMetricsContext());
161       assertEquals("Master,sub=IPC", masterSource.getMetricsJmxContext());
162       assertEquals("Master", masterSource.getMetricsName());
163     }
164 
165     MetricsHBaseServerSource rsSource = null;
166     for (String serverName : regionServerNames) {
167       rsSource = new MetricsHBaseServer(serverName, new MetricsHBaseServerWrapperStub())
168           .getMetricsSource();
169       assertEquals("regionserver", rsSource.getMetricsContext());
170       assertEquals("RegionServer,sub=IPC", rsSource.getMetricsJmxContext());
171       assertEquals("RegionServer", rsSource.getMetricsName());
172     }
173   }
174 }
175