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.client; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertTrue; 022 023import com.codahale.metrics.RatioGauge; 024import com.codahale.metrics.RatioGauge.Ratio; 025import java.io.IOException; 026import java.util.concurrent.Executors; 027import java.util.concurrent.ThreadPoolExecutor; 028import org.apache.hadoop.hbase.HBaseClassTestRule; 029import org.apache.hadoop.hbase.testclassification.ClientTests; 030import org.apache.hadoop.hbase.testclassification.MetricsTests; 031import org.apache.hadoop.hbase.testclassification.SmallTests; 032import org.apache.hadoop.hbase.util.Bytes; 033import org.junit.AfterClass; 034import org.junit.BeforeClass; 035import org.junit.ClassRule; 036import org.junit.Test; 037import org.junit.experimental.categories.Category; 038 039import org.apache.hbase.thirdparty.com.google.protobuf.ByteString; 040 041import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil; 042import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ClientService; 043import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.GetRequest; 044import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MultiRequest; 045import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutateRequest; 046import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutationProto.MutationType; 047import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ScanRequest; 048import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.RegionSpecifier; 049import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.RegionSpecifier.RegionSpecifierType; 050 051@Category({ClientTests.class, MetricsTests.class, SmallTests.class}) 052public class TestMetricsConnection { 053 @ClassRule 054 public static final HBaseClassTestRule CLASS_RULE = 055 HBaseClassTestRule.forClass(TestMetricsConnection.class); 056 057 private static MetricsConnection METRICS; 058 private static final ThreadPoolExecutor BATCH_POOL = 059 (ThreadPoolExecutor) Executors.newFixedThreadPool(2); 060 @BeforeClass 061 public static void beforeClass() { 062 METRICS = new MetricsConnection("mocked-connection", () -> BATCH_POOL, () -> null); 063 } 064 065 @AfterClass 066 public static void afterClass() { 067 METRICS.shutdown(); 068 } 069 070 @Test 071 public void testStaticMetrics() throws IOException { 072 final byte[] foo = Bytes.toBytes("foo"); 073 final RegionSpecifier region = RegionSpecifier.newBuilder() 074 .setValue(ByteString.EMPTY) 075 .setType(RegionSpecifierType.REGION_NAME) 076 .build(); 077 final int loop = 5; 078 079 for (int i = 0; i < loop; i++) { 080 METRICS.updateRpc( 081 ClientService.getDescriptor().findMethodByName("Get"), 082 GetRequest.getDefaultInstance(), 083 MetricsConnection.newCallStats()); 084 METRICS.updateRpc( 085 ClientService.getDescriptor().findMethodByName("Scan"), 086 ScanRequest.getDefaultInstance(), 087 MetricsConnection.newCallStats()); 088 METRICS.updateRpc( 089 ClientService.getDescriptor().findMethodByName("Multi"), 090 MultiRequest.getDefaultInstance(), 091 MetricsConnection.newCallStats()); 092 METRICS.updateRpc( 093 ClientService.getDescriptor().findMethodByName("Mutate"), 094 MutateRequest.newBuilder() 095 .setMutation(ProtobufUtil.toMutation(MutationType.APPEND, new Append(foo))) 096 .setRegion(region) 097 .build(), 098 MetricsConnection.newCallStats()); 099 METRICS.updateRpc( 100 ClientService.getDescriptor().findMethodByName("Mutate"), 101 MutateRequest.newBuilder() 102 .setMutation(ProtobufUtil.toMutation(MutationType.DELETE, new Delete(foo))) 103 .setRegion(region) 104 .build(), 105 MetricsConnection.newCallStats()); 106 METRICS.updateRpc( 107 ClientService.getDescriptor().findMethodByName("Mutate"), 108 MutateRequest.newBuilder() 109 .setMutation(ProtobufUtil.toMutation(MutationType.INCREMENT, new Increment(foo))) 110 .setRegion(region) 111 .build(), 112 MetricsConnection.newCallStats()); 113 METRICS.updateRpc( 114 ClientService.getDescriptor().findMethodByName("Mutate"), 115 MutateRequest.newBuilder() 116 .setMutation(ProtobufUtil.toMutation(MutationType.PUT, new Put(foo))) 117 .setRegion(region) 118 .build(), 119 MetricsConnection.newCallStats()); 120 } 121 for (String method: new String[]{"Get", "Scan", "Mutate"}) { 122 final String metricKey = "rpcCount_" + ClientService.getDescriptor().getName() + "_" + method; 123 final long metricVal = METRICS.rpcCounters.get(metricKey).getCount(); 124 assertTrue("metric: " + metricKey + " val: " + metricVal, metricVal >= loop); 125 } 126 for (MetricsConnection.CallTracker t : new MetricsConnection.CallTracker[] { 127 METRICS.getTracker, METRICS.scanTracker, METRICS.multiTracker, METRICS.appendTracker, 128 METRICS.deleteTracker, METRICS.incrementTracker, METRICS.putTracker 129 }) { 130 assertEquals("Failed to invoke callTimer on " + t, loop, t.callTimer.getCount()); 131 assertEquals("Failed to invoke reqHist on " + t, loop, t.reqHist.getCount()); 132 assertEquals("Failed to invoke respHist on " + t, loop, t.respHist.getCount()); 133 } 134 RatioGauge executorMetrics = (RatioGauge) METRICS.getMetricRegistry() 135 .getMetrics().get(METRICS.getExecutorPoolName()); 136 RatioGauge metaMetrics = (RatioGauge) METRICS.getMetricRegistry() 137 .getMetrics().get(METRICS.getMetaPoolName()); 138 assertEquals(Ratio.of(0, 3).getValue(), executorMetrics.getValue(), 0); 139 assertEquals(Double.NaN, metaMetrics.getValue(), 0); 140 } 141}