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