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 org.apache.hadoop.classification.InterfaceAudience;
22  import org.apache.hadoop.hbase.io.HeapSize;
23  import org.apache.hadoop.hbase.util.Bytes;
24  import org.apache.hadoop.hbase.util.ClassSize;
25  
26  /**
27   * Represents an entry in the {@link LruBlockCache}.
28   *
29   * <p>Makes the block memory-aware with {@link HeapSize} and Comparable
30   * to sort by access time for the LRU.  It also takes care of priority by
31   * either instantiating as in-memory or handling the transition from single
32   * to multiple access.
33   */
34  @InterfaceAudience.Private
35  public class LruCachedBlock implements HeapSize, Comparable<LruCachedBlock> {
36  
37    public final static long PER_BLOCK_OVERHEAD = ClassSize.align(
38      ClassSize.OBJECT + (3 * ClassSize.REFERENCE) + (3 * Bytes.SIZEOF_LONG) +
39      ClassSize.STRING + ClassSize.BYTE_BUFFER);
40  
41    private final BlockCacheKey cacheKey;
42    private final Cacheable buf;
43    private volatile long accessTime;
44    private long size;
45    private BlockPriority priority;
46    /**
47     * Time this block was cached.  Presumes we are created just before we are added to the cache.
48     */
49    private final long cachedTime = System.nanoTime();
50  
51    public LruCachedBlock(BlockCacheKey cacheKey, Cacheable buf, long accessTime) {
52      this(cacheKey, buf, accessTime, false);
53    }
54  
55    public LruCachedBlock(BlockCacheKey cacheKey, Cacheable buf, long accessTime,
56        boolean inMemory) {
57      this.cacheKey = cacheKey;
58      this.buf = buf;
59      this.accessTime = accessTime;
60      // We approximate the size of this class by the size of its name string
61      // plus the size of its byte buffer plus the overhead associated with all
62      // the base classes. We also include the base class
63      // sizes in the PER_BLOCK_OVERHEAD variable rather than align()ing them with
64      // their buffer lengths. This variable is used elsewhere in unit tests.
65      this.size = ClassSize.align(cacheKey.heapSize())
66          + ClassSize.align(buf.heapSize()) + PER_BLOCK_OVERHEAD;
67      if(inMemory) {
68        this.priority = BlockPriority.MEMORY;
69      } else {
70        this.priority = BlockPriority.SINGLE;
71      }
72    }
73  
74    /**
75     * Block has been accessed.
76     * @param accessTime Last access; this is actually a incremented sequence number rather than an
77     * actual time.
78     */
79    public void access(long accessTime) {
80      this.accessTime = accessTime;
81      if(this.priority == BlockPriority.SINGLE) {
82        this.priority = BlockPriority.MULTI;
83      }
84    }
85  
86    /**
87     * @return Time we were cached at in nano seconds.
88     */
89    public long getCachedTime() {
90      return this.cachedTime;
91    }
92  
93    public long heapSize() {
94      return size;
95    }
96  
97    @Override
98    public int compareTo(LruCachedBlock that) {
99      // Newer accessed blocks sort before older ones.
100     if (this.accessTime == that.accessTime) return 0;
101     return this.accessTime < that.accessTime ? 1 : -1;
102   }
103 
104   @Override
105   public int hashCode() {
106     return (int)(accessTime ^ (accessTime >>> 32));
107   }
108 
109   @Override
110   public boolean equals(Object obj) {
111     if (this == obj) {
112       return true;
113     }
114     if (obj == null || getClass() != obj.getClass()) {
115       return false;
116     }
117     LruCachedBlock other = (LruCachedBlock) obj;
118     return compareTo(other) == 0;
119   }
120 
121   public Cacheable getBuffer() {
122     return this.buf;
123   }
124 
125   public BlockCacheKey getCacheKey() {
126     return this.cacheKey;
127   }
128 
129   public BlockPriority getPriority() {
130     return this.priority;
131   }
132 }