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;
20  
21  import java.nio.ByteBuffer;
22  
23  import junit.framework.TestCase;
24  import org.apache.hadoop.hbase.testclassification.SmallTests;
25  import org.junit.experimental.categories.Category;
26  
27  @Category(SmallTests.class)
28  public class TestCachedBlockQueue extends TestCase {
29  
30    public void testQueue() throws Exception {
31  
32      CachedBlock cb1 = new CachedBlock(1000, "cb1", 1);
33      CachedBlock cb2 = new CachedBlock(1500, "cb2", 2);
34      CachedBlock cb3 = new CachedBlock(1000, "cb3", 3);
35      CachedBlock cb4 = new CachedBlock(1500, "cb4", 4);
36      CachedBlock cb5 = new CachedBlock(1000, "cb5", 5);
37      CachedBlock cb6 = new CachedBlock(1750, "cb6", 6);
38      CachedBlock cb7 = new CachedBlock(1000, "cb7", 7);
39      CachedBlock cb8 = new CachedBlock(1500, "cb8", 8);
40      CachedBlock cb9 = new CachedBlock(1000, "cb9", 9);
41      CachedBlock cb10 = new CachedBlock(1500, "cb10", 10);
42  
43      LruCachedBlockQueue queue = new LruCachedBlockQueue(10000,1000);
44  
45      queue.add(cb1);
46      queue.add(cb2);
47      queue.add(cb3);
48      queue.add(cb4);
49      queue.add(cb5);
50      queue.add(cb6);
51      queue.add(cb7);
52      queue.add(cb8);
53      queue.add(cb9);
54      queue.add(cb10);
55  
56      // We expect cb1 through cb8 to be in the queue
57      long expectedSize = cb1.heapSize() + cb2.heapSize() + cb3.heapSize() +
58        cb4.heapSize() + cb5.heapSize() + cb6.heapSize() + cb7.heapSize() +
59        cb8.heapSize();
60  
61      assertEquals(queue.heapSize(), expectedSize);
62  
63      for (int i = 1; i <= 8; i++) {
64        assertEquals(queue.pollLast().getCacheKey().getHfileName(), "cb"+i);      
65      }
66    }
67  
68    public void testQueueSmallBlockEdgeCase() throws Exception {
69  
70      CachedBlock cb1 = new CachedBlock(1000, "cb1", 1);
71      CachedBlock cb2 = new CachedBlock(1500, "cb2", 2);
72      CachedBlock cb3 = new CachedBlock(1000, "cb3", 3);
73      CachedBlock cb4 = new CachedBlock(1500, "cb4", 4);
74      CachedBlock cb5 = new CachedBlock(1000, "cb5", 5);
75      CachedBlock cb6 = new CachedBlock(1750, "cb6", 6);
76      CachedBlock cb7 = new CachedBlock(1000, "cb7", 7);
77      CachedBlock cb8 = new CachedBlock(1500, "cb8", 8);
78      CachedBlock cb9 = new CachedBlock(1000, "cb9", 9);
79      CachedBlock cb10 = new CachedBlock(1500, "cb10", 10);
80  
81      LruCachedBlockQueue queue = new LruCachedBlockQueue(10000,1000);
82  
83      queue.add(cb1);
84      queue.add(cb2);
85      queue.add(cb3);
86      queue.add(cb4);
87      queue.add(cb5);
88      queue.add(cb6);
89      queue.add(cb7);
90      queue.add(cb8);
91      queue.add(cb9);
92      queue.add(cb10);
93  
94      CachedBlock cb0 = new CachedBlock(10 + CachedBlock.PER_BLOCK_OVERHEAD, "cb0", 0);
95      queue.add(cb0);
96  
97      // This is older so we must include it, but it will not end up kicking
98      // anything out because (heapSize - cb8.heapSize + cb0.heapSize < maxSize)
99      // and we must always maintain heapSize >= maxSize once we achieve it.
100 
101     // We expect cb0 through cb8 to be in the queue
102     long expectedSize = cb1.heapSize() + cb2.heapSize() + cb3.heapSize() +
103       cb4.heapSize() + cb5.heapSize() + cb6.heapSize() + cb7.heapSize() +
104       cb8.heapSize() + cb0.heapSize();
105 
106     assertEquals(queue.heapSize(), expectedSize);
107 
108     for (int i = 0; i <= 8; i++) {
109       assertEquals(queue.pollLast().getCacheKey().getHfileName(), "cb"+i);      
110     }
111   }
112 
113   private static class CachedBlock extends org.apache.hadoop.hbase.io.hfile.LruCachedBlock
114   {
115     public CachedBlock(final long heapSize, String name, long accessTime) {
116       super(new BlockCacheKey(name, 0),
117           new Cacheable() {
118             @Override
119             public long heapSize() {
120               return ((int)(heapSize - CachedBlock.PER_BLOCK_OVERHEAD));
121             }
122 
123             @Override
124             public int getSerializedLength() {
125               return 0;
126             }
127 
128             @Override
129             public void serialize(ByteBuffer destination) {
130             }
131 
132             @Override
133             public CacheableDeserializer<Cacheable> getDeserializer() {
134               // TODO Auto-generated method stub
135               return null;
136             }
137 
138             @Override
139             public BlockType getBlockType() {
140               return BlockType.DATA;
141             }
142 
143           }, accessTime, false);
144     }
145   }
146 
147 }
148