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