Search in sources :

Example 1 with TransactionalTaskContext

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());
    }
}
Also used : TransactionalTask(com.hazelcast.transaction.TransactionalTask) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) ISet(com.hazelcast.core.ISet) Test(org.junit.Test)

Example 2 with TransactionalTaskContext

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);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) TransactionalQueue(com.hazelcast.core.TransactionalQueue) TransactionContext(com.hazelcast.transaction.TransactionContext) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 3 with TransactionalTaskContext

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));
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) TransactionalMultiMap(com.hazelcast.core.TransactionalMultiMap) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 4 with TransactionalTaskContext

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"));
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) IMap(com.hazelcast.core.IMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 5 with TransactionalTaskContext

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"));
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

TransactionalTaskContext (com.hazelcast.transaction.TransactionalTaskContext)63 Test (org.junit.Test)63 TransactionException (com.hazelcast.transaction.TransactionException)62 HazelcastInstance (com.hazelcast.core.HazelcastInstance)61 TransactionalMap (com.hazelcast.core.TransactionalMap)59 ParallelTest (com.hazelcast.test.annotation.ParallelTest)58 QuickTest (com.hazelcast.test.annotation.QuickTest)58 NightlyTest (com.hazelcast.test.annotation.NightlyTest)47 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)38 Config (com.hazelcast.config.Config)36 MapStoreConfig (com.hazelcast.config.MapStoreConfig)32 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)32 IMap (com.hazelcast.core.IMap)18 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)7 ExpectedRuntimeException (com.hazelcast.test.ExpectedRuntimeException)7 Mockito.anyObject (org.mockito.Mockito.anyObject)7 TransactionNotActiveException (com.hazelcast.transaction.TransactionNotActiveException)6 ExecutionException (java.util.concurrent.ExecutionException)6 MapStoreAdapter (com.hazelcast.core.MapStoreAdapter)5 Employee (com.hazelcast.query.SampleObjects.Employee)5