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 CachedBlock implements HeapSize, Comparable<CachedBlock> {
36  
37    public final static long PER_BLOCK_OVERHEAD = ClassSize.align(
38      ClassSize.OBJECT + (3 * ClassSize.REFERENCE) + (2 * Bytes.SIZEOF_LONG) +
39      ClassSize.STRING + ClassSize.BYTE_BUFFER);
40  
41    static enum BlockPriority {
42      /**
43       * Accessed a single time (used for scan-resistance)
44       */
45      SINGLE,
46      /**
47       * Accessed multiple times
48       */
49      MULTI,
50      /**
51       * Block from in-memory store
52       */
53      MEMORY
54    };
55  
56    private final BlockCacheKey cacheKey;
57    private final Cacheable buf;
58    private volatile long accessTime;
59    private long size;
60    private BlockPriority priority;
61  
62    public CachedBlock(BlockCacheKey cacheKey, Cacheable buf, long accessTime) {
63      this(cacheKey, buf, accessTime, false);
64    }
65  
66    public CachedBlock(BlockCacheKey cacheKey, Cacheable buf, long accessTime,
67        boolean inMemory) {
68      this.cacheKey = cacheKey;
69      this.buf = buf;
70      this.accessTime = accessTime;
71      // We approximate the size of this class by the size of its name string
72      // plus the size of its byte buffer plus the overhead associated with all
73      // the base classes. We also include the base class
74      // sizes in the PER_BLOCK_OVERHEAD variable rather than align()ing them with
75      // their buffer lengths. This variable is used elsewhere in unit tests.
76      this.size = ClassSize.align(cacheKey.heapSize())
77          + ClassSize.align(buf.heapSize()) + PER_BLOCK_OVERHEAD;
78      if(inMemory) {
79        this.priority = BlockPriority.MEMORY;
80      } else {
81        this.priority = BlockPriority.SINGLE;
82      }
83    }
84  
85    /**
86     * Block has been accessed.  Update its local access time.
87     */
88    public void access(long accessTime) {
89      this.accessTime = accessTime;
90      if(this.priority == BlockPriority.SINGLE) {
91        this.priority = BlockPriority.MULTI;
92      }
93    }
94  
95    public long heapSize() {
96      return size;
97    }
98  
99    @Override
100   public int compareTo(CachedBlock that) {
101     if(this.accessTime == that.accessTime) return 0;
102     return this.accessTime < that.accessTime ? 1 : -1;
103   }
104 
105   @Override
106   public boolean equals(Object obj) {
107     if (this == obj) {
108       return true;
109     }
110     if (obj == null || getClass() != obj.getClass()) {
111       return false;
112     }
113     CachedBlock other = (CachedBlock) obj;
114     return compareTo(other) == 0;
115   }
116 
117   public Cacheable getBuffer() {
118     return this.buf;
119   }
120 
121   public BlockCacheKey getCacheKey() {
122     return this.cacheKey;
123   }
124 
125   public BlockPriority getPriority() {
126     return this.priority;
127   }
128 }