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.HBaseTestingUtil;
026import org.apache.hadoop.hbase.HConstants;
027import org.apache.hadoop.hbase.SingleProcessHBaseCluster.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 HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
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
061  @Rule
062  public TestName name = new TestName();
063
064  @BeforeClass
065  public static void setupBeforeClass() throws Exception {
066    Configuration conf = TEST_UTIL.getConfiguration();
067    conf.setStrings(HConstants.REGION_SERVER_IMPL, RegionServerWithScanMetrics.class.getName());
068    TEST_UTIL.startMiniCluster(1);
069  }
070
071  @AfterClass
072  public static void tearDownAfterClass() throws Exception {
073    TEST_UTIL.shutdownMiniCluster();
074  }
075
076  @Test
077  public void testScannerRPCScanMetrics() throws Exception {
078    final TableName tableName = TableName.valueOf(name.getMethodName());
079    byte[][] splits = new byte[1][];
080    splits[0] = Bytes.toBytes("row-4");
081    Table ht = TEST_UTIL.createTable(tableName, FAMILY,splits);
082    byte[] r0 = Bytes.toBytes("row-0");
083    byte[] r1 = Bytes.toBytes("row-1");
084    byte[] r2 = Bytes.toBytes("row-2");
085    byte[] r3 = Bytes.toBytes("row-3");
086    putToTable(ht, r0);
087    putToTable(ht, r1);
088    putToTable(ht, r2);
089    putToTable(ht, r3);
090    LOG.info("Wrote our four table entries");
091    Scan scan1 = new Scan();
092    scan1.withStartRow(r0);
093    // This scan should not increment rpc full scan count (start row specified)
094    scan1.withStopRow(Bytes.toBytes("row-4"));
095    scanNextIterate(ht, scan1);
096    Scan scan2 = new Scan();
097    scan2.withStartRow(r1);
098    // This scan should increment rpc full scan count by 1 (for second region only)
099    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);
112
113    RSRpcServices testClusterRSRPCServices = TEST_UTIL.getMiniHBaseCluster().getRegionServer(0)
114      .getRpcServices();
115    assertEquals(4, testClusterRSRPCServices.rpcFullScanRequestCount.intValue());
116  }
117
118  private void putToTable(Table ht, byte[] rowkey) throws IOException {
119    Put put = new Put(rowkey);
120    put.addColumn(FAMILY, QUALIFIER, VALUE);
121    ht.put(put);
122  }
123
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())
127    {
128      // Use the result object
129    }
130    scanner.close();
131  }
132
133  private static class RegionServerWithScanMetrics extends MiniHBaseClusterRegionServer {
134    public RegionServerWithScanMetrics(Configuration conf)
135      throws IOException, InterruptedException {
136      super(conf);
137    }
138
139    protected RSRpcServices createRPCServices() throws IOException {
140      return new RSRPCServicesWithScanMetrics(this);
141    }
142  }
143  private static class RSRPCServicesWithScanMetrics extends RSRpcServices {
144    public long getScanRequestCount() {
145      return super.rpcScanRequestCount.longValue();
146    }
147    public long getFullScanRequestCount() {
148      return super.rpcFullScanRequestCount.longValue();
149    }
150    public RSRPCServicesWithScanMetrics(HRegionServer rs)
151      throws IOException {
152      super(rs);
153    }
154  }
155
156}