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.*;
021
022import org.apache.hadoop.hbase.CompatibilityFactory;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.NotServingRegionException;
025import org.apache.hadoop.hbase.RegionTooBusyException;
026import org.apache.hadoop.hbase.ServerName;
027import org.apache.hadoop.hbase.exceptions.OutOfOrderScannerNextException;
028import org.apache.hadoop.hbase.exceptions.RegionMovedException;
029import org.apache.hadoop.hbase.test.MetricsAssertHelper;
030import org.apache.hadoop.hbase.testclassification.RPCTests;
031import org.apache.hadoop.hbase.testclassification.SmallTests;
032import org.junit.ClassRule;
033import org.junit.Test;
034import org.junit.experimental.categories.Category;
035
036@Category({RPCTests.class, SmallTests.class})
037public class TestRpcMetrics {
038
039  @ClassRule
040  public static final HBaseClassTestRule CLASS_RULE =
041      HBaseClassTestRule.forClass(TestRpcMetrics.class);
042
043  public MetricsAssertHelper HELPER = CompatibilityFactory.getInstance(MetricsAssertHelper.class);
044
045  @Test
046  public void testFactory() {
047    MetricsHBaseServer masterMetrics = new MetricsHBaseServer("HMaster", new MetricsHBaseServerWrapperStub());
048    MetricsHBaseServerSource masterSource = masterMetrics.getMetricsSource();
049
050    MetricsHBaseServer rsMetrics = new MetricsHBaseServer("HRegionServer", new MetricsHBaseServerWrapperStub());
051    MetricsHBaseServerSource rsSource = rsMetrics.getMetricsSource();
052
053    assertEquals("master", masterSource.getMetricsContext());
054    assertEquals("regionserver", rsSource.getMetricsContext());
055
056    assertEquals("Master,sub=IPC", masterSource.getMetricsJmxContext());
057    assertEquals("RegionServer,sub=IPC", rsSource.getMetricsJmxContext());
058
059    assertEquals("Master", masterSource.getMetricsName());
060    assertEquals("RegionServer", rsSource.getMetricsName());
061  }
062
063  /**
064   * This test makes sure that the numbers from a MetricsHBaseServerWrapper are correctly exported
065   * to hadoop metrics 2 system.
066   */
067  @Test
068  public void testWrapperSource() {
069    MetricsHBaseServer mrpc = new MetricsHBaseServer("HMaster", new MetricsHBaseServerWrapperStub());
070    MetricsHBaseServerSource serverSource = mrpc.getMetricsSource();
071    HELPER.assertGauge("queueSize", 101, serverSource);
072    HELPER.assertGauge("numCallsInGeneralQueue", 102, serverSource);
073    HELPER.assertGauge("numCallsInReplicationQueue", 103, serverSource);
074    HELPER.assertGauge("numCallsInPriorityQueue", 104, serverSource);
075    HELPER.assertGauge("numOpenConnections", 105, serverSource);
076    HELPER.assertGauge("numActiveHandler", 106, serverSource);
077    HELPER.assertGauge("numActiveWriteHandler", 50, serverSource);
078    HELPER.assertGauge("numActiveReadHandler", 50, serverSource);
079    HELPER.assertGauge("numActiveScanHandler", 6, serverSource);
080    HELPER.assertGauge("numCallsInWriteQueue", 50, serverSource);
081    HELPER.assertGauge("numCallsInReadQueue", 50, serverSource);
082    HELPER.assertGauge("numCallsInScanQueue", 2, serverSource);
083  }
084
085  /**
086   * Test to make sure that all the actively called method on MetricsHBaseServer work.
087   */
088  @Test
089  public void testSourceMethods() {
090    MetricsHBaseServer mrpc = new MetricsHBaseServer("HMaster", new MetricsHBaseServerWrapperStub());
091    MetricsHBaseServerSource serverSource = mrpc.getMetricsSource();
092
093    for (int i=0; i < 12; i++) {
094      mrpc.authenticationFailure();
095    }
096    for (int i=0; i < 13; i++) {
097      mrpc.authenticationSuccess();
098    }
099    HELPER.assertCounter("authenticationFailures", 12, serverSource);
100    HELPER.assertCounter("authenticationSuccesses", 13, serverSource);
101
102
103
104    for (int i=0; i < 14; i++) {
105      mrpc.authorizationSuccess();
106    }
107    for (int i=0; i < 15; i++) {
108      mrpc.authorizationFailure();
109    }
110    HELPER.assertCounter("authorizationSuccesses", 14, serverSource);
111    HELPER.assertCounter("authorizationFailures", 15, serverSource);
112
113
114    mrpc.dequeuedCall(100);
115    mrpc.processedCall(101);
116    mrpc.totalCall(102);
117    HELPER.assertCounter("queueCallTime_NumOps", 1, serverSource);
118    HELPER.assertCounter("processCallTime_NumOps", 1, serverSource);
119    HELPER.assertCounter("totalCallTime_NumOps", 1, serverSource);
120
121    mrpc.sentBytes(103);
122    mrpc.sentBytes(103);
123    mrpc.sentBytes(103);
124
125    mrpc.receivedBytes(104);
126    mrpc.receivedBytes(104);
127
128    HELPER.assertCounter("sentBytes", 309, serverSource);
129    HELPER.assertCounter("receivedBytes", 208, serverSource);
130
131    mrpc.receivedRequest(105);
132    mrpc.sentResponse(106);
133    HELPER.assertCounter("requestSize_NumOps", 1, serverSource);
134    HELPER.assertCounter("responseSize_NumOps", 1, serverSource);
135
136    mrpc.exception(null);
137    HELPER.assertCounter("exceptions", 1, serverSource);
138
139    mrpc.exception(new RegionMovedException(ServerName.parseServerName("localhost:60020"), 100));
140    mrpc.exception(new RegionTooBusyException("Some region"));
141    mrpc.exception(new OutOfOrderScannerNextException());
142    mrpc.exception(new NotServingRegionException());
143    HELPER.assertCounter("exceptions.RegionMovedException", 1, serverSource);
144    HELPER.assertCounter("exceptions.RegionTooBusyException", 1, serverSource);
145    HELPER.assertCounter("exceptions.OutOfOrderScannerNextException", 1, serverSource);
146    HELPER.assertCounter("exceptions.NotServingRegionException", 1, serverSource);
147    HELPER.assertCounter("exceptions", 5, serverSource);
148  }
149
150  @Test
151  public void testServerContextNameWithHostName() {
152    String[] masterServerNames = { "master/node-xyz/10.19.250.253:16020",
153        "master/node-regionserver-xyz/10.19.250.253:16020", "HMaster/node-xyz/10.19.250.253:16020",
154        "HMaster/node-regionserver-xyz/10.19.250.253:16020" };
155
156    String[] regionServerNames = { "regionserver/node-xyz/10.19.250.253:16020",
157        "regionserver/node-master1-xyz/10.19.250.253:16020",
158        "HRegionserver/node-xyz/10.19.250.253:16020",
159        "HRegionserver/node-master1-xyz/10.19.250.253:16020" };
160
161    MetricsHBaseServerSource masterSource = null;
162    for (String serverName : masterServerNames) {
163      masterSource = new MetricsHBaseServer(serverName, new MetricsHBaseServerWrapperStub())
164          .getMetricsSource();
165      assertEquals("master", masterSource.getMetricsContext());
166      assertEquals("Master,sub=IPC", masterSource.getMetricsJmxContext());
167      assertEquals("Master", masterSource.getMetricsName());
168    }
169
170    MetricsHBaseServerSource rsSource = null;
171    for (String serverName : regionServerNames) {
172      rsSource = new MetricsHBaseServer(serverName, new MetricsHBaseServerWrapperStub())
173          .getMetricsSource();
174      assertEquals("regionserver", rsSource.getMetricsContext());
175      assertEquals("RegionServer,sub=IPC", rsSource.getMetricsJmxContext());
176      assertEquals("RegionServer", rsSource.getMetricsName());
177    }
178  }
179}
180