use of com.hazelcast.transaction.TransactionalTaskContext in project hazelcast by hazelcast.
the class SetAbstractTest method testNameBasedAffinity.
@Test
public void testNameBasedAffinity() {
//creates more instances to increase a chance 'foo' will not be owned by
//the same member as 'foo@1'
newInstances(config);
newInstances(config);
int numberOfSets = 100;
ISet[] localSets = new ISet[numberOfSets];
ISet[] targetSets = new ISet[numberOfSets];
for (int i = 0; i < numberOfSets; i++) {
String name = randomName() + "@" + i;
localSets[i] = local.getSet(name);
targetSets[i] = target.getSet(name);
}
for (final ISet set : localSets) {
TransactionalTask task = new TransactionalTask() {
@Override
public Object execute(TransactionalTaskContext context) throws TransactionException {
TransactionalSet<String> txSet = context.getSet(set.getName());
txSet.add("Hello");
return null;
}
};
local.executeTransaction(task);
}
for (ISet set : localSets) {
assertEquals(1, set.size());
}
}
use of com.hazelcast.transaction.TransactionalTaskContext in project hazelcast by hazelcast.
the class TransactionQueueTest method issue_6259_backupNotRollingBackCorrectly.
// https://github.com/hazelcast/hazelcast/issues/6259
@Test
public void issue_6259_backupNotRollingBackCorrectly() {
HazelcastInstance[] cluster = createHazelcastInstanceFactory(2).newInstances();
HazelcastInstance local = cluster[0];
HazelcastInstance remote = cluster[1];
final String queueName = generateKeyOwnedBy(remote);
// first we add an item
local.executeTransaction(new TransactionalTask<Object>() {
@Override
public Object execute(TransactionalTaskContext context) throws TransactionException {
TransactionalQueue<String> queue = context.getQueue(queueName);
queue.offer("item");
return null;
}
});
// we remove the item and then do a rollback. This causes the local (backup) to become out
// of sync with the remote (primary)
TransactionContext firstCtxt = local.newTransactionContext();
firstCtxt.beginTransaction();
TransactionalQueue<String> queue = firstCtxt.getQueue(queueName);
queue.poll();
firstCtxt.rollbackTransaction();
// we kill the remote. Now the local (which was the backup) is going to become primary
remote.shutdown();
// if we take the item, we should get an error
TransactionContext secondCtxt = local.newTransactionContext();
secondCtxt.beginTransaction();
queue = secondCtxt.getQueue(queueName);
String found = queue.poll();
assertEquals("item", found);
}
use of com.hazelcast.transaction.TransactionalTaskContext in project hazelcast by hazelcast.
the class TxnMultiMapTest method testTxnCommit.
@Test
public void testTxnCommit() throws TransactionException {
final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
final HazelcastInstance h1 = factory.newHazelcastInstance();
final HazelcastInstance h2 = factory.newHazelcastInstance();
final String map1 = "map1";
final String map2 = "map2";
final String key = "1";
boolean b = h1.executeTransaction(new TransactionalTask<Boolean>() {
public Boolean execute(TransactionalTaskContext context) throws TransactionException {
final TransactionalMultiMap<Object, Object> txMap1 = context.getMultiMap(map1);
final TransactionalMultiMap<Object, Object> txMap2 = context.getMultiMap(map2);
assertTrue(txMap1.put(key, "value1"));
Object value1 = getSingleValue(txMap1, key);
assertEquals("value1", value1);
assertTrue(txMap2.put(key, "value2"));
Object value2 = getSingleValue(txMap2, key);
assertEquals("value2", value2);
return true;
}
});
assertTrue(b);
assertEquals("value1", getSingleValue(h1.getMultiMap(map1), key));
assertEquals("value1", getSingleValue(h2.getMultiMap(map1), key));
assertEquals("value2", getSingleValue(h1.getMultiMap(map2), key));
assertEquals("value2", getSingleValue(h2.getMultiMap(map2), key));
}
use of com.hazelcast.transaction.TransactionalTaskContext in project hazelcast by hazelcast.
the class MapTransactionTest method testSet.
// =================== set ===============================
@Test
public void testSet() throws TransactionException {
Config config = getConfig();
final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
final HazelcastInstance h1 = factory.newHazelcastInstance(config);
final HazelcastInstance h2 = factory.newHazelcastInstance(config);
final IMap map1 = h1.getMap("default");
final IMap map2 = h2.getMap("default");
boolean b = h1.executeTransaction(options, new TransactionalTask<Boolean>() {
public Boolean execute(TransactionalTaskContext context) throws TransactionException {
final TransactionalMap<Object, Object> txMap = context.getMap("default");
txMap.set("1", "value");
txMap.set("1", "value2");
assertEquals("value2", txMap.get("1"));
assertNull(map1.get("1"));
assertNull(map1.get("2"));
assertEquals(1, txMap.size());
return true;
}
});
assertTrue(b);
assertEquals("value2", map1.get("1"));
assertEquals("value2", map2.get("1"));
}
use of com.hazelcast.transaction.TransactionalTaskContext in project hazelcast by hazelcast.
the class MapTransactionTest method testCommitOrder.
@Test
public void testCommitOrder() throws TransactionException {
Config config = getConfig();
final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(4);
final HazelcastInstance h1 = factory.newHazelcastInstance(config);
final HazelcastInstance h2 = factory.newHazelcastInstance(config);
final HazelcastInstance h3 = factory.newHazelcastInstance(config);
final HazelcastInstance h4 = factory.newHazelcastInstance(config);
boolean b = h1.executeTransaction(options, new TransactionalTask<Boolean>() {
public Boolean execute(TransactionalTaskContext context) throws TransactionException {
final TransactionalMap<Object, Object> txMap = context.getMap("default");
txMap.put("1", "value1");
assertEquals("value1", txMap.put("1", "value2"));
assertEquals("value2", txMap.put("1", "value3"));
assertEquals("value3", txMap.put("1", "value4"));
assertEquals("value4", txMap.put("1", "value5"));
assertEquals("value5", txMap.put("1", "value6"));
assertEquals(1, txMap.size());
return true;
}
});
assertEquals("value6", h4.getMap("default").get("1"));
}
Aggregations