Search in sources :

Example 81 with TransactionException

use of com.hazelcast.transaction.TransactionException in project hazelcast by hazelcast.

the class ClusterStateManager method commitClusterState.

public void commitClusterState(ClusterStateChange stateChange, Address initiator, String txnId, boolean isTransient) {
    Preconditions.checkNotNull(stateChange);
    stateChange.validate();
    clusterServiceLock.lock();
    try {
        final LockGuard stateLock = getStateLock();
        if (!stateLock.allowsUnlock(txnId)) {
            throw new TransactionException("Cluster state change [" + state + " -> " + stateChange + "] failed for " + initiator + ", current state: " + stateToString());
        }
        if (stateChange.isOfType(ClusterState.class)) {
            ClusterState newState = (ClusterState) stateChange.getNewState();
            doSetClusterState(newState, isTransient);
            // if state is changed to ACTIVE, then remove all members which left while not active.
            if (newState == ClusterState.ACTIVE) {
                node.getClusterService().removeMembersDeadWhileClusterIsNotActive();
            }
        } else if (stateChange.isOfType(Version.class)) {
            // version is validated on cluster-state-lock, thus we can commit without checking compatibility
            doSetClusterVersion((Version) stateChange.getNewState());
        } else {
            throw new IllegalArgumentException("Illegal ClusterStateChange of type " + stateChange.getType() + ".");
        }
    } finally {
        clusterServiceLock.unlock();
    }
}
Also used : ClusterState(com.hazelcast.cluster.ClusterState) TransactionException(com.hazelcast.transaction.TransactionException) Version(com.hazelcast.version.Version) LockGuard(com.hazelcast.internal.util.LockGuard)

Example 82 with TransactionException

use of com.hazelcast.transaction.TransactionException in project hazelcast by hazelcast.

the class ClusterStateManager method lockClusterState.

/**
     * Validate cluster state change requested and set a {@code ClusterStateLock}.
     * @param stateChange
     * @param initiator
     * @param txnId
     * @param leaseTime
     * @param partitionStateVersion
     */
public void lockClusterState(ClusterStateChange stateChange, Address initiator, String txnId, long leaseTime, int partitionStateVersion) {
    Preconditions.checkNotNull(stateChange);
    clusterServiceLock.lock();
    try {
        if (!node.getNodeExtension().isStartCompleted()) {
            throw new IllegalStateException("Can not lock cluster state! Startup is not completed yet!");
        }
        if (stateChange.isOfType(Version.class)) {
            validateNodeCompatibleWith((Version) stateChange.getNewState());
            validateClusterVersionChange((Version) stateChange.getNewState());
        }
        checkMigrationsAndPartitionStateVersion(stateChange, partitionStateVersion);
        final LockGuard currentLock = getStateLock();
        if (!currentLock.allowsLock(txnId)) {
            throw new TransactionException("Locking failed for " + initiator + ", tx: " + txnId + ", current state: " + toString());
        }
        stateLockRef.set(new LockGuard(initiator, txnId, leaseTime));
        try {
            // check migration status and partition-state version again
            // if partition state is changed then release the lock and fail.
            checkMigrationsAndPartitionStateVersion(stateChange, partitionStateVersion);
        } catch (IllegalStateException e) {
            stateLockRef.set(LockGuard.NOT_LOCKED);
            throw e;
        }
    } finally {
        clusterServiceLock.unlock();
    }
}
Also used : TransactionException(com.hazelcast.transaction.TransactionException) LockGuard(com.hazelcast.internal.util.LockGuard)

Example 83 with TransactionException

use of com.hazelcast.transaction.TransactionException in project hazelcast by hazelcast.

the class TransactionManagerServiceImpl method executeTransaction.

@Override
public <T> T executeTransaction(TransactionOptions options, TransactionalTask<T> task) throws TransactionException {
    checkNotNull(task, "TransactionalTask is required!");
    TransactionContext context = newTransactionContext(options);
    context.beginTransaction();
    try {
        T value = task.execute(context);
        context.commitTransaction();
        return value;
    } catch (Throwable e) {
        context.rollbackTransaction();
        if (e instanceof TransactionException) {
            throw (TransactionException) e;
        }
        if (e.getCause() instanceof TransactionException) {
            throw (TransactionException) e.getCause();
        }
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        }
        throw new TransactionException(e);
    }
}
Also used : TransactionException(com.hazelcast.transaction.TransactionException) TransactionContext(com.hazelcast.transaction.TransactionContext)

Example 84 with TransactionException

use of com.hazelcast.transaction.TransactionException in project hazelcast by hazelcast.

