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.client;
019
020import static org.junit.Assert.assertNotNull;
021import static org.junit.Assert.assertNull;
022import static org.junit.Assert.assertTrue;
023
024import java.io.IOException;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.fs.FileSystem;
027import org.apache.hadoop.fs.Path;
028import org.apache.hadoop.hbase.HBaseClassTestRule;
029import org.apache.hadoop.hbase.HBaseTestingUtility;
030import org.apache.hadoop.hbase.HConstants;
031import org.apache.hadoop.hbase.TableName;
032import org.apache.hadoop.hbase.io.hfile.BlockCache;
033import org.apache.hadoop.hbase.io.hfile.IndexOnlyLruBlockCache;
034import org.apache.hadoop.hbase.testclassification.ClientTests;
035import org.apache.hadoop.hbase.testclassification.SmallTests;
036import org.junit.AfterClass;
037import org.junit.Before;
038import org.junit.BeforeClass;
039import org.junit.ClassRule;
040import org.junit.Test;
041import org.junit.experimental.categories.Category;
042
043@Category({ SmallTests.class, ClientTests.class })
044public class TestClientSideRegionScanner {
045  @ClassRule
046  public static final HBaseClassTestRule CLASS_RULE =
047    HBaseClassTestRule.forClass(TestClientSideRegionScanner.class);
048
049  private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
050
051  private Configuration conf;
052  private Path rootDir;
053  private FileSystem fs;
054  private TableDescriptor htd;
055  private RegionInfo hri;
056  private Scan scan;
057
058  @BeforeClass
059  public static void setUpBeforeClass() throws Exception {
060    TEST_UTIL.startMiniCluster(1);
061  }
062
063  @AfterClass
064  public static void tearDownAfterClass() throws Exception {
065    TEST_UTIL.shutdownMiniCluster();
066  }
067
068  @Before
069  public void setup() throws IOException {
070    conf = TEST_UTIL.getConfiguration();
071    rootDir = TEST_UTIL.getDefaultRootDirPath();
072    fs = TEST_UTIL.getTestFileSystem();
073    htd = TEST_UTIL.getAdmin().getDescriptor(TableName.META_TABLE_NAME);
074    hri = TEST_UTIL.getAdmin().getRegions(TableName.META_TABLE_NAME).get(0);
075    scan = new Scan();
076  }
077
078  @Test
079  public void testDefaultBlockCache() throws IOException {
080    Configuration copyConf = new Configuration(conf);
081    ClientSideRegionScanner clientSideRegionScanner =
082      new ClientSideRegionScanner(copyConf, fs, rootDir, htd, hri, scan, null);
083
084    BlockCache blockCache = clientSideRegionScanner.getRegion().getBlockCache();
085    assertNotNull(blockCache);
086    assertTrue(blockCache instanceof IndexOnlyLruBlockCache);
087    assertTrue(HConstants.HBASE_CLIENT_SCANNER_ONHEAP_BLOCK_CACHE_FIXED_SIZE_DEFAULT
088        == blockCache.getMaxSize());
089  }
090
091  @Test
092  public void testConfiguredBlockCache() throws IOException {
093    Configuration copyConf = new Configuration(conf);
094    // tiny 1MB fixed cache size
095    long blockCacheFixedSize = 1024 * 1024L;
096    copyConf.setLong(HConstants.HFILE_ONHEAP_BLOCK_CACHE_FIXED_SIZE_KEY, blockCacheFixedSize);
097    ClientSideRegionScanner clientSideRegionScanner =
098      new ClientSideRegionScanner(copyConf, fs, rootDir, htd, hri, scan, null);
099
100    BlockCache blockCache = clientSideRegionScanner.getRegion().getBlockCache();
101    assertNotNull(blockCache);
102    assertTrue(blockCache instanceof IndexOnlyLruBlockCache);
103    assertTrue(blockCacheFixedSize == blockCache.getMaxSize());
104  }
105
106  @Test
107  public void testNoBlockCache() throws IOException {
108    Configuration copyConf = new Configuration(conf);
109    copyConf.setFloat(HConstants.HFILE_BLOCK_CACHE_SIZE_KEY, 0.0f);
110    ClientSideRegionScanner clientSideRegionScanner =
111      new ClientSideRegionScanner(copyConf, fs, rootDir, htd, hri, scan, null);
112
113    BlockCache blockCache = clientSideRegionScanner.getRegion().getBlockCache();
114    assertNull(blockCache);
115  }
116}