View Javadoc

1   /**
2    * Copyright 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  
21  package org.apache.hadoop.hbase.io.hfile.bucket;
22  
23  import java.util.Arrays;
24  import java.util.Comparator;
25  import java.util.HashSet;
26  import java.util.Iterator;
27  import java.util.Map;
28  import java.util.Queue;
29  import java.util.Set;
30  import java.util.concurrent.atomic.AtomicLong;
31  
32  import com.google.common.collect.MinMaxPriorityQueue;
33  import org.apache.commons.collections.map.LinkedMap;
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  import org.apache.hadoop.hbase.classification.InterfaceAudience;
37  import org.apache.hadoop.hbase.io.hfile.BlockCacheKey;
38  import org.apache.hadoop.hbase.io.hfile.CacheConfig;
39  import org.apache.hadoop.hbase.io.hfile.bucket.BucketCache.BucketEntry;
40  
41  import com.google.common.base.Objects;
42  import com.google.common.base.Preconditions;
43  import com.google.common.primitives.Ints;
44  
45  /**
46   * This class is used to allocate a block with specified size and free the block
47   * when evicting. It manages an array of buckets, each bucket is associated with
48   * a size and caches elements up to this size. For a completely empty bucket, this
49   * size could be re-specified dynamically.
50   *
51   * This class is not thread safe.
52   */
53  @InterfaceAudience.Private
54  public final class BucketAllocator {
55    private static final Log LOG = LogFactory.getLog(BucketAllocator.class);
56  
57    public final static class Bucket {
58      private long baseOffset;
59      private int itemAllocationSize, sizeIndex;
60      private int itemCount;
61      private int freeList[];
62      private int freeCount, usedCount;
63  
64      public Bucket(long offset) {
65        baseOffset = offset;
66        sizeIndex = -1;
67      }
68  
69      void reconfigure(int sizeIndex, int[] bucketSizes, long bucketCapacity) {
70        Preconditions.checkElementIndex(sizeIndex, bucketSizes.length);
71        this.sizeIndex = sizeIndex;
72        itemAllocationSize = bucketSizes[sizeIndex];
73        itemCount = (int) (bucketCapacity / (long) itemAllocationSize);
74        freeCount = itemCount;
75        usedCount = 0;
76        freeList = new int[itemCount];
77        for (int i = 0; i < freeCount; ++i)
78          freeList[i] = i;
79      }
80  
81      public boolean isUninstantiated() {
82        return sizeIndex == -1;
83      }
84  
85      public int sizeIndex() {
86        return sizeIndex;
87      }
88  
89      public int getItemAllocationSize() {
90        return itemAllocationSize;
91      }
92  
93      public boolean hasFreeSpace() {
94        return freeCount > 0;
95      }
96  
97      public boolean isCompletelyFree() {
98        return usedCount == 0;
99      }
100 
101     public int freeCount() {
102       return freeCount;
103     }
104 
105     public int usedCount() {
106       return usedCount;
107     }
108 
109     public int getFreeBytes() {
110       return freeCount * itemAllocationSize;
111     }
112 
113     public int getUsedBytes() {
114       return usedCount * itemAllocationSize;
115     }
116 
117     public long getBaseOffset() {
118       return baseOffset;
119     }
120 
121     /**
122      * Allocate a block in this bucket, return the offset representing the
123      * position in physical space
124      * @return the offset in the IOEngine
125      */
126     public long allocate() {
127       assert freeCount > 0; // Else should not have been called
128       assert sizeIndex != -1;
129       ++usedCount;
130       long offset = baseOffset + (freeList[--freeCount] * itemAllocationSize);
131       assert offset >= 0;
132       return offset;
133     }
134 
135     public void addAllocation(long offset) throws BucketAllocatorException {
136       offset -= baseOffset;
137       if (offset < 0 || offset % itemAllocationSize != 0)
138         throw new BucketAllocatorException(
139             "Attempt to add allocation for bad offset: " + offset + " base="
140                 + baseOffset + ", bucket size=" + itemAllocationSize);
141       int idx = (int) (offset / itemAllocationSize);
142       boolean matchFound = false;
143       for (int i = 0; i < freeCount; ++i) {
144         if (matchFound) freeList[i - 1] = freeList[i];
145         else if (freeList[i] == idx) matchFound = true;
146       }
147       if (!matchFound)
148         throw new BucketAllocatorException("Couldn't find match for index "
149             + idx + " in free list");
150       ++usedCount;
151       --freeCount;
152     }
153 
154     private void free(long offset) {
155       offset -= baseOffset;
156       assert offset >= 0;
157       assert offset < itemCount * itemAllocationSize;
158       assert offset % itemAllocationSize == 0;
159       assert usedCount > 0;
160       assert freeCount < itemCount; // Else duplicate free
161       int item = (int) (offset / (long) itemAllocationSize);
162       assert !freeListContains(item);
163       --usedCount;
164       freeList[freeCount++] = item;
165     }
166 
167     private boolean freeListContains(int blockNo) {
168       for (int i = 0; i < freeCount; ++i) {
169         if (freeList[i] == blockNo) return true;
170       }
171       return false;
172     }
173   }
174 
175   final class BucketSizeInfo {
176     // Free bucket means it has space to allocate a block;
177     // Completely free bucket means it has no block.
178     private LinkedMap bucketList, freeBuckets, completelyFreeBuckets;
179     private int sizeIndex;
180 
181     BucketSizeInfo(int sizeIndex) {
182       bucketList = new LinkedMap();
183       freeBuckets = new LinkedMap();
184       completelyFreeBuckets = new LinkedMap();
185       this.sizeIndex = sizeIndex;
186     }
187 
188     public synchronized void instantiateBucket(Bucket b) {
189       assert b.isUninstantiated() || b.isCompletelyFree();
190       b.reconfigure(sizeIndex, bucketSizes, bucketCapacity);
191       bucketList.put(b, b);
192       freeBuckets.put(b, b);
193       completelyFreeBuckets.put(b, b);
194     }
195 
196     public int sizeIndex() {
197       return sizeIndex;
198     }
199 
200     /**
201      * Find a bucket to allocate a block
202      * @return the offset in the IOEngine
203      */
204     public long allocateBlock() {
205       Bucket b = null;
206       if (freeBuckets.size() > 0) {
207         // Use up an existing one first...
208         b = (Bucket) freeBuckets.lastKey();
209       }
210       if (b == null) {
211         b = grabGlobalCompletelyFreeBucket();
212         if (b != null) instantiateBucket(b);
213       }
214       if (b == null) return -1;
215       long result = b.allocate();
216       blockAllocated(b);
217       return result;
218     }
219 
220     void blockAllocated(Bucket b) {
221       if (!b.isCompletelyFree()) completelyFreeBuckets.remove(b);
222       if (!b.hasFreeSpace()) freeBuckets.remove(b);
223     }
224 
225     public Bucket findAndRemoveCompletelyFreeBucket() {
226       Bucket b = null;
227       assert bucketList.size() > 0;
228       if (bucketList.size() == 1) {
229         // So we never get complete starvation of a bucket for a size
230         return null;
231       }
232 
233       if (completelyFreeBuckets.size() > 0) {
234         b = (Bucket) completelyFreeBuckets.firstKey();
235         removeBucket(b);
236       }
237       return b;
238     }
239 
240     private synchronized void removeBucket(Bucket b) {
241       assert b.isCompletelyFree();
242       bucketList.remove(b);
243       freeBuckets.remove(b);
244       completelyFreeBuckets.remove(b);
245     }
246 
247     public void freeBlock(Bucket b, long offset) {
248       assert bucketList.containsKey(b);
249       // else we shouldn't have anything to free...
250       assert (!completelyFreeBuckets.containsKey(b));
251       b.free(offset);
252       if (!freeBuckets.containsKey(b)) freeBuckets.put(b, b);
253       if (b.isCompletelyFree()) completelyFreeBuckets.put(b, b);
254     }
255 
256     public synchronized IndexStatistics statistics() {
257       long free = 0, used = 0;
258       for (Object obj : bucketList.keySet()) {
259         Bucket b = (Bucket) obj;
260         free += b.freeCount();
261         used += b.usedCount();
262       }
263       return new IndexStatistics(free, used, bucketSizes[sizeIndex]);
264     }
265 
266     @Override
267     public String toString() {
268       return Objects.toStringHelper(this.getClass())
269         .add("sizeIndex", sizeIndex)
270         .add("bucketSize", bucketSizes[sizeIndex])
271         .toString();
272     }
273   }
274 
275   // Default block size in hbase is 64K, so we choose more sizes near 64K, you'd better
276   // reset it according to your cluster's block size distribution
277   // TODO Support the view of block size distribution statistics
278   // TODO: Why we add the extra 1024 bytes? Slop?
279   private static final int DEFAULT_BUCKET_SIZES[] = { 4 * 1024 + 1024, 8 * 1024 + 1024,
280       16 * 1024 + 1024, 32 * 1024 + 1024, 40 * 1024 + 1024, 48 * 1024 + 1024,
281       56 * 1024 + 1024, 64 * 1024 + 1024, 96 * 1024 + 1024, 128 * 1024 + 1024,
282       192 * 1024 + 1024, 256 * 1024 + 1024, 384 * 1024 + 1024,
283       512 * 1024 + 1024 };
284 
285   /**
286    * Round up the given block size to bucket size, and get the corresponding
287    * BucketSizeInfo
288    */
289   public BucketSizeInfo roundUpToBucketSizeInfo(int blockSize) {
290     for (int i = 0; i < bucketSizes.length; ++i)
291       if (blockSize <= bucketSizes[i])
292         return bucketSizeInfos[i];
293     return null;
294   }
295 
296   /**
297    * So, what is the minimum amount of items we'll tolerate in a single bucket?
298    */
299   static public final int FEWEST_ITEMS_IN_BUCKET = 4;
300 
301   private final int[] bucketSizes;
302   private final int bigItemSize;
303   // The capacity size for each bucket
304   private final long bucketCapacity;
305   private Bucket[] buckets;
306   private BucketSizeInfo[] bucketSizeInfos;
307   private final long totalSize;
308   private transient long usedSize = 0;
309 
310   BucketAllocator(long availableSpace, int[] bucketSizes)
311       throws BucketAllocatorException {
312     this.bucketSizes = bucketSizes == null ? DEFAULT_BUCKET_SIZES : bucketSizes;
313     Arrays.sort(this.bucketSizes);
314     this.bigItemSize = Ints.max(this.bucketSizes);
315     this.bucketCapacity = FEWEST_ITEMS_IN_BUCKET * bigItemSize;
316     buckets = new Bucket[(int) (availableSpace / bucketCapacity)];
317     if (buckets.length < this.bucketSizes.length)
318       throw new BucketAllocatorException("Bucket allocator size too small (" + buckets.length +
319         "); must have room for at least " + this.bucketSizes.length + " buckets");
320     bucketSizeInfos = new BucketSizeInfo[this.bucketSizes.length];
321     for (int i = 0; i < this.bucketSizes.length; ++i) {
322       bucketSizeInfos[i] = new BucketSizeInfo(i);
323     }
324     for (int i = 0; i < buckets.length; ++i) {
325       buckets[i] = new Bucket(bucketCapacity * i);
326       bucketSizeInfos[i < this.bucketSizes.length ? i : this.bucketSizes.length - 1]
327           .instantiateBucket(buckets[i]);
328     }
329     this.totalSize = ((long) buckets.length) * bucketCapacity;
330     if (LOG.isInfoEnabled()) {
331       LOG.info("Cache totalSize=" + this.totalSize + ", buckets=" + this.buckets.length +
332         ", bucket capacity=" + this.bucketCapacity +
333         "=(" + FEWEST_ITEMS_IN_BUCKET + "*" + this.bigItemSize + ")=" +
334         "(FEWEST_ITEMS_IN_BUCKET*(largest configured bucketcache size))");
335     }
336   }
337 
338   /**
339    * Rebuild the allocator's data structures from a persisted map.
340    * @param availableSpace capacity of cache
341    * @param map A map stores the block key and BucketEntry(block's meta data
342    *          like offset, length)
343    * @param realCacheSize cached data size statistics for bucket cache
344    * @throws BucketAllocatorException
345    */
346   BucketAllocator(long availableSpace, int[] bucketSizes, Map<BlockCacheKey, BucketEntry> map,
347       AtomicLong realCacheSize) throws BucketAllocatorException {
348     this(availableSpace, bucketSizes);
349 
350     // each bucket has an offset, sizeindex. probably the buckets are too big
351     // in our default state. so what we do is reconfigure them according to what
352     // we've found. we can only reconfigure each bucket once; if more than once,
353     // we know there's a bug, so we just log the info, throw, and start again...
354     boolean[] reconfigured = new boolean[buckets.length];
355     int sizeNotMatchedCount = 0;
356     int insufficientCapacityCount = 0;
357     Iterator<Map.Entry<BlockCacheKey, BucketEntry>> iterator = map.entrySet().iterator();
358     while (iterator.hasNext()) {
359       Map.Entry<BlockCacheKey, BucketEntry> entry = iterator.next();
360       long foundOffset = entry.getValue().offset();
361       int foundLen = entry.getValue().getLength();
362       int bucketSizeIndex = -1;
363       for (int i = 0; i < this.bucketSizes.length; ++i) {
364         if (foundLen <= this.bucketSizes[i]) {
365           bucketSizeIndex = i;
366           break;
367         }
368       }
369       if (bucketSizeIndex == -1) {
370         sizeNotMatchedCount++;
371         iterator.remove();
372         continue;
373       }
374       int bucketNo = (int) (foundOffset / bucketCapacity);
375       if (bucketNo < 0 || bucketNo >= buckets.length) {
376         insufficientCapacityCount++;
377         iterator.remove();
378         continue;
379       }
380       Bucket b = buckets[bucketNo];
381       if (reconfigured[bucketNo]) {
382         if (b.sizeIndex() != bucketSizeIndex)
383           throw new BucketAllocatorException(
384               "Inconsistent allocation in bucket map;");
385       } else {
386         if (!b.isCompletelyFree())
387           throw new BucketAllocatorException("Reconfiguring bucket "
388               + bucketNo + " but it's already allocated; corrupt data");
389         // Need to remove the bucket from whichever list it's currently in at
390         // the moment...
391         BucketSizeInfo bsi = bucketSizeInfos[bucketSizeIndex];
392         BucketSizeInfo oldbsi = bucketSizeInfos[b.sizeIndex()];
393         oldbsi.removeBucket(b);
394         bsi.instantiateBucket(b);
395         reconfigured[bucketNo] = true;
396       }
397       realCacheSize.addAndGet(foundLen);
398       buckets[bucketNo].addAllocation(foundOffset);
399       usedSize += buckets[bucketNo].getItemAllocationSize();
400       bucketSizeInfos[bucketSizeIndex].blockAllocated(b);
401     }
402 
403     if (sizeNotMatchedCount > 0) {
404       LOG.warn("There are " + sizeNotMatchedCount + " blocks which can't be rebuilt because "
405           + "there is no matching bucket size for these blocks");
406     }
407     if (insufficientCapacityCount > 0) {
408       LOG.warn("There are " + insufficientCapacityCount + " blocks which can't be rebuilt - "
409           + "did you shrink the cache?");
410     }
411   }
412 
413   public String toString() {
414     StringBuilder sb = new StringBuilder(1024);
415     for (int i = 0; i < buckets.length; ++i) {
416       Bucket b = buckets[i];
417       if (i > 0) sb.append(", ");
418       sb.append("bucket.").append(i).append(": size=").append(b.getItemAllocationSize());
419       sb.append(", freeCount=").append(b.freeCount()).append(", used=").append(b.usedCount());
420     }
421     return sb.toString();
422   }
423 
424   public long getUsedSize() {
425     return this.usedSize;
426   }
427 
428   public long getFreeSize() {
429     return this.totalSize - getUsedSize();
430   }
431 
432   public long getTotalSize() {
433     return this.totalSize;
434   }
435 
436   /**
437    * Allocate a block with specified size. Return the offset
438    * @param blockSize size of block
439    * @throws BucketAllocatorException
440    * @throws CacheFullException
441    * @return the offset in the IOEngine
442    */
443   public synchronized long allocateBlock(int blockSize) throws CacheFullException,
444       BucketAllocatorException {
445     assert blockSize > 0;
446     BucketSizeInfo bsi = roundUpToBucketSizeInfo(blockSize);
447     if (bsi == null) {
448       throw new BucketAllocatorException("Allocation too big size=" + blockSize +
449         "; adjust BucketCache sizes " + CacheConfig.BUCKET_CACHE_BUCKETS_KEY +
450         " to accomodate if size seems reasonable and you want it cached.");
451     }
452     long offset = bsi.allocateBlock();
453 
454     // Ask caller to free up space and try again!
455     if (offset < 0)
456       throw new CacheFullException(blockSize, bsi.sizeIndex());
457     usedSize += bucketSizes[bsi.sizeIndex()];
458     return offset;
459   }
460 
461   private Bucket grabGlobalCompletelyFreeBucket() {
462     for (BucketSizeInfo bsi : bucketSizeInfos) {
463       Bucket b = bsi.findAndRemoveCompletelyFreeBucket();
464       if (b != null) return b;
465     }
466     return null;
467   }
468 
469   /**
470    * Free a block with the offset
471    * @param offset block's offset
472    * @return size freed
473    */
474   public synchronized int freeBlock(long offset) {
475     int bucketNo = (int) (offset / bucketCapacity);
476     assert bucketNo >= 0 && bucketNo < buckets.length;
477     Bucket targetBucket = buckets[bucketNo];
478     bucketSizeInfos[targetBucket.sizeIndex()].freeBlock(targetBucket, offset);
479     usedSize -= targetBucket.getItemAllocationSize();
480     return targetBucket.getItemAllocationSize();
481   }
482 
483   public int sizeIndexOfAllocation(long offset) {
484     int bucketNo = (int) (offset / bucketCapacity);
485     assert bucketNo >= 0 && bucketNo < buckets.length;
486     Bucket targetBucket = buckets[bucketNo];
487     return targetBucket.sizeIndex();
488   }
489 
490   public int sizeOfAllocation(long offset) {
491     int bucketNo = (int) (offset / bucketCapacity);
492     assert bucketNo >= 0 && bucketNo < buckets.length;
493     Bucket targetBucket = buckets[bucketNo];
494     return targetBucket.getItemAllocationSize();
495   }
496 
497   static class IndexStatistics {
498     private long freeCount, usedCount, itemSize, totalCount;
499 
500     public long freeCount() {
501       return freeCount;
502     }
503 
504     public long usedCount() {
505       return usedCount;
506     }
507 
508     public long totalCount() {
509       return totalCount;
510     }
511 
512     public long freeBytes() {
513       return freeCount * itemSize;
514     }
515 
516     public long usedBytes() {
517       return usedCount * itemSize;
518     }
519 
520     public long totalBytes() {
521       return totalCount * itemSize;
522     }
523 
524     public long itemSize() {
525       return itemSize;
526     }
527 
528     public IndexStatistics(long free, long used, long itemSize) {
529       setTo(free, used, itemSize);
530     }
531 
532     public IndexStatistics() {
533       setTo(-1, -1, 0);
534     }
535 
536     public void setTo(long free, long used, long itemSize) {
537       this.itemSize = itemSize;
538       this.freeCount = free;
539       this.usedCount = used;
540       this.totalCount = free + used;
541     }
542   }
543 
544   public Bucket [] getBuckets() {
545     return this.buckets;
546   }
547 
548   void logStatistics() {
549     IndexStatistics total = new IndexStatistics();
550     IndexStatistics[] stats = getIndexStatistics(total);
551     LOG.info("Bucket allocator statistics follow:\n");
552     LOG.info("  Free bytes=" + total.freeBytes() + "+; used bytes="
553         + total.usedBytes() + "; total bytes=" + total.totalBytes());
554     for (IndexStatistics s : stats) {
555       LOG.info("  Object size " + s.itemSize() + " used=" + s.usedCount()
556           + "; free=" + s.freeCount() + "; total=" + s.totalCount());
557     }
558   }
559 
560   IndexStatistics[] getIndexStatistics(IndexStatistics grandTotal) {
561     IndexStatistics[] stats = getIndexStatistics();
562     long totalfree = 0, totalused = 0;
563     for (IndexStatistics stat : stats) {
564       totalfree += stat.freeBytes();
565       totalused += stat.usedBytes();
566     }
567     grandTotal.setTo(totalfree, totalused, 1);
568     return stats;
569   }
570 
571   IndexStatistics[] getIndexStatistics() {
572     IndexStatistics[] stats = new IndexStatistics[bucketSizes.length];
573     for (int i = 0; i < stats.length; ++i)
574       stats[i] = bucketSizeInfos[i].statistics();
575     return stats;
576   }
577 
578   public long freeBlock(long freeList[]) {
579     long sz = 0;
580     for (int i = 0; i < freeList.length; ++i)
581       sz += freeBlock(freeList[i]);
582     return sz;
583   }
584 
585   public int getBucketIndex(long offset) {
586     return (int) (offset / bucketCapacity);
587   }
588 
589   /**
590    * Returns a set of indices of the buckets that are least filled
591    * excluding the offsets, we also the fully free buckets for the
592    * BucketSizes where everything is empty and they only have one
593    * completely free bucket as a reserved
594    *
595    * @param excludedBuckets the buckets that need to be excluded due to
596    *                        currently being in used
597    * @param bucketCount     max Number of buckets to return
598    * @return set of bucket indices which could be used for eviction
599    */
600   public Set<Integer> getLeastFilledBuckets(Set<Integer> excludedBuckets,
601                                             int bucketCount) {
602     Queue<Integer> queue = MinMaxPriorityQueue.<Integer>orderedBy(
603         new Comparator<Integer>() {
604           @Override
605           public int compare(Integer left, Integer right) {
606             // We will always get instantiated buckets
607             return Float.compare(
608                 ((float) buckets[left].usedCount) / buckets[left].itemCount,
609                 ((float) buckets[right].usedCount) / buckets[right].itemCount);
610           }
611         }).maximumSize(bucketCount).create();
612 
613     for (int i = 0; i < buckets.length; i ++ ) {
614       if (!excludedBuckets.contains(i) && !buckets[i].isUninstantiated() &&
615           // Avoid the buckets that are the only buckets for a sizeIndex
616           bucketSizeInfos[buckets[i].sizeIndex()].bucketList.size() != 1) {
617         queue.add(i);
618       }
619     }
620 
621     Set<Integer> result = new HashSet<>(bucketCount);
622     result.addAll(queue);
623 
624     return result;
625   }
626 }