Upload
others
View
6
Download
0
Embed Size (px)
Citation preview
PTx Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
Partition-Level Transactions
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. PTX - 2
Objectives
After completing this lesson, you should be able to: • Enumerate atomic partition-level transaction concepts • Describe Cache and EntryProcessor features • Implement Coherence 3.7 Atomic Operations support
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. PTX - 3
Partition-Level Transactions
Benefits: • Development of entry processors that process multiple
entries atomically across different caches (in the same partition)
• Efficient entry back-ups – All changes communicated in a single message “for free”
• Safe and efficient re-entrance support – Access and mutate other entries and caches from an entry
processor, without: — Using heavy transactions — Worrying about thread safety
– Identify and prevent Dead Locking
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. PTX - 4
Atomic Partition-Level Transactions
Partition-level transactions are: • Written in the context of an entry processor • Either all complete or none complete • Used to mutate or change multiple entries in a single
operation • Used to access multiple caches within a single operation
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. PTX - 5
1
Partitioned Cache: Review
Each member is responsible for a unique set of primary and backup partitions.
Data is distributed across storage-enabled members by partitions.
A specific key is bound to a specific partition for the life of the cache.
2
3
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. PTX - 6
Entry Processor: Review
Entry processors are implemented via the InvokableMap API.
Entry processors are managed similar to data.
Entry processors are invoked against a key or set of keys.
When an entry processor is delivered to the member where a key resides, it is executed.
Any changes are committed to the backing map, and then backed up.
1
2
3
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. PTX - 7
Concurrent Processing and Entry Processors
• Entry processor methods are executed concurrently. • The server isolates the operation from all other operations
on the same key.
public Object process(Entry entry) {
// get the value of the entry Integer iValue = (Integer)entry.getValue();
// Update the value iValue++;
// Update the value, holding the lock on this entry entry.setValue(iValue);
return iValue; }
Entry processors implicitly hold a lock on the key for which they are executing.
As a result, any code executing within the entry processors process method is atomic.
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. PTX - 8
Partition-Level Operations Pre 3.7: Example
public Object process(Entry entry) {
// modify the checking value BinaryEntry checking = (BinaryEntry)entry; Integer newAmount = (Integer) checking.getValue – m_iAmount; checkingEntry.setValue(newAmount);
// Possible race condition // All data in interval format Map savingsMap = checking.getContext().getBackingMap("savings"); BinaryEntry savings = (BinaryEntry)savingsMap.get(m_iSavingsAccountId); newAmount = (Integer)savings.getValue() + m_iAmount; savingsMap.put(m_iSavingsAccountId, savings.getContext(). getValueToInternalConverter().convert(newAmount));
return true }
All these operations are suspect!
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. PTX - 9
Atomic-Level Operation Support
• Coherence added support for accessing multiple keys atomically in 3.7.
• BackingMapContext.getBackingMapEntry(Object key)can be used to access non-Entry data.
• Considerations and requirements are as follows: – Keys must be located in the same partition. Continue to use
KeyAssociation. – All operations are atomic in the context of a single
EntryProcessor. – All entries that access via this method are locked. – Transaction-read consistency is maintained. – Deadlock detection is now supported.
Note: Always use BackingMapContext to access other data.
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. PTX - 10
Partition-Level Operations 3.7 and Later: Example
public Object process(Entry entry) {
// modify the checking value BinaryEntry checking = (BinaryEntry)entry; Integer newAmount = (Integer) checking.getValue – m_iAmount; checkingEntry.setValue(newAmount);
// access is safe, and no longer requires format translation BinaryEntry savings = ((BinaryEntry)checking).getContext() .getBackingMapContext("savings"). getBackingMapEntry(m_iSavingsAccountId); newAmount = ((Integer)savings.getValue()) + m_iAmount; savings.setValue(newAmount);
return true; }
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. PTX - 11
Accessing Other Keys in the Same Cache
Partition-level transactions can also be applied to other keys in the same cache.
... public void process(Entry entry) {
((BinaryEntry)entry).getBackingMap() .put("otherkey", value);
//get an entry in the same partition Entry otherentry = ((BinaryEntry)entry).getBackingMapContext() .getBackingMapEntry("somekey"); }
...
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. PTX - 12
Summary
In this lesson, you should have learned to: • Describe atomic partition-level transactions • Review a partitioned cache and an entry processor • Explain Coherence 3.7 atomic operations support