Professional Open Source™

JBossCache
In-Memory Replicated Transactional Cache

© JBoss, Inc. 2003-2005.

8/2/2005

1

Topics
Professional Open Source™

– – – – –

What is JBossCache ? Architecture How do I use it ? – API Eviction Policies and CacheLoading JBossCache/AOP and POJOs

© JBoss, Inc. 2003-2005.

2

1

What is JBossCache ?
Professional Open Source™

Two separate implementations
– TreeCache – TreeCacheAOP

Available as stand-alone or embeddable (MBean)
– TreeCacheAOP requires JBossAOP runtime – Can be used with other app servers as well

TreeCache
– Stores and replicates values from a tree structure (hence the name) – Each value is associated with a path and key

TreeCacheAOP
– Can manage caching and replication on full Java objects – Object Oriented cache

© JBoss, Inc. 2003-2005.

3

What is TreeCache ?
Professional Open Source™

Local or replicated
– Synchronous or asynchronous replication (using JGroups)

Transactional or non-transactional
– Transactional • Replication at TX commit • DB isolation levels supported • Support for pluggable TxManagers – Non-Transactional • Replication after each modification

Pluggable eviction policies
– Ships with time-based and size-based (LRU) policies

Cache loader
– Persistent backend store (load - store)

© JBoss, Inc. 2003-2005.

4

2

Architecture - Structure
Professional Open Source™

a

Tree structure
– Each node has a name and zero or more children – Can be navigated recursively from node to node or using a fully qualified name (/a/d/g) – Multiple roots per cache allowed – Each node is a map with keys and values

b

c

d

e

f

g

/a/d/g
h

– Graphs not supported

© JBoss, Inc. 2003-2005.

5

Architecture - Structure
Professional Open Source™

Each node has
– – – – a name (Object) ("address") a fully qualified name (FQN) ("/322649/address") child node(s) a hashmap (attributes)

Navigation
– Get root node and then navigate yourself (Node.getChildren()) – External naming for nodes • Strings: "/322649/address" • FQNs: FQN.create(new Object[]{new Integer(322649), "address"})

Replication
– Keys and values for replicated tree cache must be serializable

© JBoss, Inc. 2003-2005.

6

3

Replication
Professional Open Source™

If transaction is present, replication occurs at transaction commit time
– Multiple changes can be made to items in the cache within a tx – Single replication message is sent as part of the commit handling – If there is a rollback, we have zero replication cost

Changes to items without a transaction are replicated immediately
– If there are 1000 changes, there will be 1000 replication messages – Compare to 1000 changes in a tx, 1 replication message

© JBoss, Inc. 2003-2005.

7

Configuration
Professional Open Source™

replyAsync-service.xml <server> <server> <mbean code="org.jboss.cache.TreeCache" name="jboss.cache:service=TreeCache"> <mbean code="org.jboss.cache.TreeCache" name="jboss.cache:service=TreeCache"> ... ... <attribute name="IsolationLevel">REPEATABLE_READ</attribute> <attribute name="IsolationLevel">REPEATABLE_READ</attribute> <!-- Valid modes are LOCAL, REPL_ASYNC and REPL_SYNC --> <!-- Valid modes are LOCAL, REPL_ASYNC and REPL_SYNC --> <attribute name="CacheMode">REPL_ASYNC</attribute> <attribute name="CacheMode">REPL_ASYNC</attribute> <!-- Just used for async repl: use a replication queue --> <!-- Just used for async repl: use a replication queue --> <attribute name="UseReplQueue">false</attribute> <attribute name="UseReplQueue">false</attribute> <!-- Replication interval for replication queue (in ms) --> <!-- Replication interval for replication queue (in ms) --> <attribute name="ReplQueueInterval">0</attribute> <attribute name="ReplQueueInterval">0</attribute> <!-- Max number of elements which trigger replication --> <!-- Max number of elements which trigger replication --> <attribute name="ReplQueueMaxElements">0</attribute> <attribute name="ReplQueueMaxElements">0</attribute> ... ...

© JBoss, Inc. 2003-2005.

8

4

Configuration
Professional Open Source™

