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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org
.apache
.hadoop
.hbase
.regionserver
;
20 import static org
.junit
.Assert
.assertEquals
;
22 import java
.io
.IOException
;
23 import org
.apache
.hadoop
.conf
.Configuration
;
24 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
25 import org
.apache
.hadoop
.hbase
.HBaseTestingUtil
;
26 import org
.apache
.hadoop
.hbase
.HConstants
;
27 import org
.apache
.hadoop
.hbase
.SingleProcessHBaseCluster
.MiniHBaseClusterRegionServer
;
28 import org
.apache
.hadoop
.hbase
.TableName
;
29 import org
.apache
.hadoop
.hbase
.client
.Put
;
30 import org
.apache
.hadoop
.hbase
.client
.Result
;
31 import org
.apache
.hadoop
.hbase
.client
.ResultScanner
;
32 import org
.apache
.hadoop
.hbase
.client
.Scan
;
33 import org
.apache
.hadoop
.hbase
.client
.Table
;
34 import org
.apache
.hadoop
.hbase
.testclassification
.MediumTests
;
35 import org
.apache
.hadoop
.hbase
.testclassification
.RegionServerTests
;
36 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
37 import org
.junit
.AfterClass
;
38 import org
.junit
.BeforeClass
;
39 import org
.junit
.ClassRule
;
40 import org
.junit
.Rule
;
41 import org
.junit
.Test
;
42 import org
.junit
.experimental
.categories
.Category
;
43 import org
.junit
.rules
.TestName
;
44 import org
.slf4j
.Logger
;
45 import org
.slf4j
.LoggerFactory
;
47 @Category({ RegionServerTests
.class, MediumTests
.class})
48 public class TestScannerRPCScanMetrics
{
51 public static final HBaseClassTestRule CLASS_RULE
=
52 HBaseClassTestRule
.forClass(TestScannerRPCScanMetrics
.class);
54 private static final Logger LOG
= LoggerFactory
.getLogger(TestScannerRPCScanMetrics
.class);
55 private final static HBaseTestingUtil TEST_UTIL
= new HBaseTestingUtil();
56 private static final byte[] FAMILY
= Bytes
.toBytes("testFamily");
57 private static final byte[] QUALIFIER
= Bytes
.toBytes("testQualifier");
58 private static final byte[] VALUE
= Bytes
.toBytes("testValue");
62 public TestName name
= new TestName();
65 public static void setupBeforeClass() throws Exception
{
66 Configuration conf
= TEST_UTIL
.getConfiguration();
67 conf
.setStrings(HConstants
.REGION_SERVER_IMPL
, RegionServerWithScanMetrics
.class.getName());
68 TEST_UTIL
.startMiniCluster(1);
72 public static void tearDownAfterClass() throws Exception
{
73 TEST_UTIL
.shutdownMiniCluster();
77 public void testScannerRPCScanMetrics() throws Exception
{
78 final TableName tableName
= TableName
.valueOf(name
.getMethodName());
79 byte[][] splits
= new byte[1][];
80 splits
[0] = Bytes
.toBytes("row-4");
81 Table ht
= TEST_UTIL
.createTable(tableName
, FAMILY
,splits
);
82 byte[] r0
= Bytes
.toBytes("row-0");
83 byte[] r1
= Bytes
.toBytes("row-1");
84 byte[] r2
= Bytes
.toBytes("row-2");
85 byte[] r3
= Bytes
.toBytes("row-3");
90 LOG
.info("Wrote our four table entries");
91 Scan scan1
= new Scan();
92 scan1
.withStartRow(r0
);
93 // This scan should not increment rpc full scan count (start row specified)
94 scan1
.withStopRow(Bytes
.toBytes("row-4"));
95 scanNextIterate(ht
, scan1
);
96 Scan scan2
= new Scan();
97 scan2
.withStartRow(r1
);
98 // This scan should increment rpc full scan count by 1 (for second region only)
99 scanNextIterate(ht
, scan2
);
100 Scan scan3
= new Scan();
101 scan3
.withStopRow(Bytes
.toBytes("row-5"));
102 // This scan should increment rpc full scan count by 1 (for firts region only)
103 scanNextIterate(ht
, scan3
);
104 Scan scan4
= new Scan();
105 scan4
.withStartRow(r1
);
106 scan4
.withStopRow(r2
);
107 // This scan should not increment rpc full scan count (both start and stop row)
108 scanNextIterate(ht
, scan4
);
109 Scan dummyScan
= new Scan();
110 // This scan should increment rpc full scan count by 2 (both regions - no stop/start row)
111 scanNextIterate(ht
, dummyScan
);
113 RSRpcServices testClusterRSRPCServices
= TEST_UTIL
.getMiniHBaseCluster().getRegionServer(0)
115 assertEquals(4, testClusterRSRPCServices
.rpcFullScanRequestCount
.intValue());
118 private void putToTable(Table ht
, byte[] rowkey
) throws IOException
{
119 Put put
= new Put(rowkey
);
120 put
.addColumn(FAMILY
, QUALIFIER
, VALUE
);
124 private void scanNextIterate(Table ht
, Scan scan
) throws Exception
{
125 ResultScanner scanner
= ht
.getScanner(scan
);
126 for (Result result
= scanner
.next(); result
!= null; result
= scanner
.next())
128 // Use the result object
133 private static class RegionServerWithScanMetrics
extends MiniHBaseClusterRegionServer
{
134 public RegionServerWithScanMetrics(Configuration conf
)
135 throws IOException
, InterruptedException
{
139 protected RSRpcServices
createRPCServices() throws IOException
{
140 return new RSRPCServicesWithScanMetrics(this);
143 private static class RSRPCServicesWithScanMetrics
extends RSRpcServices
{
144 public long getScanRequestCount() {
145 return super.rpcScanRequestCount
.longValue();
147 public long getFullScanRequestCount() {
148 return super.rpcFullScanRequestCount
.longValue();
150 public RSRPCServicesWithScanMetrics(HRegionServer rs
)