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  }
093
094  /**
095   * Test to make sure that all the actively called method on MetricsHBaseServer work.
096   */
097  @Test
098  public void testSourceMethods() {
099    MetricsHBaseServer mrpc =
100      new MetricsHBaseServer("HMaster", new MetricsHBaseServerWrapperStub());
101    MetricsHBaseServerSource serverSource = mrpc.getMetricsSource();
102
103    for (int i = 0; i < 12; i++) {
104      mrpc.authenticationFailure();
105    }
106    for (int i = 0; i < 13; i++) {
107      mrpc.authenticationSuccess();
108    }
109    HELPER.assertCounter("authenticationFailures", 12, serverSource);
110    HELPER.assertCounter("authenticationSuccesses", 13, serverSource);
111
112    for (int i = 0; i < 14; i++) {
113      mrpc.authorizationSuccess();
114    }
115    for (int i = 0; i < 15; i++) {
116      mrpc.authorizationFailure();
117    }
118    HELPER.assertCounter("authorizationSuccesses", 14, serverSource);
119    HELPER.assertCounter("authorizationFailures", 15, serverSource);
120
121    mrpc.dequeuedCall(100);
122    mrpc.processedCall(101);
123    mrpc.totalCall(102);
124    HELPER.assertCounter("queueCallTime_NumOps", 1, serverSource);
125    HELPER.assertCounter("processCallTime_NumOps", 1, serverSource);
126    HELPER.assertCounter("totalCallTime_NumOps", 1, serverSource);
127
128    mrpc.sentBytes(103);
129    mrpc.sentBytes(103);
130    mrpc.sentBytes(103);
131
132    mrpc.receivedBytes(104);
133    mrpc.receivedBytes(104);
134
135    HELPER.assertCounter("sentBytes", 309, serverSource);
136    HELPER.assertCounter("receivedBytes", 208, serverSource);
137
138    mrpc.receivedRequest(105);
139    mrpc.sentResponse(106);
140    HELPER.assertCounter("requestSize_NumOps", 1, serverSource);
141    HELPER.assertCounter("responseSize_NumOps", 1, serverSource);
142
143    mrpc.exception(null);
144    HELPER.assertCounter("exceptions", 1, serverSource);
145
146    mrpc.exception(new RegionMovedException(ServerName.parseServerName("localhost:60020"), 100));
147    mrpc.exception(new RegionTooBusyException("Some region"));
148    mrpc.exception(new OutOfOrderScannerNextException());
149    mrpc.exception(new NotServingRegionException());
150    mrpc.exception(new CallDroppedException());
151    mrpc.exception(new RequestTooBigException());
152    mrpc.exception(new FakeException());
153    HELPER.assertCounter("exceptions.RegionMovedException", 1, serverSource);
154    HELPER.assertCounter("exceptions.RegionTooBusyException", 1, serverSource);
155    HELPER.assertCounter("exceptions.OutOfOrderScannerNextException", 1, serverSource);
156    HELPER.assertCounter("exceptions.NotServingRegionException", 1, serverSource);
157    HELPER.assertCounter("exceptions.callDropped", 1, serverSource);
158    HELPER.assertCounter("exceptions.requestTooBig", 1, serverSource);
159    HELPER.assertCounter("exceptions.otherExceptions", 1, serverSource);
160    HELPER.assertCounter("exceptions", 8, serverSource);
161  }
162
163  private class FakeException extends DoNotRetryIOException {
164
165    public FakeException() {
166      super();
167    }
168  }
169
170  @Test
171  public void testServerContextNameWithHostName() {
172    String[] masterServerNames = { "master/node-xyz/10.19.250.253:16020",
173      "master/node-regionserver-xyz/10.19.250.253:16020", "HMaster/node-xyz/10.19.250.253:16020",
174      "HMaster/node-regionserver-xyz/10.19.250.253:16020" };
175
176    String[] regionServerNames = { "regionserver/node-xyz/10.19.250.253:16020",
177      "regionserver/node-master1-xyz/10.19.250.253:16020",
178      "HRegionserver/node-xyz/10.19.250.253:16020",
179      "HRegionserver/node-master1-xyz/10.19.250.253:16020" };
180
181    MetricsHBaseServerSource masterSource = null;
182    for (String serverName : masterServerNames) {
183      masterSource =
184        new MetricsHBaseServer(serverName, new MetricsHBaseServerWrapperStub()).getMetricsSource();
185      assertEquals("master", masterSource.getMetricsContext());
186      assertEquals("Master,sub=IPC", masterSource.getMetricsJmxContext());
187      assertEquals("Master", masterSource.getMetricsName());
188    }
189
190    MetricsHBaseServerSource rsSource = null;
191    for (String serverName : regionServerNames) {
192      rsSource =
193        new MetricsHBaseServer(serverName, new MetricsHBaseServerWrapperStub()).getMetricsSource();
194      assertEquals("regionserver", rsSource.getMetricsContext());
195      assertEquals("RegionServer,sub=IPC", rsSource.getMetricsJmxContext());
196      assertEquals("RegionServer", rsSource.getMetricsName());
197    }
198  }
199}