View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.client;
19  
20  import com.google.protobuf.ByteString;
21  import org.apache.hadoop.hbase.client.HConnectionManager.HConnectionImplementation;
22  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
23  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ClientService;
24  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.GetRequest;
25  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ScanRequest;
26  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MultiRequest;
27  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MutateRequest;
28  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MutationProto.MutationType;
29  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.RegionSpecifier;
30  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.RegionSpecifier.RegionSpecifierType;
31  import org.apache.hadoop.hbase.testclassification.SmallTests;
32  import org.apache.hadoop.hbase.util.Bytes;
33  import org.junit.AfterClass;
34  import org.junit.Assert;
35  import org.junit.BeforeClass;
36  import org.junit.Test;
37  import org.junit.experimental.categories.Category;
38  import org.mockito.Mockito;
39  
40  import java.io.IOException;
41  
42  @Category(SmallTests.class)
43  public class TestMetricsConnection {
44  
45    private static MetricsConnection METRICS;
46  
47    @BeforeClass
48    public static void beforeClass() {
49      HConnectionImplementation mocked = Mockito.mock(HConnectionImplementation.class);
50      Mockito.when(mocked.toString()).thenReturn("mocked-connection");
51      METRICS = new MetricsConnection(Mockito.mock(HConnectionImplementation.class));
52    }
53  
54    @AfterClass
55    public static void afterClass() {
56      METRICS.shutdown();
57    }
58  
59    @Test
60    public void testStaticMetrics() throws IOException {
61      final byte[] foo = Bytes.toBytes("foo");
62      final RegionSpecifier region = RegionSpecifier.newBuilder()
63          .setValue(ByteString.EMPTY)
64          .setType(RegionSpecifierType.REGION_NAME)
65          .build();
66      final int loop = 5;
67  
68      for (int i = 0; i < loop; i++) {
69        METRICS.updateRpc(
70            ClientService.getDescriptor().findMethodByName("Get"),
71            GetRequest.getDefaultInstance(),
72            MetricsConnection.newCallStats());
73        METRICS.updateRpc(
74            ClientService.getDescriptor().findMethodByName("Scan"),
75            ScanRequest.getDefaultInstance(),
76            MetricsConnection.newCallStats());
77        METRICS.updateRpc(
78            ClientService.getDescriptor().findMethodByName("Multi"),
79            MultiRequest.getDefaultInstance(),
80            MetricsConnection.newCallStats());
81        METRICS.updateRpc(
82            ClientService.getDescriptor().findMethodByName("Mutate"),
83            MutateRequest.newBuilder()
84                .setMutation(ProtobufUtil.toMutation(MutationType.APPEND, new Append(foo)))
85                .setRegion(region)
86                .build(),
87            MetricsConnection.newCallStats());
88        METRICS.updateRpc(
89            ClientService.getDescriptor().findMethodByName("Mutate"),
90            MutateRequest.newBuilder()
91                .setMutation(ProtobufUtil.toMutation(MutationType.DELETE, new Delete(foo)))
92                .setRegion(region)
93                .build(),
94            MetricsConnection.newCallStats());
95        METRICS.updateRpc(
96            ClientService.getDescriptor().findMethodByName("Mutate"),
97            MutateRequest.newBuilder()
98                .setMutation(ProtobufUtil.toMutation(MutationType.INCREMENT, new Increment(foo)))
99                .setRegion(region)
100               .build(),
101           MetricsConnection.newCallStats());
102       METRICS.updateRpc(
103           ClientService.getDescriptor().findMethodByName("Mutate"),
104           MutateRequest.newBuilder()
105               .setMutation(ProtobufUtil.toMutation(MutationType.PUT, new Put(foo)))
106               .setRegion(region)
107               .build(),
108           MetricsConnection.newCallStats());
109     }
110     for (MetricsConnection.CallTracker t : new MetricsConnection.CallTracker[] {
111         METRICS.getTracker, METRICS.scanTracker, METRICS.multiTracker, METRICS.appendTracker,
112         METRICS.deleteTracker, METRICS.incrementTracker, METRICS.putTracker
113     }) {
114       Assert.assertEquals("Failed to invoke callTimer on " + t, loop, t.callTimer.count());
115       Assert.assertEquals("Failed to invoke reqHist on " + t, loop, t.reqHist.count());
116       Assert.assertEquals("Failed to invoke respHist on " + t, loop, t.respHist.count());
117     }
118   }
119 }