use of com.hazelcast.transaction.TransactionOptions in project hazelcast by hazelcast.
the class AdvancedClusterStateTest method changeClusterState_shouldNotFail_whenInitiatorDies_afterPrepare.
@Test
public void changeClusterState_shouldNotFail_whenInitiatorDies_afterPrepare() throws Exception {
final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(3);
final HazelcastInstance[] instances = factory.newInstances();
final HazelcastInstance hz = instances[instances.length - 1];
TransactionManagerServiceImpl transactionManagerService = spyTransactionManagerService(hz);
TransactionOptions options = TransactionOptions.getDefault().setDurability(1);
when(transactionManagerService.newAllowedDuringPassiveStateTransaction(options)).thenAnswer(new TransactionAnswer() {
@Override
protected void afterPrepare() {
terminateInstance(hz);
}
});
try {
hz.getCluster().changeClusterState(ClusterState.FROZEN, options);
fail("This instance is terminated. Cannot commit the transaction!");
} catch (HazelcastInstanceNotActiveException ignored) {
}
assertTrueEventually(new AssertTask() {
@Override
public void run() throws Exception {
assertClusterState(ClusterState.FROZEN, instances[0], instances[1]);
}
});
}
use of com.hazelcast.transaction.TransactionOptions in project camel by apache.
the class HazelcastAggregationRepository method remove.
/**
* This method performs transactional operation on removing the {@code exchange}
* from the operational storage and moving it into the persistent one if the {@link HazelcastAggregationRepository}
* runs in recoverable mode and {@code optimistic} is false. It will act at <u>your own</u> risk otherwise.
* @param camelContext the current CamelContext
* @param key the correlation key
* @param exchange the exchange to remove
*/
@Override
public void remove(CamelContext camelContext, String key, Exchange exchange) {
DefaultExchangeHolder holder = DefaultExchangeHolder.marshal(exchange, true, allowSerializedHeaders);
if (optimistic) {
LOG.trace("Removing an exchange with ID {} for key {} in an optimistic manner.", exchange.getExchangeId(), key);
if (!cache.remove(key, holder)) {
LOG.error("Optimistic locking failed for exchange with key {}: IMap#remove removed no Exchanges, while it's expected to remove one.", key);
throw new OptimisticLockingException();
}
LOG.trace("Removed an exchange with ID {} for key {} in an optimistic manner.", exchange.getExchangeId(), key);
if (useRecovery) {
LOG.trace("Putting an exchange with ID {} for key {} into a recoverable storage in an optimistic manner.", exchange.getExchangeId(), key);
persistedCache.put(exchange.getExchangeId(), holder);
LOG.trace("Put an exchange with ID {} for key {} into a recoverable storage in an optimistic manner.", exchange.getExchangeId(), key);
}
} else {
if (useRecovery) {
LOG.trace("Removing an exchange with ID {} for key {} in a thread-safe manner.", exchange.getExchangeId(), key);
// The only considerable case for transaction usage is fault tolerance:
// the transaction will be rolled back automatically (default timeout is 2 minutes)
// if no commit occurs during the timeout. So we are still consistent whether local node crashes.
TransactionOptions tOpts = new TransactionOptions();
tOpts.setTransactionType(TransactionOptions.TransactionType.ONE_PHASE);
TransactionContext tCtx = hzInstance.newTransactionContext(tOpts);
try {
tCtx.beginTransaction();
TransactionalMap<String, DefaultExchangeHolder> tCache = tCtx.getMap(cache.getName());
TransactionalMap<String, DefaultExchangeHolder> tPersistentCache = tCtx.getMap(persistedCache.getName());
DefaultExchangeHolder removedHolder = tCache.remove(key);
LOG.trace("Putting an exchange with ID {} for key {} into a recoverable storage in a thread-safe manner.", exchange.getExchangeId(), key);
tPersistentCache.put(exchange.getExchangeId(), removedHolder);
tCtx.commitTransaction();
LOG.trace("Removed an exchange with ID {} for key {} in a thread-safe manner.", exchange.getExchangeId(), key);
LOG.trace("Put an exchange with ID {} for key {} into a recoverable storage in a thread-safe manner.", exchange.getExchangeId(), key);
} catch (Throwable throwable) {
tCtx.rollbackTransaction();
final String msg = String.format("Transaction with ID %s was rolled back for remove operation with a key %s and an Exchange ID %s.", tCtx.getTxnId(), key, exchange.getExchangeId());
LOG.warn(msg, throwable);
throw new RuntimeException(msg, throwable);
}
} else {
cache.remove(key);
}
}
}
Aggregations