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