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.jupiter.api.Assertions.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.NotServingRegionException;
026import org.apache.hadoop.hbase.RegionTooBusyException;
027import org.apache.hadoop.hbase.ServerName;
028import org.apache.hadoop.hbase.exceptions.OutOfOrderScannerNextException;
029import org.apache.hadoop.hbase.exceptions.RegionMovedException;
030import org.apache.hadoop.hbase.exceptions.RequestTooBigException;
031import org.apache.hadoop.hbase.test.MetricsAssertHelper;
032import org.apache.hadoop.hbase.testclassification.RPCTests;
033import org.apache.hadoop.hbase.testclassification.SmallTests;
034import org.junit.jupiter.api.Tag;
035import org.junit.jupiter.api.Test;
036
037@Tag(RPCTests.TAG)
038@Tag(SmallTests.TAG)
039public class TestRpcMetrics {
040
041  public MetricsAssertHelper HELPER = CompatibilityFactory.getInstance(MetricsAssertHelper.class);
042
043  @Test
044  public void testFactory() {
045    MetricsHBaseServer masterMetrics =
046      new MetricsHBaseServer("HMaster", new MetricsHBaseServerWrapperStub());
047    MetricsHBaseServerSource masterSource = masterMetrics.getMetricsSource();
048
049    MetricsHBaseServer rsMetrics =
050      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 =
070      new MetricsHBaseServer("HMaster", new MetricsHBaseServerWrapperStub());
071    MetricsHBaseServerSource serverSource = mrpc.getMetricsSource();
072    HELPER.assertGauge("queueSize", 101, serverSource);
073    HELPER.assertGauge("numCallsInGeneralQueue", 102, serverSource);
074    HELPER.assertGauge("numCallsInReplicationQueue", 103, serverSource);
075    HELPER.assertGauge("numCallsInPriorityQueue", 104, serverSource);
076    HELPER.assertGauge("numOpenConnections", 105, serverSource);
077    HELPER.assertGauge("numActiveHandler", 106, serverSource);
078    HELPER.assertGauge("numActiveGeneralHandler", 201, serverSource);
079    HELPER.assertGauge("numActivePriorityHandler", 202, serverSource);
080    HELPER.assertGauge("numActiveReplicationHandler", 203, serverSource);
081    HELPER.assertGauge("numActiveWriteHandler", 50, serverSource);
082    HELPER.assertGauge("numActiveReadHandler", 50, serverSource);
083    HELPER.assertGauge("numActiveScanHandler", 6, serverSource);
084    HELPER.assertGauge("numCallsInWriteQueue", 50, serverSource);
085    HELPER.assertGauge("numCallsInReadQueue", 50, serverSource);
086    HELPER.assertGauge("numCallsInScanQueue", 2, serverSource);
087    HELPER.assertGauge("nettyDirectMemoryUsage", 100, serverSource);
088    HELPER.assertGauge("nettyTotalPendingOutboundBytes", 100, serverSource);
089    HELPER.assertGauge("nettyMaxPendingOutboundBytes", 5, serverSource);
090  }
091
092  /**
093   * Test to make sure that all the actively called method on MetricsHBaseServer work.
094   */
095  @Test
096  public void testSourceMethods() {
097    MetricsHBaseServer mrpc =
098      new MetricsHBaseServer("HMaster", new MetricsHBaseServerWrapperStub());
099    MetricsHBaseServerSource serverSource = mrpc.getMetricsSource();
100
101    mrpc.unwritableTime(100);
102    mrpc.maxOutboundBytesExceeded();
103    mrpc.maxOutboundBytesExceeded();
104    HELPER.assertCounter("maxOutboundBytesExceeded", 2, serverSource);
105    HELPER.assertCounter("unwritableTime_NumOps", 1, serverSource);
106
107    for (int i = 0; i < 12; i++) {
108      mrpc.authenticationFailure();
109    }
110    for (int i = 0; i < 13; i++) {
111      mrpc.authenticationSuccess();
112    }
113    HELPER.assertCounter("authenticationFailures", 12, serverSource);
114    HELPER.assertCounter("authenticationSuccesses", 13, serverSource);
115
116    for (int i = 0; i < 14; i++) {
117      mrpc.authorizationSuccess();
118    }
119    for (int i = 0; i < 15; i++) {
120      mrpc.authorizationFailure();
121    }
122    HELPER.assertCounter("authorizationSuccesses", 14, serverSource);
123    HELPER.assertCounter("authorizationFailures", 15, serverSource);
124
125    mrpc.dequeuedCall(100);
126    mrpc.processedCall(101);
127    mrpc.totalCall(102);
128    HELPER.assertCounter("queueCallTime_NumOps", 1, serverSource);
129    HELPER.assertCounter("processCallTime_NumOps", 1, serverSource);
130    HELPER.assertCounter("totalCallTime_NumOps", 1, serverSource);
131
132    mrpc.sentBytes(103);
133    mrpc.sentBytes(103);
134    mrpc.sentBytes(103);
135
136    mrpc.receivedBytes(104);
137    mrpc.receivedBytes(104);
138
139    HELPER.assertCounter("sentBytes", 309, serverSource);
140    HELPER.assertCounter("receivedBytes", 208, serverSource);
141
142    mrpc.receivedRequest(105);
143    mrpc.sentResponse(106);
144    HELPER.assertCounter("requestSize_NumOps", 1, serverSource);
145    HELPER.assertCounter("responseSize_NumOps", 1, serverSource);
146
147    mrpc.exception(null);
148    HELPER.assertCounter("exceptions", 1, serverSource);
149
150    mrpc.exception(new RegionMovedException(ServerName.parseServerName("localhost:60020"), 100));
151    mrpc.exception(new RegionTooBusyException("Some region"));
152    mrpc.exception(new OutOfOrderScannerNextException());
153    mrpc.exception(new NotServingRegionException());
154    mrpc.exception(new CallDroppedException());
155    mrpc.exception(new RequestTooBigException());
156    mrpc.exception(new FakeException());
157    HELPER.assertCounter("exceptions.RegionMovedException", 1, serverSource);
158    HELPER.assertCounter("exceptions.RegionTooBusyException", 1, serverSource);
159    HELPER.assertCounter("exceptions.OutOfOrderScannerNextException", 1, serverSource);
160    HELPER.assertCounter("exceptions.NotServingRegionException", 1, serverSource);
161    HELPER.assertCounter("exceptions.callDropped", 1, serverSource);
162    HELPER.assertCounter("exceptions.requestTooBig", 1, serverSource);
163    HELPER.assertCounter("exceptions.otherExceptions", 1, serverSource);
164    HELPER.assertCounter("exceptions", 8, serverSource);
165  }
166
167  private class FakeException extends DoNotRetryIOException {
168
169    public FakeException() {
170      super();
171    }
172  }
173
174  @Test
175  public void testServerContextNameWithHostName() {
176    String[] masterServerNames = { "master/node-xyz/10.19.250.253:16020",
177      "master/node-regionserver-xyz/10.19.250.253:16020", "HMaster/node-xyz/10.19.250.253:16020",
178      "HMaster/node-regionserver-xyz/10.19.250.253:16020" };
179
180    String[] regionServerNames = { "regionserver/node-xyz/10.19.250.253:16020",
181      "regionserver/node-master1-xyz/10.19.250.253:16020",
182      "HRegionserver/node-xyz/10.19.250.253:16020",
183      "HRegionserver/node-master1-xyz/10.19.250.253:16020" };
184
185    MetricsHBaseServerSource masterSource = null;
186    for (String serverName : masterServerNames) {
187      masterSource =
188        new MetricsHBaseServer(serverName, new MetricsHBaseServerWrapperStub()).getMetricsSource();
189      assertEquals("master", masterSource.getMetricsContext());
190      assertEquals("Master,sub=IPC", masterSource.getMetricsJmxContext());
191      assertEquals("Master", masterSource.getMetricsName());
192    }
193
194    MetricsHBaseServerSource rsSource = null;
195    for (String serverName : regionServerNames) {
196      rsSource =
197        new MetricsHBaseServer(serverName, new MetricsHBaseServerWrapperStub()).getMetricsSource();
198      assertEquals("regionserver", rsSource.getMetricsContext());
199      assertEquals("RegionServer,sub=IPC", rsSource.getMetricsJmxContext());
200      assertEquals("RegionServer", rsSource.getMetricsName());
201    }
202  }
203}