Search in sources :

Example 1 with TransactionalQueue

use of com.hazelcast.core.TransactionalQueue in project hazelcast by hazelcast.

the class QueueTestsFrom2X method testQueuePollRollbackSize.

@Test
public void testQueuePollRollbackSize() {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    HazelcastInstance instance = factory.newHazelcastInstance();
    factory.newHazelcastInstance();
    TransactionContext context = instance.newTransactionContext();
    IQueue<Object> queue = instance.getQueue("testQueuePollRollbackSize");
    queue.offer("item1");
    queue.offer("item2");
    assertEquals(2, queue.size());
    context.beginTransaction();
    TransactionalQueue txnQ1 = context.getQueue("testQueuePollRollbackSize");
    assertEquals("item1", txnQ1.poll());
    assertEquals(1, txnQ1.size());
    assertEquals(1, queue.size());
    context.rollbackTransaction();
    assertEquals(2, queue.size());
    assertEquals("item1", queue.poll());
    assertEquals("item2", queue.poll());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionalQueue(com.hazelcast.core.TransactionalQueue) TransactionContext(com.hazelcast.transaction.TransactionContext) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 2 with TransactionalQueue

use of com.hazelcast.core.TransactionalQueue 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 TransactionalQueue

use of com.hazelcast.core.TransactionalQueue in project hazelcast by hazelcast.

the class TransactionalQueueOfferMessageTask method innerCall.

@Override
protected Object innerCall() throws Exception {
    final TransactionContext context = endpoint.getTransactionContext(parameters.txnId);
    final TransactionalQueue queue = context.getQueue(parameters.name);
    return queue.offer(parameters.item, parameters.timeout, TimeUnit.MILLISECONDS);
}
Also used : TransactionalQueue(com.hazelcast.core.TransactionalQueue) TransactionContext(com.hazelcast.transaction.TransactionContext)

Example 4 with TransactionalQueue

use of com.hazelcast.core.TransactionalQueue in project hazelcast by hazelcast.

the class TransactionalQueuePeekMessageTask method innerCall.

@Override
protected Object innerCall() throws Exception {
    final TransactionContext context = endpoint.getTransactionContext(parameters.txnId);
    final TransactionalQueue queue = context.getQueue(parameters.name);
    Object item = queue.peek(parameters.timeout, TimeUnit.MILLISECONDS);
    return serializationService.toData(item);
}
Also used : TransactionalQueue(com.hazelcast.core.TransactionalQueue) TransactionContext(com.hazelcast.transaction.TransactionContext)

Example 5 with TransactionalQueue

use of com.hazelcast.core.TransactionalQueue in project hazelcast by hazelcast.

the class ClientTxnTest method testTxnRollbackOnServerCrash.

@Test
public void testTxnRollbackOnServerCrash() throws Exception {
    final String queueName = randomString();
    final TransactionContext context = client.newTransactionContext();
    CountDownLatch txnRollbackLatch = new CountDownLatch(1);
    final CountDownLatch memberRemovedLatch = new CountDownLatch(1);
    context.beginTransaction();
    final TransactionalQueue queue = context.getQueue(queueName);
    queue.offer(randomString());
    client.getCluster().addMembershipListener(new MembershipAdapter() {

        @Override
        public void memberRemoved(MembershipEvent membershipEvent) {
            memberRemovedLatch.countDown();
        }
    });
    server.getLifecycleService().terminate();
    try {
        context.commitTransaction();
        fail("commit should throw exception !");
    } catch (Exception e) {
        context.rollbackTransaction();
        txnRollbackLatch.countDown();
    }
    assertOpenEventually(txnRollbackLatch);
    assertOpenEventually(memberRemovedLatch);
    final IQueue<Object> q = client.getQueue(queueName);
    assertNull(q.poll());
    assertEquals(0, q.size());
}
Also used : TransactionalQueue(com.hazelcast.core.TransactionalQueue) TransactionContext(com.hazelcast.transaction.TransactionContext) MembershipEvent(com.hazelcast.core.MembershipEvent) MembershipAdapter(com.hazelcast.core.MembershipAdapter) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

TransactionalQueue (com.hazelcast.core.TransactionalQueue)13 TransactionContext (com.hazelcast.transaction.TransactionContext)13 ParallelTest (com.hazelcast.test.annotation.ParallelTest)8 QuickTest (com.hazelcast.test.annotation.QuickTest)8 Test (org.junit.Test)8 IQueue (com.hazelcast.core.IQueue)4 HazelcastTestSupport.randomString (com.hazelcast.test.HazelcastTestSupport.randomString)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 HazelcastInstance (com.hazelcast.core.HazelcastInstance)2 MembershipAdapter (com.hazelcast.core.MembershipAdapter)2 MembershipEvent (com.hazelcast.core.MembershipEvent)2 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)1 TransactionException (com.hazelcast.transaction.TransactionException)1 TransactionalTaskContext (com.hazelcast.transaction.TransactionalTaskContext)1