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