Вы находитесь на странице: 1из 17

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)
b c d
– Multiple roots per cache allowed
– Each node is a map with keys and values

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
<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>
......

© JBoss, Inc. 2003-2005. 8

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>

© 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
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());
}}

© JBoss, Inc. 2003-2005. 12

6
Sample Step-by-Step
Professional Open Source™

a 1 tx.begin()
2 put(”a/b/c”, ”age”, 38)
3 create
b

3 create (X) Lock Repl.


C Queue
age 38
4

© JBoss, Inc. 2003-2005. 13

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....

© 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 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

© 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
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>

© 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
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>

© JBoss, Inc. 2003-2005. 21

Replication (shared datastore)


Professional Open Source™

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

© JBoss, Inc. 2003-2005. 22

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

© 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
<?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>

© 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 name Bela

322649
hobbies

city San Jose


city: „ San Jose“
zip 95124
addr
hobbies
zip: 95124

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
Personjoe
joe==new
newPerson();
Person();
joe.setName("Joe
joe.setName("JoeBlack");
Black");
joe.setAge(31);
joe.setAge(31);
Persons Internal
Person
Personmary
mary==new
newPerson();
Person();
mary.setName("Mary
mary.setName("MaryWhite");
White");
mary.setAge(30);
mary.setAge(30);

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);

© 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)
• Locking b c d

• 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

Вам также может понравиться