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();
}
}
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();
}
}
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);
}
}
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());
}
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();
}
Aggregations