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 MapTransactionLockingTest method testTxnReplace_whenReplaceIfSameFails_keyShouldRemainUnlockedDuringTransaction.
@Test
public void testTxnReplace_whenReplaceIfSameFails_keyShouldRemainUnlockedDuringTransaction() throws InterruptedException {
final HazelcastInstance hazelcastInstance = createHazelcastInstance(getConfig());
final IMap<String, Object> map = hazelcastInstance.getMap(mapName);
hazelcastInstance.executeTransaction(new TransactionalTask<Object>() {
@Override
public Object execute(TransactionalTaskContext transactionContext) throws TransactionException {
TransactionalMap<String, Object> transactionalMap = transactionContext.getMap(mapName);
boolean replace = transactionalMap.replace(key, value + "other", value);
assertFalse(replace);
assertFalse("Key remains locked!", map.isLocked(key));
return null;
}
});
}
use of com.hazelcast.transaction.TransactionalTaskContext in project hazelcast by hazelcast.
the class MapTransactionRegressionTest method test_Issue615_ValuesWithPredicate.
@Test
public void test_Issue615_ValuesWithPredicate() throws TransactionException {
Config config = getConfig();
final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
final HazelcastInstance h1 = factory.newHazelcastInstance(config);
final HazelcastInstance h2 = factory.newHazelcastInstance(config);
final IMap map2 = h2.getMap("default");
final SampleObjects.Employee emp1 = new SampleObjects.Employee("abc-123-xvz", 34, true, 10D);
map2.put(1, emp1);
final SampleObjects.Employee emp2 = new SampleObjects.Employee("xvz", 4, true, 10D);
boolean b = h1.executeTransaction(options, new TransactionalTask<Boolean>() {
public Boolean execute(TransactionalTaskContext context) throws TransactionException {
final TransactionalMap<Object, Object> txMap = context.getMap("default");
assertEquals(0, txMap.values(new SqlPredicate("age <= 10")).size());
txMap.put(2, emp2);
Collection coll = txMap.values(new SqlPredicate("age <= 10"));
Iterator<Object> iterator = coll.iterator();
while (iterator.hasNext()) {
final SampleObjects.Employee e = (SampleObjects.Employee) iterator.next();
assertEquals(emp2, e);
}
coll = txMap.values(new SqlPredicate("age > 30 "));
iterator = coll.iterator();
while (iterator.hasNext()) {
final SampleObjects.Employee e = (SampleObjects.Employee) iterator.next();
assertEquals(emp1, e);
}
txMap.remove(2);
coll = txMap.values(new SqlPredicate("age <= 10 "));
assertEquals(0, coll.size());
return true;
}
});
assertEquals(0, map2.values(new SqlPredicate("age <= 10")).size());
assertEquals(1, map2.values(new SqlPredicate("age = 34")).size());
h1.shutdown();
h2.shutdown();
}
use of com.hazelcast.transaction.TransactionalTaskContext in project hazelcast by hazelcast.
the class MapTransactionRegressionTest method test_Issue615_values.
@Test
public void test_Issue615_values() throws TransactionException {
Config config = getConfig();
final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
final HazelcastInstance h1 = factory.newHazelcastInstance(config);
final HazelcastInstance h2 = factory.newHazelcastInstance(config);
final IMap map2 = h2.getMap("default");
map2.put("1", "1");
map2.put("2", "2");
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("3", "3");
assertEquals(3, txMap.values().size());
map2.put("4", "4");
assertEquals(4, txMap.values().size());
txMap.remove("1");
assertEquals(3, txMap.values().size());
map2.remove("2");
assertEquals(2, txMap.values().size());
assertEquals(2, txMap.size());
txMap.put("12", "32");
assertEquals(2, map2.values().size());
return true;
}
});
assertEquals(3, map2.values().size());
h1.shutdown();
h2.shutdown();
}
use of com.hazelcast.transaction.TransactionalTaskContext in project hazelcast by hazelcast.
the class MapTransactionRegressionTest method test_Issue615_keySet.
@Test
public void test_Issue615_keySet() throws TransactionException {
Config config = getConfig();
final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
final HazelcastInstance h1 = factory.newHazelcastInstance(config);
final HazelcastInstance h2 = factory.newHazelcastInstance(config);
final IMap map = h2.getMap("default");
map.put("1", "1");
map.put("2", "2");
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("3", "3");
assertEquals(3, txMap.keySet().size());
map.put("4", "4");
assertEquals(4, txMap.keySet().size());
txMap.remove("1");
assertEquals(3, txMap.keySet().size());
map.remove("2");
assertEquals(2, txMap.keySet().size());
assertEquals(2, txMap.size());
return true;
}
});
assertEquals(2, map.keySet().size());
// raise an exception and rollback changes.
try {
boolean b2 = h1.executeTransaction(options, new TransactionalTask<Boolean>() {
public Boolean execute(TransactionalTaskContext context) throws TransactionException {
final TransactionalMap<Object, Object> txMap = context.getMap("default");
txMap.put("5", "5");
assertEquals(3, txMap.keySet().size());
assertEquals(2, map.keySet().size());
throw new DummyUncheckedHazelcastTestException();
}
});
} catch (Exception e) {
if (!(e instanceof DummyUncheckedHazelcastTestException)) {
throw new RuntimeException(e);
}
}
assertEquals(2, map.keySet().size());
h1.shutdown();
h2.shutdown();
}
Aggregations