Академический Документы
Профессиональный Документы
Культура Документы
JBossCache
In-Memory Replicated Transactional Cache
Topics
Professional Open Source™
– What is JBossCache ?
– Architecture
– How do I use it ? – API
– Eviction Policies and CacheLoading
– JBossCache/AOP and POJOs
1
What is JBossCache ?
Professional Open Source™
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
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
Cache loader
– Persistent backend store (load - store)
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)
b c d
– Multiple roots per cache allowed
– Each node is a map with keys and values
e
f g
/a/d/g
Architecture - Structure
Professional Open Source™
3
Replication
Professional Open Source™
Configuration
Professional Open Source™
replyAsync-service.xml
<server>
<server>
<mbean
<mbeancode="org.jboss.cache.TreeCache"
code="org.jboss.cache.TreeCache"name="jboss.cache:service=TreeCache">
name="jboss.cache:service=TreeCache">
......
<attribute
<attributename="IsolationLevel">REPEATABLE_READ</attribute>
name="IsolationLevel">REPEATABLE_READ</attribute>
<!--
<!--Valid
Validmodes
modesare
areLOCAL,
LOCAL,REPL_ASYNC
REPL_ASYNCandandREPL_SYNC
REPL_SYNC-->
-->
<attribute
<attributename="CacheMode">REPL_ASYNC</attribute>
name="CacheMode">REPL_ASYNC</attribute>
<!--
<!--Just
Justused
usedfor
forasync
asyncrepl:
repl:use
useaareplication
replicationqueue
queue-->
-->
<attribute
<attributename="UseReplQueue">false</attribute>
name="UseReplQueue">false</attribute>
<!--
<!--Replication
Replicationinterval
intervalfor
forreplication
replicationqueue
queue(in
(inms)
ms)-->
-->
<attribute
<attributename="ReplQueueInterval">0</attribute>
name="ReplQueueInterval">0</attribute>
<!--
<!--Max
Maxnumber
numberof ofelements
elementswhich
whichtrigger
triggerreplication
replication-->
-->
<attribute
<attributename="ReplQueueMaxElements">0</attribute>
name="ReplQueueMaxElements">0</attribute>
......
4
Configuration
Professional Open Source™
replyAsync-service.xml
…
…
<!--
<!--Whether
Whetheror ornot
nottotofetch
fetchstate
stateon
onjoining
joiningaacluster
cluster-->
-->
<attribute
<attributename="FetchStateOnStartup">true</attribute>
name="FetchStateOnStartup">true</attribute>
<!--
<!--The
Themax
maxamount
amountof oftime
time(in
(inmilliseconds)
milliseconds)we wewait
waituntil
untilthe
theinitial
initialstate
state(ie.
(ie.the
thecontents
contentsofofthe
the
cache)
cache)are
areretrieved
retrievedfrom
fromexisting
existingmembers
membersin inaaclustered
clusteredenvironment
environment--> -->
<attribute
<attributename="InitialStateRetrievalTimeout">5000</attribute>
name="InitialStateRetrievalTimeout">5000</attribute>
<!--
<!--Number
Numberof ofmilliseconds
millisecondsto towait
waituntil
untilall
allresponses
responsesfor foraasynchronous
synchronouscall callhave
havebeen
beenreceived.
received.-->
-->
<attribute name="SyncReplTimeout">10000</attribute>
<attribute name="SyncReplTimeout">10000</attribute>
<!--
<!--Max
Maxnumber
numberof ofmilliseconds
millisecondsto towait
waitfor
foraalock
lockacquisition
acquisition-->-->
<attribute
<attributename="LockAcquisitionTimeout">15000</attribute>
name="LockAcquisitionTimeout">15000</attribute>
<!--
<!--Name
Nameof ofthe
theeviction
evictionpolicy
policyclass.
class.Not
Notsupported
supportednow.now.-->
-->
<attribute
<attributename="EvictionPolicyClass"></attribute>
name="EvictionPolicyClass"></attribute></mbean> </mbean>
</server>
</server>
API
Professional Open Source™
5
API
Professional Open Source™
remove(FQN name)
– Removes and its children from the tree
Sample Code
Professional Open Source™
try
try{{
tx=(UserTransaction)new
tx=(UserTransaction)new
InitialContext(p).lookup("UserTransaction");
InitialContext(p).lookup("UserTransaction");
TreeCache
TreeCachec=newc=newTreeCache("test",
TreeCache("test",null,
null,10000);
10000);
c.setMode(TreeCache.REPL_ASYNC);
c.setMode(TreeCache.REPL_ASYNC);
c.start();
c.start();
tx.begin();
tx.begin();
c.put("/a/b/c",
c.put("/a/b/c","age",
"age",new
newInteger(38));
Integer(38));
c.put("/a/b/c",
c.put("/a/b/c","age",
"age",new
newInteger(39));
Integer(39));
tx.commit();
tx.commit();
assertEquals(new
assertEquals(newInteger(39),
Integer(39),c.get("/a/b/c",
c.get("/a/b/c","age"));
"age"));
}}
catch(Throwable
catch(Throwablet)t){{
if(tx
if(tx!=
!=null)
null)tx.rollback();
tx.rollback();
fail(t.toString());
fail(t.toString());
}}
6
Sample Step-by-Step
Professional Open Source™
a 1 tx.begin()
2 put(”a/b/c”, ”age”, 38)
3 create
b
Sample Step-by-Step
Professional Open Source™
a 1 tx.begin()
2 put(”a/b/c”, ”age”, 38)
3 create
b 5 put(”a/b/c”, ”age”, 39)
6 tx.commit()
Repl. Msg
3 create (X) Lock Repl.
C Queue
age 39 38 8 UseReplQueue?
4 7 ASYNC
9 Thread continue....
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
A Acquire write-lock A
on Node B ? Commit
TxID = 1 TxID = 2
SYNC Replication
B B
Local Local
Commit Acquire write-lock
(X) Lock (X) Lock
on Node B ?
Timeout
CacheLoader
Professional Open Source™
8
CacheLoader
Professional Open Source™
<!--
<!--====================================================================
====================================================================--> -->
<!--
<!--Defines
DefinesTreeCache
TreeCacheconfiguration
configuration-->
-->
<!--
<!--====================================================================
====================================================================--> -->
<mbean
<mbeancode="org.jboss.cache.TreeCache"
code="org.jboss.cache.TreeCache"name="jboss.cache:service=TreeCache">
name="jboss.cache:service=TreeCache">
<attribute
<attributename="CacheLoaderClass">org.jboss.cache.loader.bdbje.BdbjeCacheLoader</attribute>
name="CacheLoaderClass">org.jboss.cache.loader.bdbje.BdbjeCacheLoader</attribute>
<!--
<!--attribute
attributename="CacheLoaderClass">org.jboss.cache.loader.FileCacheLoader</attribute
name="CacheLoaderClass">org.jboss.cache.loader.FileCacheLoader</attribute-->
-->
<attribute
<attributename="CacheLoaderConfig">c:\tmp\bdbje</attribute>
name="CacheLoaderConfig">c:\tmp\bdbje</attribute>
<attribute
<attributename="CacheLoaderShared">true</attribute>
name="CacheLoaderShared">true</attribute>
<attribute
<attributename="CacheLoaderPreload">/</attribute>
name="CacheLoaderPreload">/</attribute>
<attribute
<attributename="CacheLoaderFetchTransientState">false</attribute>
name="CacheLoaderFetchTransientState">false</attribute>
<attribute
<attributename="CacheLoaderFetchPersistentState">true</attribute>
name="CacheLoaderFetchPersistentState">true</attribute>
</mbean>
</mbean>
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
9
Eviction Policy
Professional Open Source™
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)
10
Eviction Policy XML Configuration
Professional Open Source™
<!--
<!--Name
Nameofofthetheeviction
evictionpolicy
policyclass.
class.-->
-->
<attribute
<attributename="EvictionPolicyClass">org.jboss.cache.eviction.LRUPolicy</attribute>
name="EvictionPolicyClass">org.jboss.cache.eviction.LRUPolicy</attribute>
<!--
<!-- Specific eviction policy configurations.This
Specific eviction policy configurations. ThisisisLRU
LRU-->
-->
<attribute
<attributename="EvictionPolicyConfig">
name="EvictionPolicyConfig">
<config>
<config>
<attribute
<attributename="wakeUpIntervalSeconds">5</attribute>
name="wakeUpIntervalSeconds">5</attribute>
<!--
<!--Cache
Cachewide
widedefault
default-->
-->
<region
<regionname="/_default_">
name="/_default_">
<attribute
<attributename="maxNodes">5000</attribute>
name="maxNodes">5000</attribute>
<attribute
<attributename="timeToIdleSeconds">1000</attribute>
name="timeToIdleSeconds">1000</attribute>
</region>
</region>
<!–
<!– User-definedregion
User-defined region-->
-->
<region
<regionname="/org/jboss/data">
name="/org/jboss/data">
<attribute name="maxNodes">5000</attribute>
<attribute name="maxNodes">5000</attribute>
<attribute
<attributename="timeToIdleSeconds">1000</attribute>
name="timeToIdleSeconds">1000</attribute>
</region>
</region>
</config>
</config>
</attribute>
</attribute>
</attribute>
</attribute>
Process A Process B
a a
b c d b c d
e f g e f g
h h
read/write read
Backend storage
11
Replication (unshared datastores)
Professional Open Source™
Process A Process B
a a
b c d b c d
e f g e f g
h h
Backend storage
TreeCacheAOP
Object Oriented Caching
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
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
13
TreeCacheAOP - Mapping
Professional Open Source™
Example of a jboss-aop.xml
Professional Open Source™
<?xml
<?xmlversion="1.0"
version="1.0"encoding="UTF-8"?>
encoding="UTF-8"?>
<aop>
<aop>
<prepare
<prepareexpr="field(*
expr="field(*$instanceof{org.jboss.test.cache.test.standAloneAop.Student}->*)"
$instanceof{org.jboss.test.cache.test.standAloneAop.Student}->*)"/>/>
<prepare
<prepareexpr="field(*
expr="field(*$instanceof{org.jboss.test.cache.test.standAloneAop.Address}->*)"
$instanceof{org.jboss.test.cache.test.standAloneAop.Address}->*)"/>/>
<prepare
<prepareexpr="field(*
expr="field(*$instanceof{org.jboss.test.cache.test.standAloneAop.Person}->*)"
$instanceof{org.jboss.test.cache.test.standAloneAop.Person}->*)"/>/>
</aop>
</aop>
14
TreeCacheAOP - Interceptors
Professional Open Source™
TreeCacheAOP - Mapping
Professional Open Source™
Person p (key=/persons/322649)
persons
name: „Bela“
322649
hobbies
0 1 2
15
TreeCacheAOP - Mapping
Professional Open Source™
1 2 Address Address
Addressaddr
addr==new
newAddress();
Address();
addr.setCity("Sunnyvale");
addr.setCity("Sunnyvale");
name Joe name Mary city Sunnyvale addr.setStreet("123
addr.setStreet("123 AlbertAve");
Albert Ave");
age 31 age 30 street 123 Al... addr.setZip(94086);
addr.setZip(94086);
zip 94086
joe.setAddress(addr);
joe.setAddress(addr);
mary.setAddress(addr);
mary.setAddress(addr);
Misc
Professional Open Source™
16
Roadmap
Professional Open Source™
• Eviction e f g
• Persistence (CacheLoaders)
h
• Security
Buddy Replication
Optimistic locking Call
Summary
Professional Open Source™
17