View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.io.hfile.slab;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertTrue;
23  
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.MediumTests;
26  import org.apache.hadoop.hbase.io.hfile.CacheTestUtils;
27  import org.apache.hadoop.hbase.io.hfile.slab.SlabCache.SlabStats;
28  import org.junit.After;
29  import org.junit.Before;
30  import org.junit.Test;
31  import org.junit.Ignore;
32  import org.junit.experimental.categories.Category;
33  
34  /**
35   * Basic test of SlabCache. Puts and gets.
36   * <p>
37   *
38   * Tests will ensure that blocks that are uncached are identical to the ones
39   * being cached, and that the cache never exceeds its capacity. Note that its
40   * fine if the cache evicts before it reaches max capacity - Guava Mapmaker may
41   * choose to evict at any time.
42   *
43   */
44  // Starts 50 threads, high variability of execution time => Medium
45  @Category(MediumTests.class)
46  public class TestSlabCache {
47    static final int CACHE_SIZE = 1000000;
48    static final int NUM_BLOCKS = 101;
49    static final int BLOCK_SIZE = CACHE_SIZE / NUM_BLOCKS;
50    static final int NUM_THREADS = 50;
51    static final int NUM_QUERIES = 10000;
52    SlabCache cache;
53  
54    @Before
55    public void setup() {
56      cache = new SlabCache(CACHE_SIZE + BLOCK_SIZE * 2, BLOCK_SIZE);
57      cache.addSlabByConf(new Configuration());
58    }
59  
60    @After
61    public void tearDown() {
62      cache.shutdown();
63    }
64  
65    @Test
66    public void testElementPlacement() {
67      assertEquals(cache.getHigherBlock(BLOCK_SIZE).getKey().intValue(),
68          (BLOCK_SIZE * 11 / 10));
69      assertEquals(cache.getHigherBlock((BLOCK_SIZE * 2)).getKey()
70          .intValue(), (BLOCK_SIZE * 21 / 10));
71    }
72  
73   @Test
74    public void testCacheSimple() throws Exception {
75      CacheTestUtils.testCacheSimple(cache, BLOCK_SIZE, NUM_QUERIES);
76    }
77  
78    @Test
79    public void testCacheMultiThreaded() throws Exception {
80      CacheTestUtils.testCacheMultiThreaded(cache, BLOCK_SIZE, NUM_THREADS,
81          NUM_QUERIES, 0.80);
82    }
83  
84    @Test
85    public void testCacheMultiThreadedSingleKey() throws Exception {
86      CacheTestUtils.hammerSingleKey(cache, BLOCK_SIZE, NUM_THREADS, NUM_QUERIES);
87    }
88  
89    @Test
90    public void testCacheMultiThreadedEviction() throws Exception {
91      CacheTestUtils.hammerEviction(cache, BLOCK_SIZE, 10, NUM_QUERIES);
92    }
93  
94    @Test
95    /*Just checks if ranges overlap*/
96    public void testStatsArithmetic(){
97      SlabStats test = cache.requestStats;
98      for(int i = 0; i < test.NUMDIVISIONS; i++){
99        assertTrue("Upper for index " + i + " is " + test.getUpperBound(i) +
100           " lower " + test.getLowerBound(i + 1),
101           test.getUpperBound(i) <= test.getLowerBound(i + 1));
102     }
103   }
104 
105   @Test
106   public void testHeapSizeChanges(){
107     CacheTestUtils.testHeapSizeChanges(cache, BLOCK_SIZE);
108   }
109 
110 }
111