인피니스팬데이터그리드따라잡기 (@jco 2014)

of 49 /49
JBoss 인피니스팬 데이터그리드 플랫폼 따라잡기 전재홍/JBoss User Group

Author: jaehong-cheon

Post on 19-Aug-2014

205 views

Category:

Engineering


5 download

Embed Size (px)

DESCRIPTION

인피니스팬에 관해서 2014년 JCO에서 발표한 자료입니다. 이전 것과 유사하고 스프링시큐리티와 연동하여 세션 클러스터링 하는 예제를 포함하였습니다.

TRANSCRIPT

  • JBoss /JBoss User Group
  • Distributed Cache Fast access to data Performance boost Elasticity High Availability JSR 107 (Temporary Caching for the Java Platform) Read, write, expiration, write-through, distribution
  • Distributed Cache (cont.) Persistence (Database) Distributed Cache Cluster Node Node Node App App App
  • In-Memory Data Grid Evolution of distributed caches Clustered by nature (shared across multiple servers) Low response time High throughput Predictable scalability High Availability Querying Task Execution (Map/Reduce) JSR 347 (Data Grid for the Java Platform)
  • In-Memory Data Grid (cont.) Persistence (DataStore) In-Memory Data Grid Node Node Node App App App
  • IMDG for What? Sharing data (session, app states) Toolkit for clustering Performance (caching, in-memory processing) Scalability Database on cloud
  • Players Oracle Coherence GridGain HazelCast IBM eXtreme Scale GigaSpaces VmWare GemFire Terracotta ScaleOut StateServer JBoss Infinispan
  • Cache vs. Data Grid JSR 107 - Temporary Caching for the Java Platform Basic interaction(read, write, expiry) Transactions with JTA compatibility Listener Persistence: read-through, write-through, write-behind Annotations javax.cache.* JSR 347 - Data Grids for the Java Platform Asynchronous, non-blocking API Distributed code execution and map/reduce API Group API for co-location Annotations (CDI) Eventually Consistent API Querying Configuration javax.datagrid.*
  • Infinispan Distributed in-memory key/value data grid and cache Distributed as library and server Manageable Open Source DefaultCacheManager manager = new DefaultCacheManager(); // Cache cache = manager.getCache(); Cache cache = manager.getCache(myCache);
  • Architecture: as library Embedded library on the same JVM with Application Infinispan JVM App
  • Architecture: as library, clustered Cluster Infinispan JVM App Infinispan JVM App Infinispan JVM App Application doesnt know its on cluster l Use as library More features Richer APIs Programmatic/ Declarative configuration Extendable/ embeddable Faster (API call)
  • Architecture: as server, clustered Use as server Remote Data tier shared by multi-apps App doesnt affect cluster Non-java clients C++, .NET, Ruby , Python, Java Cluster Infinispan JVM Infinispan JVM Infinispan JVM App App App
  • Architecture: multi clusters Multi-clusters By replication By persistence By replication to other clust er (topology a ware) Cluster Infinispan JVM Infinispan JVM Cluster Infinispan JVM Infinispan JVM persistence
  • Clustering Peer-to-Peer No central master, no single point of failure, no single bottle neck JGroups Reliable multicast communication library, nodes discovery, sharing data, performing cluster scaling Consistent Hash Hash based data distribution How it finds where data locates Linear in nature: throughput, capacity
  • Cluster Mode: Replication() Replication Mode Cache on Server 1 K,V Cache on Server 2 K,V Cache on Server 4 K,V Cache on Server 3 K,V cache.put(K,V)
  • Cluster Mode: Distribution() Distribution Mode(numOwners=2) Cache on Server 1 K,V Cache on Server 2 K,V Cache on Server 4 Cache on Server 3 cache.put(K,V) cache.get(K,V)
  • Cluster Mode: Invalidation() Invalidation Mode Cache on Server 1 K,V2 Cache on Server 2 K,V Cache on Server 4 Cache on Server 3 cache.put(K,V2) DB
  • Persistence Used for durability Cache Store - Persistence Storage File System, Cloud, Remote, JDBC, JPA, LevelDB, Cassandra, HBase, MongoDB, BerkeleyDB, JDBM, REST CacheLoader, CacheWriter Read-through, write-through, write-behind Passivation, activation Store chain Shared store
  • Persistence (cont.) Passivation write to persistence when evicted from memory (default) Activation read to memory and remove from persistence
  • Transaction JTA Transaction Support Support MVCC (Multi-Versioned Concurrency Control) Isolation Level READ_COMMITTED (default) REPEATABLE_READ Locking Mode Optimistic Lock (default) Pessimistic Lock Locking Lock timeout Lock striping
  • Query Query on values JBoss Hibernate Search + Apache Lucene Index Directory Lucene Directory: in-memory, file system, JDBC Infinispan Directory Distributed queries
  • Distributed Execution Executes codes on distributed nodes Through a standard JDK ExecutorService interface Use DistributedCallable extends java.util.concurrent.Callable
  • Map/Reduce Based on Distributed Execution Framework Mapper, Reducer, Collator, MapReduceTask public interface Callator { R collate(Map); } public interface Mapper extends Serializable { void map(KIn key, VIn value, Collector collector); } public interface Reducer extends Serializable { VOut reduce(KOut reducedKey, Iterator iter); }
  • Configuration: Declarative Eviction() on the memory Expiration() on the cluster
  • Configuration: Programmatic Configuration Based on XML Programmatic configuration DefaultCacheManager manager = new DefaultCacheManager("infinispan-config.xml"); Configuration baseConf = manager.getDefaultCacheConfiguration(); Configuration config =new ConfigurationBuilder(). read(baseConf).expiration().lifespan(50000).build(); manager.defineConfiguration(programmaticCache, config); Cache cache = manager.getCache("secureContextCache"); DefaultCacheManager manager = new DefaultCacheManager(); Configuration config = new ConfigurationBuilder() .loaders() .shared(false).passivation(false).preload(false) .addCacheLoader() .cacheLoader(new JdbcStringBasedCacheStore()) .addProperty("connectionFactoryClass","org.infinispan.loaders.jdbc .connectionfactory.ManagedConnectionFactory") .addProperty("datasourceJndiLocation", "java:jboss/datasources/MySQLDS") .addProperty("userName", "root") .addProperty("password", "admin") .async().threadPoolSize(10).build(); manager.defineConfiguration(programmaticCache, config); Cache cache = manager.getCache("secureContextCache");
  • Listener Listener on CacheManager Node join/ leave, Cache start/ stop Cache CRUD, Eviction/ Passivation Rehashing/ Transaction completion @Listener public class SimpleListener { @CacheEntryCreated public void dataAdded(CacheEntryCreatedEvent event) { if (event.isPre()) { System.out.println("Before creating the entry:" + event.getKey()); } else { System.out.println("After creating the entry:" + event.getKey()); } } DefaultCacheManager manager = new DefaultCacheManager(); manager.addListener(listener); Cache cache = manager.getCache(); cache.addListener(listener);
  • Asynchronous APIs put() and get() and remove() are synchronous They wait for RPC and Locks (and maybe cache stores) The asynchronous API returns NotifyingFuture Events are fired on completion of the operation NotifyingFuture future = c.removeAsync(key); future.attachListener(new FutureListener() { @Override public void futureDone(Future future) { try { future.get(); System.out.printf ("The entry stored under key %s has been removed.", key); } catch (ExecutionException e) { System.out.printf("Failed to remove %s!", key); } } });
  • Spring Integration Infinispan provider for Spring cache abstraction infinispan-spring.jar @Cacheable(value = "secureContextCache", key="#contextId") public SecureLayerContext getSecureLayerContext(String contextId) { return null; } @CachePut(value = "secureContextCache", key="#contextId") public SecureLayerContext setSecureLayerContext(String contextId, SecureLayerContext secureLayerContext) { return secureLayerContext; } @CacheEvict(value = "secureContextCache", key="#contextId") public void removeSecureLayerContext(String contextId) { // Intentionally blank }
  • Customizing with Interceptors Infinispan follows Interceptor, Command/Visitor design patterns Custom interceptor extends BaseCustomInterceptor
  • Infinispan on JBoss AS 7 (WildFly 8) Used for session clustering, Hibernate L2 cache Application gets cache with JNDI name using @Resource XML Configuration in server configuration file
  • Marshalling JBoss Marshalling framework used for POJOs User can provide custom Externalizer impls for non-Serializable object Custome marshaller can be provided as well
  • Client Implementations
  • Monitoring/Management Mbeans on CacheManager, Cache RHQ (JON, JBoss Operations Network)
  • Radar Gun Data grid and distributed cache benchmarking framework Built to test Infinispan and other distributed data grid platforms https://github.com/radargun/radargun
  • Use Cases: In Streaming Processing Infinispan Data Grid
  • Use Cases: Data Grid Platform In-Memory , 6 , 2013
  • Use Case: Session Clustering Store session information into cache in Spring MVC Interceptor
  • Case Study: Session Clustering #1 Spring Cache Abstraction ConcurrentHashMap, EHCache Infinispan #2 SecurityContext HTTP Session (HttpSessionSecurityContextRepository) SecurityContextRepository CacheSecurityContextRepository HTTP Session
  • Case Study: Infinispan with Spring Loadbalancer Client Client Client Infinispan Application Services Spring Security WAS Infinispan Application Services Spring Security WAS Infinispan Application Services Spring Security WAS . . .
  • Infinispan with Spring (cont.) infinispan-config.xml
  • Infinispan with Spring (cont.) spring-cache-config.xml SpringEmbeddedCacheManagerFactoryBean public class MyCacheManagerFactoryBean extends SpringEmbeddedCacheManagerFactoryBean { @Override public void afterPropertiesSet() throws Exception { super.afterPropertiesSet(); addListeners(); } private void addListeners() throws Exception { SpringEmbeddedCacheManager cacheManager = getObject(); if (cacheManager.getNativeCacheManager() instanceof EmbeddedCacheManager) { cacheManager.getNativeCacheManager().addListener(new MyCacheManagerListener()); } Collection cacheNames = cacheManager.getCacheNames(); for (String cacheName : cacheNames) { SpringCache cache = cacheManager.getCache(cacheName); if (cache.getNativeCache() instanceof Cache) { ((Cache)cache.getNativeCache()).addListener(new MyCacheListener()); }}}}
  • Infinispan with Spring (cont.) Dao Implementation @Named("SecurityContextDao") public class SecurityContextDaoImpl implements SecurityContextDao { @Cacheable(value = "securityContextCache", key="#key") public SecurityContext getSecurityContext(String key) { return null; } @CachePut(value = "securityContextCache", key="#key") public SecurityContext setSecurityContext(String key, SecurityContext securityContext) { return securityContext; } @CacheEvict(value = "securityContextCache", key="#key") public void removeSecurityContext(String key) { // Intentionally blank } }
  • Infinispan with Spring (cont.) Spring Securitys SecurityContextRepository public class MyCacheSecurityContextRepository implements SecurityContextRepository { @Inject SecurityContextDao securityContextDao; public SecurityContext loadContext(HttpRequestResponseHolder requestResponseHolder) { ... return securityContextDao.getSecurityContext(authToken); } public void saveContext(SecurityContext context, HttpServletRequest request, HttpServletResponse response) { ... securityContextDao.setSecurityContext(authToken, context); ... } public boolean containsContext(HttpServletRequest request) { ... return securityContextDao.getSecurityContext(key) != null; } }
  • Infinispan with Spring (cont.) spring-security-config.xml
  • References infinispan.org blog.infinispan.org infinispan-ko.blogspot.com facebook.com/groups/infinispan red.ht/data-grid coherence.oracle.com
  • .