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.regionserver; 019 020import static org.junit.Assert.assertEquals; 021 022import java.io.IOException; 023import org.apache.hadoop.conf.Configuration; 024import org.apache.hadoop.hbase.HBaseClassTestRule; 025import org.apache.hadoop.hbase.HBaseTestingUtility; 026import org.apache.hadoop.hbase.HConstants; 027import org.apache.hadoop.hbase.MiniHBaseCluster.MiniHBaseClusterRegionServer; 028import org.apache.hadoop.hbase.TableName; 029import org.apache.hadoop.hbase.client.Put; 030import org.apache.hadoop.hbase.client.Result; 031import org.apache.hadoop.hbase.client.ResultScanner; 032import org.apache.hadoop.hbase.client.Scan; 033import org.apache.hadoop.hbase.client.Table; 034import org.apache.hadoop.hbase.testclassification.MediumTests; 035import org.apache.hadoop.hbase.testclassification.RegionServerTests; 036import org.apache.hadoop.hbase.util.Bytes; 037import org.junit.AfterClass; 038import org.junit.BeforeClass; 039import org.junit.ClassRule; 040import org.junit.Rule; 041import org.junit.Test; 042import org.junit.experimental.categories.Category; 043import org.junit.rules.TestName; 044import org.slf4j.Logger; 045import org.slf4j.LoggerFactory; 046 047@Category({ RegionServerTests.class, MediumTests.class }) 048public class TestScannerRPCScanMetrics { 049 050 @ClassRule 051 public static final HBaseClassTestRule CLASS_RULE = 052 HBaseClassTestRule.forClass(TestScannerRPCScanMetrics.class); 053 054 private static final Logger LOG = LoggerFactory.getLogger(TestScannerRPCScanMetrics.class); 055 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 056 private static final byte[] FAMILY = Bytes.toBytes("testFamily"); 057 private static final byte[] QUALIFIER = Bytes.toBytes("testQualifier"); 058 private static final byte[] VALUE = Bytes.toBytes("testValue"); 059 060 @Rule 061 public TestName name = new TestName(); 062 063 @BeforeClass 064 public static void setupBeforeClass() throws Exception { 065 Configuration conf = TEST_UTIL.getConfiguration(); 066 conf.setStrings(HConstants.REGION_SERVER_IMPL, RegionServerWithScanMetrics.class.getName()); 067 TEST_UTIL.startMiniCluster(1); 068 } 069 070 @AfterClass 071 public static void tearDownAfterClass() throws Exception { 072 TEST_UTIL.shutdownMiniCluster(); 073 } 074 075 @Test 076 public void testScannerRPCScanMetrics() throws Exception { 077 final TableName tableName = TableName.valueOf(name.getMethodName()); 078 byte[][] splits = new byte[1][]; 079 splits[0] = Bytes.toBytes("row-4"); 080 Table ht = TEST_UTIL.createTable(tableName, FAMILY, splits); 081 byte[] r0 = Bytes.toBytes("row-0"); 082 byte[] r1 = Bytes.toBytes("row-1"); 083 byte[] r2 = Bytes.toBytes("row-2"); 084 byte[] r3 = Bytes.toBytes("row-3"); 085 putToTable(ht, r0); 086 putToTable(ht, r1); 087 putToTable(ht, r2); 088 putToTable(ht, r3); 089 LOG.info("Wrote our four table entries"); 090 Scan scan1 = new Scan(); 091 scan1.withStartRow(r0); 092 // This scan should not increment rpc full scan count (start row specified) 093 scan1.withStopRow(Bytes.toBytes("row-4")); 094 scanNextIterate(ht, scan1); 095 Scan scan2 = new Scan(); 096 scan2.withStartRow(r1); 097 // This scan should increment rpc full scan count by 1 (for second region only) 098 scanNextIterate(ht, scan2); 099 Scan scan3 = new Scan(); 100 scan3.withStopRow(Bytes.toBytes("row-5")); 101 // This scan should increment rpc full scan count by 1 (for firts region only) 102 scanNextIterate(ht, scan3); 103 Scan scan4 = new Scan(); 104 scan4.withStartRow(r1); 105 scan4.withStopRow(r2); 106 // This scan should not increment rpc full scan count (both start and stop row) 107 scanNextIterate(ht, scan4); 108 Scan dummyScan = new Scan(); 109 // This scan should increment rpc full scan count by 2 (both regions - no stop/start row) 110 scanNextIterate(ht, dummyScan); 111 112 RSRpcServices testClusterRSRPCServices = 113 TEST_UTIL.getMiniHBaseCluster().getRegionServer(0).rpcServices; 114 assertEquals(4, testClusterRSRPCServices.rpcFullScanRequestCount.intValue()); 115 } 116 117 private void putToTable(Table ht, byte[] rowkey) throws IOException { 118 Put put = new Put(rowkey); 119 put.addColumn(FAMILY, QUALIFIER, VALUE); 120 ht.put(put); 121 } 122 123 private void scanNextIterate(Table ht, Scan scan) throws Exception { 124 ResultScanner scanner = ht.getScanner(scan); 125 for (Result result = scanner.next(); result != null; result = scanner.next()) { 126 // Use the result object 127 } 128 scanner.close(); 129 } 130 131 private static class RegionServerWithScanMetrics extends MiniHBaseClusterRegionServer { 132 public RegionServerWithScanMetrics(Configuration conf) 133 throws IOException, InterruptedException { 134 super(conf); 135 } 136 137 protected RSRpcServices createRPCServices() throws IOException { 138 return new RSRPCServicesWithScanMetrics(this); 139 } 140 } 141 142 private static class RSRPCServicesWithScanMetrics extends RSRpcServices { 143 public long getScanRequestCount() { 144 return super.rpcScanRequestCount.longValue(); 145 } 146 147 public long getFullScanRequestCount() { 148 return super.rpcFullScanRequestCount.longValue(); 149 } 150 151 public RSRPCServicesWithScanMetrics(HRegionServer rs) throws IOException { 152 super(rs); 153 } 154 } 155 156}