replyAsync-service.xml … … <!-- Whether or not to fetch state on joining a cluster --> <!-- Whether or not to fetch state on joining a cluster --> <attribute name="FetchStateOnStartup">true</attribute> <attribute name="FetchStateOnStartup">true</attribute> <!-- The max amount of time (in milliseconds) we wait until the initial state (ie. the contents of the <!-- The max amount of time (in milliseconds) we wait until the initial state (ie. the contents of the cache) are retrieved from existing members in a clustered environment --> cache) are retrieved from existing members in a clustered environment --> <attribute name="InitialStateRetrievalTimeout">5000</attribute> <attribute name="InitialStateRetrievalTimeout">5000</attribute> <!-- Number of milliseconds to wait until all responses for a synchronous call have been received. --> <!-- Number of milliseconds to wait until all responses for a synchronous call have been received. --> <attribute name="SyncReplTimeout">10000</attribute> <attribute name="SyncReplTimeout">10000</attribute> <!-- Max number of milliseconds to wait for a lock acquisition --> <!-- Max number of milliseconds to wait for a lock acquisition --> <attribute name="LockAcquisitionTimeout">15000</attribute> <attribute name="LockAcquisitionTimeout">15000</attribute> <!-- Name of the eviction policy class. Not supported now. --> <!-- Name of the eviction policy class. Not supported now. --> <attribute name="EvictionPolicyClass"></attribute> </mbean> <attribute name="EvictionPolicyClass"></attribute> </mbean> </server> </server>

© JBoss, Inc. 2003-2005.

9

API
Professional Open Source™

put(FQN name, Map data)
– Adds a new node to the tree and sets its data. If the node does not yet exist, it will be created. – Also, parent nodes will be created if not existent. If the node already has data, then the new data will override the old one.

put(FQN name, Object key, Object val)
– Adds a key and value to a given node. – If the node doesn't exist, it will be created.

boolean exists(FQN name)
– Checks whether a given node exists in the tree. Does not acquire any locks in doing so (result may be dirty read)

© JBoss, Inc. 2003-2005.

10

5

API
Professional Open Source™

Object get(FQN name, Object key)
– Finds a node given its name and returns the value associated with a given key in its data map

Node get(FQN name)
– Use for tree navigation

remove(FQN name)
– Removes and its children from the tree

remove(FQN name, Object key)
– Removes key from the node's hashmap

© JBoss, Inc. 2003-2005.

11

Sample Code
Professional Open Source™

try {{ try tx=(UserTransaction)new tx=(UserTransaction)new InitialContext(p).lookup("UserTransaction"); InitialContext(p).lookup("UserTransaction"); TreeCache c=new TreeCache("test", null, 10000); TreeCache c=new TreeCache("test", null, 10000); c.setMode(TreeCache.REPL_ASYNC); c.setMode(TreeCache.REPL_ASYNC); c.start(); c.start(); tx.begin(); tx.begin(); c.put("/a/b/c", "age", new Integer(38)); c.put("/a/b/c", "age", new Integer(38)); c.put("/a/b/c", "age", new Integer(39)); c.put("/a/b/c", "age", new Integer(39)); tx.commit(); tx.commit(); assertEquals(new Integer(39), c.get("/a/b/c", "age")); assertEquals(new Integer(39), c.get("/a/b/c", "age")); }} catch(Throwable t) {{ catch(Throwable t) if(tx != null) tx.rollback(); if(tx != null) tx.rollback(); fail(t.toString()); fail(t.toString()); }}

© JBoss, Inc. 2003-2005.

12

6

Sample Step-by-Step
Professional Open Source™

a 3
create

1 2 b

tx.begin() put(”a/b/c”, ”age”, 38)

3

create

(X) Lock

C 4
age 38

Repl. Queue

© JBoss, Inc. 2003-2005.

13

Sample Step-by-Step
Professional Open Source™

a 3
create

1 2 b

tx.begin() put(”a/b/c”, ”age”, 38)

5 6
(X) Lock

put(”a/b/c”, ”age”, 39) tx.commit() Repl. Queue

3

create

Repl. Msg 8
UseReplQueue?

C 4
age 39 38

7

ASYNC

9

Thread continue....

© JBoss, Inc. 2003-2005.

14

7

Cluster Wide Locking
Professional Open Source™