the class ClientTxnMapTest method testGetForUpdate.

@Test
public void testGetForUpdate() throws TransactionException {
    final String mapName = randomString();
    final String key = "key";
    final int initialValue = 111;
    final int value = 888;
    final CountDownLatch getKeyForUpdateLatch = new CountDownLatch(1);
    final CountDownLatch afterTryPutResult = new CountDownLatch(1);
    final IMap<String, Integer> map = client.getMap(mapName);
    map.put(key, initialValue);
    final AtomicBoolean tryPutResult = new AtomicBoolean(true);
    Runnable incrementor = new Runnable() {

        public void run() {
            try {
                getKeyForUpdateLatch.await(30, TimeUnit.SECONDS);
                boolean result = map.tryPut(key, value, 0, TimeUnit.SECONDS);
                tryPutResult.set(result);
                afterTryPutResult.countDown();
            } catch (Exception e) {
            }
        }
    };
    new Thread(incrementor).start();
    client.executeTransaction(new TransactionalTask<Boolean>() {

        public Boolean execute(TransactionalTaskContext context) throws TransactionException {
            try {
                final TransactionalMap<String, Integer> txMap = context.getMap(mapName);
                txMap.getForUpdate(key);
                getKeyForUpdateLatch.countDown();
                afterTryPutResult.await(30, TimeUnit.SECONDS);
            } catch (Exception e) {
            }
            return true;
        }
    });
    assertFalse(tryPutResult.get());
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) HazelcastTestSupport.randomString(com.hazelcast.test.HazelcastTestSupport.randomString) CountDownLatch(java.util.concurrent.CountDownLatch) TransactionException(com.hazelcast.transaction.TransactionException) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TransactionException(com.hazelcast.transaction.TransactionException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 85 with TransactionException

use of com.hazelcast.transaction.TransactionException in project hazelcast by hazelcast.

the class ClientTxnMapTest method testDeadLockFromClientInstance.

@Test
public void testDeadLockFromClientInstance() throws InterruptedException {
    final String mapName = randomString();
    final String key = "key";
    final AtomicBoolean running = new AtomicBoolean(true);
    Thread t = new Thread() {

        public void run() {
            while (running.get()) {
                client.getMap(mapName).get(key);
            }
        }
    };
    t.start();
    CBAuthorisation cb = new CBAuthorisation();
    cb.setAmount(15000);
    try {
        TransactionContext context = client.newTransactionContext();
        context.beginTransaction();
        TransactionalMap mapTransaction = context.getMap(mapName);
        // init data
        mapTransaction.put(key, cb);
        // start test deadlock, 3 set and concurrent, get deadlock
        cb.setAmount(12000);
        mapTransaction.set(key, cb);
        cb.setAmount(10000);
        mapTransaction.set(key, cb);
        cb.setAmount(900);
        mapTransaction.set(key, cb);
        cb.setAmount(800);
        mapTransaction.set(key, cb);
        cb.setAmount(700);
        mapTransaction.set(key, cb);
        context.commitTransaction();
    } catch (TransactionException e) {
        e.printStackTrace();
        fail();
    }
    running.set(false);
    t.join();
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TransactionException(com.hazelcast.transaction.TransactionException) TransactionContext(com.hazelcast.transaction.TransactionContext) HazelcastTestSupport.randomString(com.hazelcast.test.HazelcastTestSupport.randomString) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

TransactionException (com.hazelcast.transaction.TransactionException)91 Test (org.junit.Test)74 HazelcastInstance (com.hazelcast.core.HazelcastInstance)68 QuickTest (com.hazelcast.test.annotation.QuickTest)66 ParallelTest (com.hazelcast.test.annotation.ParallelTest)62 TransactionalTaskContext (com.hazelcast.transaction.TransactionalTaskContext)62 TransactionalMap (com.hazelcast.core.TransactionalMap)60 NightlyTest (com.hazelcast.test.annotation.NightlyTest)51 Config (com.hazelcast.config.Config)39 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)39 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)35 MapStoreConfig (com.hazelcast.config.MapStoreConfig)32 IMap (com.hazelcast.core.IMap)18 TransactionContext (com.hazelcast.transaction.TransactionContext)14 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)7 ExpectedRuntimeException (com.hazelcast.test.ExpectedRuntimeException)7 TransactionOptions (com.hazelcast.transaction.TransactionOptions)7 Mockito.anyObject (org.mockito.Mockito.anyObject)7 TransactionNotActiveException (com.hazelcast.transaction.TransactionNotActiveException)6 MapStoreAdapter (com.hazelcast.core.MapStoreAdapter)5