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