1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.io.hfile;
21
22 import java.lang.ref.WeakReference;
23 import java.nio.ByteBuffer;
24 import java.util.LinkedList;
25 import java.util.PriorityQueue;
26 import java.util.concurrent.atomic.AtomicLong;
27 import java.util.concurrent.locks.ReentrantLock;
28 import java.util.concurrent.ConcurrentHashMap;
29 import java.util.concurrent.ScheduledExecutorService;
30 import java.util.concurrent.Executors;
31 import java.util.concurrent.TimeUnit;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.apache.hadoop.hbase.io.HeapSize;
36 import org.apache.hadoop.hbase.util.Bytes;
37 import org.apache.hadoop.hbase.util.ClassSize;
38 import org.apache.hadoop.util.StringUtils;
39
40 import com.google.common.util.concurrent.ThreadFactoryBuilder;
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 public class LruBlockCache implements BlockCache, HeapSize {
81
82 static final Log LOG = LogFactory.getLog(LruBlockCache.class);
83
84
85
86
87 static final float DEFAULT_LOAD_FACTOR = 0.75f;
88 static final int DEFAULT_CONCURRENCY_LEVEL = 16;
89
90
91 static final float DEFAULT_MIN_FACTOR = 0.75f;
92 static final float DEFAULT_ACCEPTABLE_FACTOR = 0.85f;
93
94
95 static final float DEFAULT_SINGLE_FACTOR = 0.25f;
96 static final float DEFAULT_MULTI_FACTOR = 0.50f;
97 static final float DEFAULT_MEMORY_FACTOR = 0.25f;
98
99
100 static final int statThreadPeriod = 60 * 5;
101
102
103 private final ConcurrentHashMap<String,CachedBlock> map;
104
105
106 private final ReentrantLock evictionLock = new ReentrantLock(true);
107
108
109 private volatile boolean evictionInProgress = false;
110
111
112 private final EvictionThread evictionThread;
113
114
115 private final ScheduledExecutorService scheduleThreadPool =
116 Executors.newScheduledThreadPool(1,
117 new ThreadFactoryBuilder()
118 .setNameFormat("LRU Statistics #%d")
119 .build());
120
121
122 private final AtomicLong size;
123
124
125 private final AtomicLong elements;
126
127
128 private final AtomicLong count;
129
130
131 private final CacheStats stats;
132
133
134 private long maxSize;
135
136
137 private long blockSize;
138
139
140 private float acceptableFactor;
141
142
143 private float minFactor;
144
145
146 private float singleFactor;
147
148
149 private float multiFactor;
150
151
152 private float memoryFactor;
153
154
155 private long overhead;
156
157
158
159
160
161
162
163
164
165
166 public LruBlockCache(long maxSize, long blockSize) {
167 this(maxSize, blockSize, true);
168 }
169
170
171
172
173 public LruBlockCache(long maxSize, long blockSize, boolean evictionThread) {
174 this(maxSize, blockSize, evictionThread,
175 (int)Math.ceil(1.2*maxSize/blockSize),
176 DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL,
177 DEFAULT_MIN_FACTOR, DEFAULT_ACCEPTABLE_FACTOR,
178 DEFAULT_SINGLE_FACTOR, DEFAULT_MULTI_FACTOR,
179 DEFAULT_MEMORY_FACTOR);
180 }
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196 public LruBlockCache(long maxSize, long blockSize, boolean evictionThread,
197 int mapInitialSize, float mapLoadFactor, int mapConcurrencyLevel,
198 float minFactor, float acceptableFactor,
199 float singleFactor, float multiFactor, float memoryFactor) {
200 if(singleFactor + multiFactor + memoryFactor != 1) {
201 throw new IllegalArgumentException("Single, multi, and memory factors " +
202 " should total 1.0");
203 }
204 if(minFactor >= acceptableFactor) {
205 throw new IllegalArgumentException("minFactor must be smaller than acceptableFactor");
206 }
207 if(minFactor >= 1.0f || acceptableFactor >= 1.0f) {
208 throw new IllegalArgumentException("all factors must be < 1");
209 }
210 this.maxSize = maxSize;
211 this.blockSize = blockSize;
212 map = new ConcurrentHashMap<String,CachedBlock>(mapInitialSize,
213 mapLoadFactor, mapConcurrencyLevel);
214 this.minFactor = minFactor;
215 this.acceptableFactor = acceptableFactor;
216 this.singleFactor = singleFactor;
217 this.multiFactor = multiFactor;
218 this.memoryFactor = memoryFactor;
219 this.stats = new CacheStats();
220 this.count = new AtomicLong(0);
221 this.elements = new AtomicLong(0);
222 this.overhead = calculateOverhead(maxSize, blockSize, mapConcurrencyLevel);
223 this.size = new AtomicLong(this.overhead);
224 if(evictionThread) {
225 this.evictionThread = new EvictionThread(this);
226 this.evictionThread.start();
227 } else {
228 this.evictionThread = null;
229 }
230 this.scheduleThreadPool.scheduleAtFixedRate(new StatisticsThread(this),
231 statThreadPeriod, statThreadPeriod, TimeUnit.SECONDS);
232 }
233
234 public void setMaxSize(long maxSize) {
235 this.maxSize = maxSize;
236 if(this.size.get() > acceptableSize() && !evictionInProgress) {
237 runEviction();
238 }
239 }
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254 public void cacheBlock(String blockName, ByteBuffer buf, boolean inMemory) {
255 CachedBlock cb = map.get(blockName);
256 if(cb != null) {
257 throw new RuntimeException("Cached an already cached block");
258 }
259 cb = new CachedBlock(blockName, buf, count.incrementAndGet(), inMemory);
260 long newSize = size.addAndGet(cb.heapSize());
261 map.put(blockName, cb);
262 elements.incrementAndGet();
263 if(newSize > acceptableSize() && !evictionInProgress) {
264 runEviction();
265 }
266 }
267
268
269
270
271
272
273
274
275
276
277
278 public void cacheBlock(String blockName, ByteBuffer buf) {
279 cacheBlock(blockName, buf, false);
280 }
281
282
283
284
285
286
287 public ByteBuffer getBlock(String blockName, boolean caching) {
288 CachedBlock cb = map.get(blockName);
289 if(cb == null) {
290 stats.miss(caching);
291 return null;
292 }
293 stats.hit(caching);
294 cb.access(count.incrementAndGet());
295 return cb.getBuffer();
296 }
297
298 protected long evictBlock(CachedBlock block) {
299 map.remove(block.getName());
300 size.addAndGet(-1 * block.heapSize());
301 elements.decrementAndGet();
302 stats.evicted();
303 return block.heapSize();
304 }
305
306
307
308
309 private void runEviction() {
310 if(evictionThread == null) {
311 evict();
312 } else {
313 evictionThread.evict();
314 }
315 }
316
317
318
319
320 void evict() {
321
322
323 if(!evictionLock.tryLock()) return;
324
325 try {
326 evictionInProgress = true;
327 long currentSize = this.size.get();
328 long bytesToFree = currentSize - minSize();
329
330 if (LOG.isDebugEnabled()) {
331 LOG.debug("Block cache LRU eviction started; Attempting to free " +
332 StringUtils.byteDesc(bytesToFree) + " of total=" +
333 StringUtils.byteDesc(currentSize));
334 }
335
336 if(bytesToFree <= 0) return;
337
338
339 BlockBucket bucketSingle = new BlockBucket(bytesToFree, blockSize,
340 singleSize());
341 BlockBucket bucketMulti = new BlockBucket(bytesToFree, blockSize,
342 multiSize());
343 BlockBucket bucketMemory = new BlockBucket(bytesToFree, blockSize,
344 memorySize());
345
346
347 for(CachedBlock cachedBlock : map.values()) {
348 switch(cachedBlock.getPriority()) {
349 case SINGLE: {
350 bucketSingle.add(cachedBlock);
351 break;
352 }
353 case MULTI: {
354 bucketMulti.add(cachedBlock);
355 break;
356 }
357 case MEMORY: {
358 bucketMemory.add(cachedBlock);
359 break;
360 }
361 }
362 }
363
364 PriorityQueue<BlockBucket> bucketQueue =
365 new PriorityQueue<BlockBucket>(3);
366
367 bucketQueue.add(bucketSingle);
368 bucketQueue.add(bucketMulti);
369 bucketQueue.add(bucketMemory);
370
371 int remainingBuckets = 3;
372 long bytesFreed = 0;
373
374 BlockBucket bucket;
375 while((bucket = bucketQueue.poll()) != null) {
376 long overflow = bucket.overflow();
377 if(overflow > 0) {
378 long bucketBytesToFree = Math.min(overflow,
379 (bytesToFree - bytesFreed) / remainingBuckets);
380 bytesFreed += bucket.free(bucketBytesToFree);
381 }
382 remainingBuckets--;
383 }
384
385 if (LOG.isDebugEnabled()) {
386 long single = bucketSingle.totalSize();
387 long multi = bucketMulti.totalSize();
388 long memory = bucketMemory.totalSize();
389 LOG.debug("Block cache LRU eviction completed; " +
390 "freed=" + StringUtils.byteDesc(bytesFreed) + ", " +
391 "total=" + StringUtils.byteDesc(this.size.get()) + ", " +
392 "single=" + StringUtils.byteDesc(single) + ", " +
393 "multi=" + StringUtils.byteDesc(multi) + ", " +
394 "memory=" + StringUtils.byteDesc(memory));
395 }
396 } finally {
397 stats.evict();
398 evictionInProgress = false;
399 evictionLock.unlock();
400 }
401 }
402
403
404
405
406
407
408
409 private class BlockBucket implements Comparable<BlockBucket> {
410
411 private CachedBlockQueue queue;
412 private long totalSize = 0;
413 private long bucketSize;
414
415 public BlockBucket(long bytesToFree, long blockSize, long bucketSize) {
416 this.bucketSize = bucketSize;
417 queue = new CachedBlockQueue(bytesToFree, blockSize);
418 totalSize = 0;
419 }
420
421 public void add(CachedBlock block) {
422 totalSize += block.heapSize();
423 queue.add(block);
424 }
425
426 public long free(long toFree) {
427 LinkedList<CachedBlock> blocks = queue.get();
428 long freedBytes = 0;
429 for(CachedBlock cb: blocks) {
430 freedBytes += evictBlock(cb);
431 if(freedBytes >= toFree) {
432 return freedBytes;
433 }
434 }
435 return freedBytes;
436 }
437
438 public long overflow() {
439 return totalSize - bucketSize;
440 }
441
442 public long totalSize() {
443 return totalSize;
444 }
445
446 public int compareTo(BlockBucket that) {
447 if(this.overflow() == that.overflow()) return 0;
448 return this.overflow() > that.overflow() ? 1 : -1;
449 }
450 }
451
452
453
454
455
456 public long getMaxSize() {
457 return this.maxSize;
458 }
459
460
461
462
463
464 public long getCurrentSize() {
465 return this.size.get();
466 }
467
468
469
470
471
472 public long getFreeSize() {
473 return getMaxSize() - getCurrentSize();
474 }
475
476
477
478
479
480 public long size() {
481 return this.elements.get();
482 }
483
484
485
486
487 public long getEvictionCount() {
488 return this.stats.getEvictionCount();
489 }
490
491
492
493
494
495 public long getEvictedCount() {
496 return this.stats.getEvictedCount();
497 }
498
499
500
501
502
503
504
505 private static class EvictionThread extends Thread {
506 private WeakReference<LruBlockCache> cache;
507
508 public EvictionThread(LruBlockCache cache) {
509 super("LruBlockCache.EvictionThread");
510 setDaemon(true);
511 this.cache = new WeakReference<LruBlockCache>(cache);
512 }
513
514 @Override
515 public void run() {
516 while(true) {
517 synchronized(this) {
518 try {
519 this.wait();
520 } catch(InterruptedException e) {}
521 }
522 LruBlockCache cache = this.cache.get();
523 if(cache == null) break;
524 cache.evict();
525 }
526 }
527 public void evict() {
528 synchronized(this) {
529 this.notify();
530 }
531 }
532 }
533
534
535
536
537 static class StatisticsThread extends Thread {
538 LruBlockCache lru;
539
540 public StatisticsThread(LruBlockCache lru) {
541 super("LruBlockCache.StatisticsThread");
542 setDaemon(true);
543 this.lru = lru;
544 }
545 @Override
546 public void run() {
547 lru.logStats();
548 }
549 }
550
551 public void logStats() {
552 if (!LOG.isDebugEnabled()) return;
553
554 long totalSize = heapSize();
555 long freeSize = maxSize - totalSize;
556 LruBlockCache.LOG.debug("LRU Stats: " +
557 "total=" + StringUtils.byteDesc(totalSize) + ", " +
558 "free=" + StringUtils.byteDesc(freeSize) + ", " +
559 "max=" + StringUtils.byteDesc(this.maxSize) + ", " +
560 "blocks=" + size() +", " +
561 "accesses=" + stats.getRequestCount() + ", " +
562 "hits=" + stats.getHitCount() + ", " +
563 "hitRatio=" + StringUtils.formatPercent(stats.getHitRatio(), 2) + "%, "+
564 "cachingAccesses=" + stats.getRequestCachingCount() + ", " +
565 "cachingHits=" + stats.getHitCachingCount() + ", " +
566 "cachingHitsRatio=" +
567 StringUtils.formatPercent(stats.getHitCachingRatio(), 2) + "%, " +
568 "evictions=" + stats.getEvictionCount() + ", " +
569 "evicted=" + stats.getEvictedCount() + ", " +
570 "evictedPerRun=" + stats.evictedPerEviction());
571 }
572
573
574
575
576
577
578
579 public CacheStats getStats() {
580 return this.stats;
581 }
582
583 public static class CacheStats {
584
585 private final AtomicLong hitCount = new AtomicLong(0);
586
587
588
589
590
591
592 private final AtomicLong hitCachingCount = new AtomicLong(0);
593
594 private final AtomicLong missCount = new AtomicLong(0);
595
596
597
598
599 private final AtomicLong missCachingCount = new AtomicLong(0);
600
601 private final AtomicLong evictionCount = new AtomicLong(0);
602
603 private final AtomicLong evictedCount = new AtomicLong(0);
604
605 public void miss(boolean caching) {
606 missCount.incrementAndGet();
607 if (caching) missCachingCount.incrementAndGet();
608 }
609
610 public void hit(boolean caching) {
611 hitCount.incrementAndGet();
612 if (caching) hitCachingCount.incrementAndGet();
613 }
614
615 public void evict() {
616 evictionCount.incrementAndGet();
617 }
618
619 public void evicted() {
620 evictedCount.incrementAndGet();
621 }
622
623 public long getRequestCount() {
624 return getHitCount() + getMissCount();
625 }
626
627 public long getRequestCachingCount() {
628 return getHitCachingCount() + getMissCachingCount();
629 }
630
631 public long getMissCount() {
632 return missCount.get();
633 }
634
635 public long getMissCachingCount() {
636 return missCachingCount.get();
637 }
638
639 public long getHitCount() {
640 return hitCount.get();
641 }
642
643 public long getHitCachingCount() {
644 return hitCachingCount.get();
645 }
646
647 public long getEvictionCount() {
648 return evictionCount.get();
649 }
650
651 public long getEvictedCount() {
652 return evictedCount.get();
653 }
654
655 public double getHitRatio() {
656 return ((float)getHitCount()/(float)getRequestCount());
657 }
658
659 public double getHitCachingRatio() {
660 return ((float)getHitCachingCount()/(float)getRequestCachingCount());
661 }
662
663 public double getMissRatio() {
664 return ((float)getMissCount()/(float)getRequestCount());
665 }
666
667 public double getMissCachingRatio() {
668 return ((float)getMissCachingCount()/(float)getRequestCachingCount());
669 }
670
671 public double evictedPerEviction() {
672 return ((float)getEvictedCount()/(float)getEvictionCount());
673 }
674 }
675
676 public final static long CACHE_FIXED_OVERHEAD = ClassSize.align(
677 (3 * Bytes.SIZEOF_LONG) + (8 * ClassSize.REFERENCE) +
678 (5 * Bytes.SIZEOF_FLOAT) + Bytes.SIZEOF_BOOLEAN
679 + ClassSize.OBJECT);
680
681
682 public long heapSize() {
683 return getCurrentSize();
684 }
685
686 public static long calculateOverhead(long maxSize, long blockSize, int concurrency){
687
688 return CACHE_FIXED_OVERHEAD + ClassSize.CONCURRENT_HASHMAP +
689 ((long)Math.ceil(maxSize*1.2/blockSize)
690 * ClassSize.CONCURRENT_HASHMAP_ENTRY) +
691 (concurrency * ClassSize.CONCURRENT_HASHMAP_SEGMENT);
692 }
693
694
695
696 private long acceptableSize() {
697 return (long)Math.floor(this.maxSize * this.acceptableFactor);
698 }
699 private long minSize() {
700 return (long)Math.floor(this.maxSize * this.minFactor);
701 }
702 private long singleSize() {
703 return (long)Math.floor(this.maxSize * this.singleFactor * this.minFactor);
704 }
705 private long multiSize() {
706 return (long)Math.floor(this.maxSize * this.multiFactor * this.minFactor);
707 }
708 private long memorySize() {
709 return (long)Math.floor(this.maxSize * this.memoryFactor * this.minFactor);
710 }
711
712 public void shutdown() {
713 this.scheduleThreadPool.shutdown();
714 }
715 }