Cluster-wide locking
– Access locks on nodes are local, until replication time – If locks can’t be acquired on all nodes at replication, timeout and rollback of the change

Distributed locking not implemented
– Be careful with concurrent write-access across process when using synchronous replication same node access leads to deadlock timeout

A TxID = 1 B
Local (X) Lock

Acquire write-lock on Node B ?

A
Commit

SYNC Replication Commit Timeout
Acquire write-lock on Node B ?

TxID = 2 B
Local (X) Lock

© JBoss, Inc. 2003-2005.

15

CacheLoader
Professional Open Source™

Plugin implementing org.jboss.cache.loader.CacheLoader
– Store to and load from a (persistent) store – Counterpart to eviction policies

On access to an element in the cache
– If not in cache --> use CacheLoader to load from store – If modified: use CacheLoader to persist to store

On startup
– Prelolad parts of cache (warm cache), e.g. product catalog – Transfer transient and persistent state from another node in a cluster

Available CacheLoader implementations
– – – – Filesystem: simple example, no transactional support JDBC: use a relational database Hierarchical: delegate to another cache Sleepycat‘s Berkeley DB Java Edition: fast transactional database

© JBoss, Inc. 2003-2005.

16

8

CacheLoader
Professional Open Source™

Provide persistent backend for the cached data
– Large datasets overflow memory, only keep the needed data in-memory – Unused data evicted to persistent store (eviction policies)
<!-- ==================================================================== --> <!-- ==================================================================== --> <!-- Defines TreeCache configuration --> <!-- Defines TreeCache configuration --> <!-- ==================================================================== --> <!-- ==================================================================== --> <mbean code="org.jboss.cache.TreeCache" name="jboss.cache:service=TreeCache"> <mbean code="org.jboss.cache.TreeCache" name="jboss.cache:service=TreeCache"> <attribute name="CacheLoaderClass">org.jboss.cache.loader.bdbje.BdbjeCacheLoader</attribute> <attribute name="CacheLoaderClass">org.jboss.cache.loader.bdbje.BdbjeCacheLoader</attribute> <!-- attribute name="CacheLoaderClass">org.jboss.cache.loader.FileCacheLoader</attribute --> <!-- attribute name="CacheLoaderClass">org.jboss.cache.loader.FileCacheLoader</attribute --> <attribute name="CacheLoaderConfig">c:\tmp\bdbje</attribute> <attribute name="CacheLoaderConfig">c:\tmp\bdbje</attribute> <attribute name="CacheLoaderShared">true</attribute> <attribute name="CacheLoaderShared">true</attribute> <attribute name="CacheLoaderPreload">/</attribute> <attribute name="CacheLoaderPreload">/</attribute> <attribute name="CacheLoaderFetchTransientState">false</attribute> <attribute name="CacheLoaderFetchTransientState">false</attribute> <attribute name="CacheLoaderFetchPersistentState">true</attribute> <attribute name="CacheLoaderFetchPersistentState">true</attribute> </mbean> </mbean>

© JBoss, Inc. 2003-2005.

17

CacheLoader
Professional Open Source™

CacheLoaderShared
– Whether using a single persistence store across cluster or one per cache instance

CacheLoaderPreload
– Preloading items from the persistence store at startup – Notice that root (”/”) loads entire data available to cache memory, usually you want to restrict this to a dataset that you anticipate that data is going to be needed frequently (e.g. ”/wombat/phonebook”) – Everything else is loaded lazily

CacheLoaderFetchTransientState
– When joining a cluster, do we want to fetch the in-memory state of other caches in the same group

CacheLoaderFetchPersistentState
– When joining a cluster, and not sharing persistent stores across the group, should we fetch the persistent state of other cache nodes in group

© JBoss, Inc. 2003-2005.

18

9

Eviction Policy
Professional Open Source™

Works with the CacheLoader to move items in and out of cache
– Works locally – evictions are not propagated across cluster

Pluggable policy
– Implement from org.jboss.cache.eviction.EvictionPolicy and org.jboss.cache.TreeCacheListener interfaces – EvictionPolicy interface specifies behavior for eviction and configuration – TreeCacheListener specifies the node event notification handling

