001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.ipc;
019
020import static org.junit.Assert.assertEquals;
021
022import org.apache.hadoop.hbase.CallDroppedException;
023import org.apache.hadoop.hbase.CompatibilityFactory;
024import org.apache.hadoop.hbase.DoNotRetryIOException;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.NotServingRegionException;
027import org.apache.hadoop.hbase.RegionTooBusyException;
028import org.apache.hadoop.hbase.ServerName;
029import org.apache.hadoop.hbase.exceptions.OutOfOrderScannerNextException;
030import org.apache.hadoop.hbase.exceptions.RegionMovedException;
031import org.apache.hadoop.hbase.exceptions.RequestTooBigException;
032import org.apache.hadoop.hbase.test.MetricsAssertHelper;
033import org.apache.hadoop.hbase.testclassification.RPCTests;
034import org.apache.hadoop.hbase.testclassification.SmallTests;
035import org.junit.ClassRule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038
039@Category({ RPCTests.class, SmallTests.class })
040public class TestRpcMetrics {
041
042  @ClassRule
043  public static final HBaseClassTestRule CLASS_RULE =
044    HBaseClassTestRule.forClass(TestRpcMetrics.class);
045
046  public MetricsAssertHelper HELPER = CompatibilityFactory.getInstance(MetricsAssertHelper.class);
047
048  @Test
049  public void testFactory() {
050    MetricsHBaseServer masterMetrics =
051      new MetricsHBaseServer("HMaster", new MetricsHBaseServerWrapperStub());
052    MetricsHBaseServerSource masterSource = masterMetrics.getMetricsSource();
053
054    MetricsHBaseServer rsMetrics =
055      new MetricsHBaseServer("HRegionServer", new MetricsHBaseServerWrapperStub());
056    MetricsHBaseServerSource rsSource = rsMetrics.getMetricsSource();
057
058    assertEquals("master", masterSource.getMetricsContext());
059    assertEquals("regionserver", rsSource.getMetricsContext());
060
061    assertEquals("Master,sub=IPC", masterSource.getMetricsJmxContext());
062    assertEquals("RegionServer,sub=IPC", rsSource.getMetricsJmxContext());
063
064    assertEquals("Master", masterSource.getMetricsName());
065    assertEquals("RegionServer", rsSource.getMetricsName());
066  }
067
068  /**
069   * This test makes sure that the numbers from a MetricsHBaseServerWrapper are correctly exported
070   * to hadoop metrics 2 system.
071   */
072  @Test
073  public void testWrapperSource() {
074    MetricsHBaseServer mrpc =
075      new MetricsHBaseServer("HMaster", new MetricsHBaseServerWrapperStub());
076    MetricsHBaseServerSource serverSource = mrpc.getMetricsSource();
077    HELPER.assertGauge("queueSize", 101, serverSource);
078    HELPER.assertGauge("numCallsInGeneralQueue", 102, serverSource);
079    HELPER.assertGauge("numCallsInReplicationQueue", 103, serverSource);
080    HELPER.assertGauge("numCallsInPriorityQueue", 104, serverSource);
081    HELPER.assertGauge("numOpenConnections", 105, serverSource);
082    HELPER.assertGauge("numActiveHandler", 106, serverSource);
083    HELPER.assertGauge("numActiveGeneralHandler", 201, serverSource);
084    HELPER.assertGauge("numActivePriorityHandler", 202, serverSource);
085    HELPER.assertGauge("numActiveReplicationHandler", 203, serverSource);
086    HELPER.assertGauge("numActiveWriteHandler", 50, serverSource);
087    HELPER.assertGauge("numActiveReadHandler", 50, serverSource);
088    HELPER.assertGauge("numActiveScanHandler", 6, serverSource);
089    HELPER.assertGauge("numCallsInWriteQueue", 50, serverSource);
090    HELPER.assertGauge("numCallsInReadQueue", 50, serverSource);
091    HELPER.assertGauge("numCallsInScanQueue", 2, serverSource);
092    HELPER.assertGauge("nettyDirectMemoryUsage", 100, serverSource);
093    HELPER.assertGauge("nettyTotalPendingOutboundBytes", 100, serverSource);
094    HELPER.assertGauge("nettyMaxPendingOutboundBytes", 5, serverSource);
095  }
096
097  /**
098   * Test to make sure that all the actively called method on MetricsHBaseServer work.
099   */
100  @Test
101  public void testSourceMethods() {
102    MetricsHBaseServer mrpc =
103      new MetricsHBaseServer("HMaster", new MetricsHBaseServerWrapperStub());
104    MetricsHBaseServerSource serverSource = mrpc.getMetricsSource();
105
106    mrpc.unwritableTime(100);
107    mrpc.maxOutboundBytesExceeded();
108    mrpc.maxOutboundBytesExceeded();
109    HELPER.assertCounter("maxOutboundBytesExceeded", 2, serverSource);
110    HELPER.assertCounter("unwritableTime_NumOps", 1, serverSource);
111
112    for (int i = 0; i < 12; i++) {
113      mrpc.authenticationFailure();
114    }
115    for (int i = 0; i < 13; i++) {
116      mrpc.authenticationSuccess();
117    }
118    HELPER.assertCounter("authenticationFailures", 12, serverSource);
119    HELPER.assertCounter("authenticationSuccesses", 13, serverSource);
120
121    for (int i = 0; i < 14; i++) {
122      mrpc.authorizationSuccess();
123    }
124    for (int i = 0; i < 15; i++) {
125      mrpc.authorizationFailure();
126    }
127    HELPER.assertCounter("authorizationSuccesses", 14, serverSource);
128    HELPER.assertCounter("authorizationFailures", 15, serverSource);
129
130    mrpc.dequeuedCall(100);
131    mrpc.processedCall(101);
132    mrpc.totalCall(102);
133    HELPER.assertCounter("queueCallTime_NumOps", 1, serverSource);
134    HELPER.assertCounter("processCallTime_NumOps", 1, serverSource);
135    HELPER.assertCounter("totalCallTime_NumOps", 1, serverSource);
136
137    mrpc.sentBytes(103);
138    mrpc.sentBytes(103);
139    mrpc.sentBytes(103);
140
141    mrpc.receivedBytes(104);
142    mrpc.receivedBytes(104);
143
144    HELPER.assertCounter("sentBytes", 309, serverSource);
145    HELPER.assertCounter("receivedBytes", 208, serverSource);
146
147    mrpc.receivedRequest(105);
148    mrpc.sentResponse(106);
149    HELPER.assertCounter("requestSize_NumOps", 1, serverSource);
150    HELPER.assertCounter("responseSize_NumOps", 1, serverSource);
151
152    mrpc.exception(null);
153    HELPER.assertCounter("exceptions", 1, serverSource);
154
155    mrpc.exception(new RegionMovedException(ServerName.parseServerName("localhost:60020"), 100));
156    mrpc.exception(new RegionTooBusyException("Some region"));
157    mrpc.exception(new OutOfOrderScannerNextException());
158    mrpc.exception(new NotServingRegionException());
159    mrpc.exception(new CallDroppedException());
160    mrpc.exception(new RequestTooBigException());
161    mrpc.exception(new FakeException());
162    HELPER.assertCounter("exceptions.RegionMovedException", 1, serverSource);
163    HELPER.assertCounter("exceptions.RegionTooBusyException", 1, serverSource);
164    HELPER.assertCounter("exceptions.OutOfOrderScannerNextException", 1, serverSource);
165    HELPER.assertCounter("exceptions.NotServingRegionException", 1, serverSource);
166    HELPER.assertCounter("exceptions.callDropped", 1, serverSource);
167    HELPER.assertCounter("exceptions.requestTooBig", 1, serverSource);
168    HELPER.assertCounter("exceptions.otherExceptions", 1, serverSource);
169    HELPER.assertCounter("exceptions", 8, serverSource);
170  }
171
172  private class FakeException extends DoNotRetryIOException {
173
174    public FakeException() {
175      super();
176    }
177  }
178
179  @Test
180  public void testServerContextNameWithHostName() {
181    String[] masterServerNames = { "master/node-xyz/10.19.250.253:16020",
182      "master/node-regionserver-xyz/10.19.250.253:16020", "HMaster/node-xyz/10.19.250.253:16020",
183      "HMaster/node-regionserver-xyz/10.19.250.253:16020" };
184
185    String[] regionServerNames = { "regionserver/node-xyz/10.19.250.253:16020",
186      "regionserver/node-master1-xyz/10.19.250.253:16020",
187      "HRegionserver/node-xyz/10.19.250.253:16020",
188      "HRegionserver/node-master1-xyz/10.19.250.253:16020" };
189
190    MetricsHBaseServerSource masterSource = null;
191    for (String serverName : masterServerNames) {
192      masterSource =
193        new MetricsHBaseServer(serverName, new MetricsHBaseServerWrapperStub()).getMetricsSource();
194      assertEquals("master", masterSource.getMetricsContext());
195      assertEquals("Master,sub=IPC", masterSource.getMetricsJmxContext());
196      assertEquals("Master", masterSource.getMetricsName());
197    }
198
199    MetricsHBaseServerSource rsSource = null;
200    for (String serverName : regionServerNames) {
201      rsSource =
202        new MetricsHBaseServer(serverName, new MetricsHBaseServerWrapperStub()).getMetricsSource();
203      assertEquals("regionserver", rsSource.getMetricsContext());
204      assertEquals("RegionServer,sub=IPC", rsSource.getMetricsJmxContext());
205      assertEquals("RegionServer", rsSource.getMetricsName());
206    }
207  }
208}