Guava Overview Part 2 Bucharest JUG #2

2,115 views

Published on

A few slides on Cache and Service abstractions from Guava.

Published in: Technology, News & Politics
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,115
On SlideShare
0
From Embeds
0
Number of Embeds
33
Actions
Shares
0
Downloads
0
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Guava Overview Part 2 Bucharest JUG #2

  1. 1. Guava Overview. Part 2 Andrei Savu / Bucharest JUG #2 asavu @ axemblr.com
  2. 2. About meFounding Partner @ Axemblr.comApache Whirr PMC Memberjclouds committerConnect with me on LinkedIn
  3. 3. @ AxemblrBuilding a tool and a platform for managingApache Hadoop clusters on cloud infrastructureAxemblr Tool for Cloudera CDH3 http://axemblr.com/toolAxemblr Service for Apache Hadoop http://axemblr.com/service
  4. 4. Plan● What is Guava?● Caches● Concurrency Services
  5. 5. Quick Intro
  6. 6. What is Guava?Open-source version of Googles core Javalibraries[...] carefully designed, tested, optimized andused in production at GoogleYou dont need to write them, test them, oroptimize them: you can just use them.
  7. 7. What is Guava? (cont)Battle-tested in production at GoogleStaggering numbers of unit tests. >110.000 asof January 2012 (generated)Lives near the bottom of the stack, right on topof the JDK itself
  8. 8. Caches
  9. 9. Caches - When to use?When a value is either● expensive to compute● expensive to retrieveMore or less similar to ConcurrentMap +automatic eviction
  10. 10. Caches - When to use? (cont)● spend memory to improve speed● keys are queried multiple times● your application would work if every value was evicted immediately● you have less data than RAM
  11. 11. Caches - Example #1
  12. 12. Caches - Example #2
  13. 13. Caches - Example #3cache.put(key, value)A Cache can be accessed as a Map:Cache.asMap() returns a ConcurrentMap
  14. 14. Caches - Eviction #1By size CacheBuilder.maximumSize(long)By custom weight CacheBuilder.weigher(Weigher) CacheBuilder.maximumWeight(long)
  15. 15. Caches - Eviction #2By time expireAfterAccess(long, TimeUnit) expireAfterWrite(long, TimeUnit)
  16. 16. Caches - Eviction #3Reference-based eviction CacheBuilder.weakKeys() CacheBuilder.weakValues() CacheBuilder.softValues()Note: all of them are using == for equality and*not* equals()
  17. 17. Caches - Eviction #4Explicit Cache.invalidate(key) Cache.invalidateAll(keys) Cache.invalidateAll()
  18. 18. Caches - Removal ListenersCacheBuilder .removalListener(RemovalListener)Be aware of long running listeners. They areblocking the queue.RemovalListeners .asynchronous(RemovalListener, Executor)
  19. 19. Caches - CleanupAutomatic eviction happens on writesExplicit Cache.cleanup()(you can put this in a thread)
  20. 20. Caches - RefreshLoadingCache.refresh(K)CacheLoader.reload(K, V)(if the new value can be computed from the oldone)
  21. 21. Cache - StatisticsCacheBuilder.recordStats()CacheStats Cache.stats() hitRate() averageLoadPenalty() evictionCount()
  22. 22. Service
  23. 23. What is this?Object with operational stateStart and stop methodsE.g. web servers, RPC servers, timers etc.Non-trivial if multiple threads are involved.
  24. 24. Lifecycle● Service.State.NEW to● Service.State.STARTING to● Service.State.RUNNING to● Service.State.STOPPING to● Service.State.TERMINATEDOn failure:● Service.State.FAILEDA stopped service may not be restarted.
  25. 25. Lifecycle #2asynchronously: start - returns ListenableFuture stopsynchronously startAndWait stopAndWait
  26. 26. Implementations
  27. 27. AbstractIdleServiceHas nothing to run:
  28. 28. AbstractExecutionThreadService
  29. 29. AbstractScheduledServiceSimilar to AbstractExecutionThreadServiceOverride runOneIterationImplement scheduler() newFixedRateSchedule(initialDelay, delay,TimeUnit) newFixedDelaySchedule(initialDelay, delay,TimeUnit)
  30. 30. Next?Functional IdiomsRangesI/OHashingMathReflectionConcurrency (ListenableFuture)EventBusSee Guava Wiki
  31. 31. Thanks! Questions?Andrei Savu / asavu @ axemblr.com

×