Currently has
– org.jboss.cache.eviction.LRUPolicy – org.jboss.cache.eviction.AopLRUPolicy (for aop only)

© JBoss, Inc. 2003-2005.

19

LRU Eviction Policy
Professional Open Source™

– LRU-based algorithm that has both time-based and node size-based policy – Region based. i.e., you can specify eviction policy per region where each region is a sub-tree branch, e.g., /org/jboss/test/. There is default region for everything else. – Region order is important, e.g., /org/jboss/data has to come before /org/jboss – Eviction policy is applied locally only, i.e., each node has its own local eviction timer

© JBoss, Inc. 2003-2005.

20

10

Eviction Policy XML Configuration
Professional Open Source™

<!-- Name of the eviction policy class. --> <!-- Name of the eviction policy class. --> <attribute name="EvictionPolicyClass">org.jboss.cache.eviction.LRUPolicy</attribute> <attribute name="EvictionPolicyClass">org.jboss.cache.eviction.LRUPolicy</attribute> <!-- Specific eviction policy configurations. This is LRU --> <!-- Specific eviction policy configurations. This is LRU --> <attribute name="EvictionPolicyConfig"> <attribute name="EvictionPolicyConfig"> <config> <config> <attribute name="wakeUpIntervalSeconds">5</attribute> <attribute name="wakeUpIntervalSeconds">5</attribute> <!-- Cache wide default --> <!-- Cache wide default --> <region name="/_default_"> <region name="/_default_"> <attribute name="maxNodes">5000</attribute> <attribute name="maxNodes">5000</attribute> <attribute name="timeToIdleSeconds">1000</attribute> <attribute name="timeToIdleSeconds">1000</attribute> </region> </region> <!– User-defined region --> <!– User-defined region --> <region name="/org/jboss/data"> <region name="/org/jboss/data"> <attribute name="maxNodes">5000</attribute> <attribute name="maxNodes">5000</attribute> <attribute name="timeToIdleSeconds">1000</attribute> <attribute name="timeToIdleSeconds">1000</attribute> </region> </region> </config> </config> </attribute> </attribute> </attribute> </attribute>

© JBoss, Inc. 2003-2005.

21

Replication (shared datastore)
Professional Open Source™

Process A
a

Process B
a

b

c

d

b

c

d

e

f

g

e

f

g

h

h

read/write

read

Backend storage

© JBoss, Inc. 2003-2005.

22

11

Replication (unshared datastores)
Professional Open Source™

Process A
a

Process B
a

b

c

d

b

c

d

e

f

g

e

f

g

h

h

Backend storage

© JBoss, Inc. 2003-2005.

23

Professional Open Source™

TreeCacheAOP
Object Oriented Caching

© JBoss, Inc. 2003-2005.

8/2/2005

24

12

TreeCacheAOP - What is it ?
Professional Open Source™

Subclass of TreeCache
– Enables managing of entire Java object structures instead of managing key/value tuples in TreeCache

Uses a combination of reflection and AOP to manage objects
– POJOs can be replicated across processes – POJOs can be persisted using a CacheLoader – POJO-specific eviction policies

© JBoss, Inc. 2003-2005.

25

TreeCacheAOP - Features
Professional Open Source™

Adds 3 methods:
– putObject(FQN name, Object pojo) – Object getObject(FQN name) – removeObject(FQN name)

Objects are added once, AOP keeps track of state changes and replicates on TX commit
– No need to call putObject() again – Only fields that are modified are replicated

POJOs do not need to implement Serializable

© JBoss, Inc. 2003-2005.

26

13

TreeCacheAOP - Mapping
Professional Open Source™

Uses reflection to discover the structure of a POJO
– Requires a jboss-aop.xml to declare user-defined class to be “advisable“ – Uses AOP to keep track of changes to a POJO

putObject() breaks an object apart and maps it to the TreeCache
– Primitive fields are mapped to entries in a node's attributes – Complex fields are mapped to child nodes (recursively) • Transparent handling of object hierarchies • No need for one-to-one, one-to-many, etc relationship declarations • POJO inheritance hierarchy is preserved – We dynamically add a field interceptor to each complex object to keep track of state changes

© JBoss, Inc. 2003-2005.

27

Example of a jboss-aop.xml
Professional Open Source™

