Cloud Zone is brought to you in partnership with:

Dmitriy Setrakyan manages daily operations of GridGain Systems and brings over 12 years of experience to GridGain Systems which spans all areas of application software development from design and architecture to team management and quality assurance. His experience includes architecture and leadership in development of distributed middleware platforms, financial trading systems, CRM applications, and more. Dmitriy is a DZone MVB and is not an employee of DZone and has posted 54 posts at DZone. You can read more from them at their website. View Full User Profile

Cool GridGain Transaction Example

06.01.2011
| 5359 views |
  • submit to reddit

I never planned to create an example like this, but one of the users in our community forums pushed me to do it. Basically the user was complaining that he gets some deadlock behavior on the grid when he reads and updates the same object from cache on multiple grid nodes in parallel. Naturally I got worried and created the following test, which passes by the way :)

In this example we basically store array of size N in cache and execute N jobs on the grid, each of which gets an array from cache and flips the array cell assigned to it from zero to one. As a result you start out with an array of all zeros and you end up with array of all ones. The cache is configured in PARTITIONED mode with synchronous commits. By default, every key stored in the cache is backed up on another node. Here is how the code looks like:

// Number of grids in the test.
private static final int GRID_COUNT = 4;

public void testTransactions() throws GridException {
// Start multiple grid nodes in the same JVM.
for (int i = 0; i < GRID_COUNT; i++) {
GridConfigurationAdapter cfg = new GridConfigurationAdapter();

// I am specifically omitting other configuration details.
cfg.setGridName("grid-" + i);

G.start(cfg);
}

try {
// Try job count N = 1000
checkTransactions(1000);
}
finally {
G.stopAll(true);
}
}

private void checkTransactions(final int jobCnt) throws GridException {
// Get frist grid node.
Grid grid = G.grid("grid-0");

final GridCache<String, int[]> cache = grid.cache();

Collection<GridFuture<?>> futs = new LinkedList<GridFuture<?>>();

for (int i = 0; i < jobCnt; i++) {
// Run these jobs in parallel on all grid nodes.
GridFuture<?> fut = grid.runAsync(
GridClosureCallMode.BALANCE,
new GridInClosureX<Integer>() {
@Override
public void applyx(Integer i) throws GridException {
GridCacheTx tx =
cache.startTx(PESSIMISTIC, REPEATABLE_READ);

try {
int[] arr = cache.get("TestKey");

if (arr == null)
arr = new int[jobCnt];

arr[i] = 1;

cache.put("TestKey", arr);

tx.commit();
}
finally {
tx.end();
}
}
},
i);

futs.add(fut);
}

for (GridFuture<?> fut : futs)
fut.get(); // Wait for completion.

// Get array from cache.
int[] arr = cache.get("TestKey");

assert arr != null;
assert jobCnt == arr.length;

// Make sure that all array cells got flipped to 1.
for (int i : arr)
assert i == 1;
}

By the way, we have recently released GridGain 3.1.0, so give it a try.

From http://gridgain.blogspot.com/2011/05/cool-gridgain-transaction-example.html

Published at DZone with permission of Dmitriy Setrakyan, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)