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("numActiveGeneralHandler", 201, serverSource);
078    HELPER.assertGauge("numActivePriorityHandler", 202, serverSource);
079    HELPER.assertGauge("numActiveReplicationHandler", 203, serverSource);
080    HELPER.assertGauge("numActiveWriteHandler", 50, serverSource);
081    HELPER.assertGauge("numActiveReadHandler", 50, serverSource);
082    HELPER.assertGauge("numActiveScanHandler", 6, serverSource);
083    HELPER.assertGauge("numCallsInWriteQueue", 50, serverSource);
084    HELPER.assertGauge("numCallsInReadQueue", 50, serverSource);
085    HELPER.assertGauge("numCallsInScanQueue", 2, serverSource);
086  }
087
088  /**
089   * Test to make sure that all the actively called method on MetricsHBaseServer work.
090   */
091  @Test
092  public void testSourceMethods() {
093    MetricsHBaseServer mrpc = new MetricsHBaseServer("HMaster", new MetricsHBaseServerWrapperStub());
094    MetricsHBaseServerSource serverSource = mrpc.getMetricsSource();
095
096    for (int i=0; i < 12; i++) {
097      mrpc.authenticationFailure();
098    }
099    for (int i=0; i < 13; i++) {
100      mrpc.authenticationSuccess();
101    }
102    HELPER.assertCounter("authenticationFailures", 12, serverSource);
103    HELPER.assertCounter("authenticationSuccesses", 13, serverSource);
104
105
106
107    for (int i=0; i < 14; i++) {
108      mrpc.authorizationSuccess();
109    }
110    for (int i=0; i < 15; i++) {
111      mrpc.authorizationFailure();
112    }
113    HELPER.assertCounter("authorizationSuccesses", 14, serverSource);
114    HELPER.assertCounter("authorizationFailures", 15, serverSource);
115
116
117    mrpc.dequeuedCall(100);
118    mrpc.processedCall(101);
119    mrpc.totalCall(102);
120    HELPER.assertCounter("queueCallTime_NumOps", 1, serverSource);
121    HELPER.assertCounter("processCallTime_NumOps", 1, serverSource);
122    HELPER.assertCounter("totalCallTime_NumOps", 1, serverSource);
123
124    mrpc.sentBytes(103);
125    mrpc.sentBytes(103);
126    mrpc.sentBytes(103);
127
128    mrpc.receivedBytes(104);
129    mrpc.receivedBytes(104);
130
131    HELPER.assertCounter("sentBytes", 309, serverSource);
132    HELPER.assertCounter("receivedBytes", 208, serverSource);
133
134    mrpc.receivedRequest(105);
135    mrpc.sentResponse(106);
136    HELPER.assertCounter("requestSize_NumOps", 1, serverSource);
137    HELPER.assertCounter("responseSize_NumOps", 1, serverSource);
138
139    mrpc.exception(null);
140    HELPER.assertCounter("exceptions", 1, serverSource);
141
142    mrpc.exception(new RegionMovedException(ServerName.parseServerName("localhost:60020"), 100));
143    mrpc.exception(new RegionTooBusyException("Some region"));
144    mrpc.exception(new OutOfOrderScannerNextException());
145    mrpc.exception(new NotServingRegionException());
146    HELPER.assertCounter("exceptions.RegionMovedException", 1, serverSource);
147    HELPER.assertCounter("exceptions.RegionTooBusyException", 1, serverSource);
148    HELPER.assertCounter("exceptions.OutOfOrderScannerNextException", 1, serverSource);
149    HELPER.assertCounter("exceptions.NotServingRegionException", 1, serverSource);
150    HELPER.assertCounter("exceptions", 5, serverSource);
151  }
152
153  @Test
154  public void testServerContextNameWithHostName() {
155    String[] masterServerNames = { "master/node-xyz/10.19.250.253:16020",
156        "master/node-regionserver-xyz/10.19.250.253:16020", "HMaster/node-xyz/10.19.250.253:16020",
157        "HMaster/node-regionserver-xyz/10.19.250.253:16020" };
158
159    String[] regionServerNames = { "regionserver/node-xyz/10.19.250.253:16020",
160        "regionserver/node-master1-xyz/10.19.250.253:16020",
161        "HRegionserver/node-xyz/10.19.250.253:16020",
162        "HRegionserver/node-master1-xyz/10.19.250.253:16020" };
163
164    MetricsHBaseServerSource masterSource = null;
165    for (String serverName : masterServerNames) {
166      masterSource = new MetricsHBaseServer(serverName, new MetricsHBaseServerWrapperStub())
167          .getMetricsSource();
168      assertEquals("master", masterSource.getMetricsContext());
169      assertEquals("Master,sub=IPC", masterSource.getMetricsJmxContext());
170      assertEquals("Master", masterSource.getMetricsName());
171    }
172
173    MetricsHBaseServerSource rsSource = null;
174    for (String serverName : regionServerNames) {
175      rsSource = new MetricsHBaseServer(serverName, new MetricsHBaseServerWrapperStub())
176          .getMetricsSource();
177      assertEquals("regionserver", rsSource.getMetricsContext());
178      assertEquals("RegionServer,sub=IPC", rsSource.getMetricsJmxContext());
179      assertEquals("RegionServer", rsSource.getMetricsName());
180    }
181  }
182}
183