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