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.ConnectionManager.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.MetricsTests;
32 import org.apache.hadoop.hbase.testclassification.SmallTests;
33 import org.apache.hadoop.hbase.util.Bytes;
34 import org.junit.AfterClass;
35 import org.junit.Assert;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.junit.experimental.categories.Category;
39 import org.mockito.Mockito;
40 
41 import java.io.IOException;
42 
43 @Category({MetricsTests.class, SmallTests.class})
44 public class TestMetricsConnection {
45 
46   private static MetricsConnection METRICS;
47 
48   @BeforeClass
beforeClass()49   public static void beforeClass() {
50     HConnectionImplementation mocked = Mockito.mock(HConnectionImplementation.class);
51     Mockito.when(mocked.toString()).thenReturn("mocked-connection");
52     METRICS = new MetricsConnection(Mockito.mock(HConnectionImplementation.class));
53   }
54 
55   @AfterClass
afterClass()56   public static void afterClass() {
57     METRICS.shutdown();
58   }
59 
60   @Test
testStaticMetrics()61   public void testStaticMetrics() throws IOException {
62     final byte[] foo = Bytes.toBytes("foo");
63     final RegionSpecifier region = RegionSpecifier.newBuilder()
64         .setValue(ByteString.EMPTY)
65         .setType(RegionSpecifierType.REGION_NAME)
66         .build();
67     final int loop = 5;
68 
69     for (int i = 0; i < loop; i++) {
70       METRICS.updateRpc(
71           ClientService.getDescriptor().findMethodByName("Get"),
72           GetRequest.getDefaultInstance(),
73           MetricsConnection.newCallStats());
74       METRICS.updateRpc(
75           ClientService.getDescriptor().findMethodByName("Scan"),
76           ScanRequest.getDefaultInstance(),
77           MetricsConnection.newCallStats());
78       METRICS.updateRpc(
79           ClientService.getDescriptor().findMethodByName("Multi"),
80           MultiRequest.getDefaultInstance(),
81           MetricsConnection.newCallStats());
82       METRICS.updateRpc(
83           ClientService.getDescriptor().findMethodByName("Mutate"),
84           MutateRequest.newBuilder()
85               .setMutation(ProtobufUtil.toMutation(MutationType.APPEND, new Append(foo)))
86               .setRegion(region)
87               .build(),
88           MetricsConnection.newCallStats());
89       METRICS.updateRpc(
90           ClientService.getDescriptor().findMethodByName("Mutate"),
91           MutateRequest.newBuilder()
92               .setMutation(ProtobufUtil.toMutation(MutationType.DELETE, new Delete(foo)))
93               .setRegion(region)
94               .build(),
95           MetricsConnection.newCallStats());
96       METRICS.updateRpc(
97           ClientService.getDescriptor().findMethodByName("Mutate"),
98           MutateRequest.newBuilder()
99               .setMutation(ProtobufUtil.toMutation(MutationType.INCREMENT, new Increment(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.PUT, new Put(foo)))
107               .setRegion(region)
108               .build(),
109           MetricsConnection.newCallStats());
110     }
111     for (MetricsConnection.CallTracker t : new MetricsConnection.CallTracker[] {
112         METRICS.getTracker, METRICS.scanTracker, METRICS.multiTracker, METRICS.appendTracker,
113         METRICS.deleteTracker, METRICS.incrementTracker, METRICS.putTracker
114     }) {
115       Assert.assertEquals("Failed to invoke callTimer on " + t, loop, t.callTimer.count());
116       Assert.assertEquals("Failed to invoke reqHist on " + t, loop, t.reqHist.count());
117       Assert.assertEquals("Failed to invoke respHist on " + t, loop, t.respHist.count());
118     }
119   }
120 }
121