<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?> <aop> <aop> <prepare expr="field(* $instanceof{org.jboss.test.cache.test.standAloneAop.Student}->*)" /> <prepare expr="field(* $instanceof{org.jboss.test.cache.test.standAloneAop.Student}->*)" /> <prepare expr="field(* $instanceof{org.jboss.test.cache.test.standAloneAop.Address}->*)" /> <prepare expr="field(* $instanceof{org.jboss.test.cache.test.standAloneAop.Address}->*)" /> <prepare expr="field(* $instanceof{org.jboss.test.cache.test.standAloneAop.Person}->*)" /> <prepare expr="field(* $instanceof{org.jboss.test.cache.test.standAloneAop.Person}->*)" /> </aop> </aop>

© JBoss, Inc. 2003-2005.

28

14

TreeCacheAOP - Interceptors
Professional Open Source™

Each interceptor remembers the node to which it maps
– On field read: interceptor returns the value from the TreeCache – On field write: interceptor updates the associated node's attributes, e.g. – person.getAddress().setCity("San Jose“) generates a put("/322649/addr", "city", "San Jose")

On TX commit: modified fields are replicated and written back to the POJO

© JBoss, Inc. 2003-2005.

29

TreeCacheAOP - Mapping
Professional Open Source™

/

Person p (key=/persons/322649) persons name: „Bela“ addr hobbies 322649
name Bela

city

San Jose 95124

city: „ San Jose“ addr zip: 95124

zip

hobbies

0

1

2

© JBoss, Inc. 2003-2005.

30

15

TreeCacheAOP - Mapping
Professional Open Source™

Object relationship management has reference counting
– Object instance that is referenced more than once is moved to an internal area
Person joe ==new Person(); Person joe new Person(); joe.setName("Joe Black"); joe.setName("Joe Black"); joe.setAge(31); joe.setAge(31);

Persons

Internal
Person mary ==new Person(); Person mary new Person(); mary.setName("Mary White"); mary.setName("Mary White"); mary.setAge(30); mary.setAge(30);

1
name age Joe 31

2
name age Mary 30

Address
city Sunnyvale street 123 Al... zip 94086

Address addr ==new Address(); Address addr new Address(); addr.setCity("Sunnyvale"); addr.setCity("Sunnyvale"); addr.setStreet("123 Albert Ave"); addr.setStreet("123 Albert Ave"); addr.setZip(94086); addr.setZip(94086); joe.setAddress(addr); joe.setAddress(addr); mary.setAddress(addr); mary.setAddress(addr);

© JBoss, Inc. 2003-2005.

31

Misc
Professional Open Source™

How to instrument system classes ?
– Collection support implemented via dynamic proxies

jboss-aop.xml has to include the classes to be added to TreeCacheAop
– Fall-back to serialization if not

Instrumentation
– Inside JBoss: classloader (load-time) or aopc (offline) – Standalone: system classloader (org.jboss.aop.standalone.SystemClassLoader) or aopc (offline)

Objects already loaded need to be redeployed to be instrumented at runtime (redeployment works only inside JBoss)

© JBoss, Inc. 2003-2005.

32

16

Roadmap
Professional Open Source™

JBoss/Tomcat 5 fine-grained session replication Aspectizing of JBossCache
– JBossCache not only aspectizes POJOs, but also uses aspects for its own implementation – Possible aspects: a • Replication (repl-to-all, buddy-replication) b c d • Locking • Eviction e f g • Persistence (CacheLoaders) h • Security

Buddy Replication Optimistic locking

Call

© JBoss, Inc. 2003-2005.

33

Summary
Professional Open Source™

JBossCache is a tree-structured cache Can be ‘aspectized’ (a.k.a. configured) with
– – – – – replication transactions / locking (isolation levels) eviction policies persistence roll your own (cache hit/miss ratio metrics, auditing)

Can be used for plain data, or POJOs Can be plugged into other appservers Use
– JBoss/TC5 HTTP session replication, HA-JNDI, SFSB Repl, Hibernate 2nd level cache, SSO

Link: http://www.jboss.com/products/jbosscache

© JBoss, Inc. 2003-2005.

34

17

Sign up to vote on this title
UsefulNot useful