use of org.apache.ignite.transactions.TransactionConcurrency in project ignite by apache.
the class CacheSerializableTransactionsTest method concurrentUpdateNoDeadlock.
/**
* @param updateNodes Nodes executing updates.
* @param threads Number of threads executing updates.
* @param get If {@code true} gets value in transaction.
* @param restart If {@code true} restarts one node.
* @param nonSer If {@code true} starts threads executing non-serializable transactions.
* @throws Exception If failed.
*/
private void concurrentUpdateNoDeadlock(final List<Ignite> updateNodes, int threads, final boolean get, final boolean restart, final boolean nonSer) throws Exception {
if (FAST)
return;
assert updateNodes.size() > 0;
final Ignite srv = ignite(1);
final String cacheName = srv.createCache(cacheConfiguration(PARTITIONED, FULL_SYNC, 1, false, false)).getName();
try {
final int KEYS = 100;
final AtomicBoolean finished = new AtomicBoolean();
IgniteInternalFuture<?> fut = restart ? restartFuture(finished, null) : null;
try {
for (int i = 0; i < 10; i++) {
log.info("Iteration: " + i);
final long stopTime = U.currentTimeMillis() + 10_000;
final AtomicInteger idx = new AtomicInteger();
IgniteInternalFuture<?> nonSerFut = null;
if (nonSer) {
nonSerFut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
int nodeIdx = idx.getAndIncrement() % updateNodes.size();
Ignite node = updateNodes.get(nodeIdx);
log.info("Non-serializable tx thread: " + node.name());
final IgniteCache<Integer, Integer> cache = node.cache(cacheName);
assertNotNull(cache);
final ThreadLocalRandom rnd = ThreadLocalRandom.current();
while (U.currentTimeMillis() < stopTime) {
final TreeMap<Integer, Integer> map = new TreeMap<>();
for (int i = 0; i < KEYS / 2; i++) map.put(rnd.nextInt(KEYS), rnd.nextInt());
TransactionConcurrency concurrency = rnd.nextBoolean() ? PESSIMISTIC : OPTIMISTIC;
doInTransaction(node, concurrency, REPEATABLE_READ, new Callable<Void>() {
@Override
public Void call() throws Exception {
cache.putAll(map);
return null;
}
});
}
return null;
}
}, 5, "non-ser-thread");
}
IgniteInternalFuture<?> updateFut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
int nodeIdx = idx.getAndIncrement() % updateNodes.size();
Ignite node = updateNodes.get(nodeIdx);
log.info("Tx thread: " + node.name());
final IgniteTransactions txs = node.transactions();
final IgniteCache<Integer, Integer> cache = node.cache(cacheName);
assertNotNull(cache);
final ThreadLocalRandom rnd = ThreadLocalRandom.current();
while (U.currentTimeMillis() < stopTime) {
final Map<Integer, Integer> map = new LinkedHashMap<>();
for (int i = 0; i < KEYS / 2; i++) map.put(rnd.nextInt(KEYS), rnd.nextInt());
try {
if (restart) {
doInTransaction(node, OPTIMISTIC, SERIALIZABLE, new Callable<Void>() {
@Override
public Void call() throws Exception {
if (get) {
for (Map.Entry<Integer, Integer> e : map.entrySet()) {
if (rnd.nextBoolean()) {
cache.get(e.getKey());
if (rnd.nextBoolean())
cache.put(e.getKey(), e.getValue());
} else
cache.put(e.getKey(), e.getValue());
}
} else
cache.putAll(map);
return null;
}
});
} else {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
if (get) {
for (Map.Entry<Integer, Integer> e : map.entrySet()) {
if (rnd.nextBoolean()) {
cache.get(e.getKey());
if (rnd.nextBoolean())
cache.put(e.getKey(), e.getValue());
} else
cache.put(e.getKey(), e.getValue());
}
} else
cache.putAll(map);
tx.commit();
}
}
} catch (TransactionOptimisticException ignore) {
// No-op.
} catch (Throwable e) {
log.error("Unexpected error: " + e, e);
throw e;
}
}
return null;
}
}, threads, "tx-thread");
updateFut.get(60, SECONDS);
if (nonSerFut != null)
nonSerFut.get(60, SECONDS);
IgniteCache<Integer, Integer> cache = srv.cache(cacheName);
for (int key = 0; key < KEYS; key++) {
Integer val = cache.get(key);
for (int node = 1; node < SRVS + CLIENTS; node++) assertEquals(val, ignite(node).cache(cache.getName()).get(key));
}
}
finished.set(true);
if (fut != null)
fut.get();
} finally {
finished.set(true);
}
} finally {
destroyCache(cacheName);
}
}
use of org.apache.ignite.transactions.TransactionConcurrency in project ignite by apache.
the class CacheTxFastFinishTest method fastFinishTx.
/**
* @param ignite Node.
*/
private void fastFinishTx(Ignite ignite) {
IgniteTransactions txs = ignite.transactions();
IgniteCache cache = ignite.cache(DEFAULT_CACHE_NAME);
for (boolean commit : new boolean[] { true, false }) {
for (TransactionConcurrency c : TransactionConcurrency.values()) {
for (TransactionIsolation isolation : TransactionIsolation.values()) {
try (Transaction tx = txs.txStart(c, isolation)) {
checkFastTxFinish(tx, commit);
}
}
}
for (int i = 0; i < 100; i++) {
try (Transaction tx = txs.txStart(OPTIMISTIC, REPEATABLE_READ)) {
cache.get(i);
checkFastTxFinish(tx, commit);
}
try (Transaction tx = txs.txStart(OPTIMISTIC, READ_COMMITTED)) {
cache.get(i);
checkFastTxFinish(tx, commit);
}
}
for (int i = 0; i < 100; i++) {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
cache.get(i);
checkNormalTxFinish(tx, commit);
}
try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
cache.get(i);
checkNormalTxFinish(tx, commit);
}
}
for (int i = 0; i < 100; i++) {
for (TransactionConcurrency c : TransactionConcurrency.values()) {
for (TransactionIsolation isolation : TransactionIsolation.values()) {
try (Transaction tx = txs.txStart(c, isolation)) {
cache.put(i, i);
checkNormalTxFinish(tx, commit);
}
}
}
}
}
}
use of org.apache.ignite.transactions.TransactionConcurrency in project ignite by apache.
the class CacheReadThroughRestartSelfTest method testReadThroughInTx.
/**
* @throws Exception If failed.
*/
private void testReadThroughInTx(boolean needVer) throws Exception {
IgniteCache<String, Integer> cache = grid(1).cache(DEFAULT_CACHE_NAME);
for (int k = 0; k < 1000; k++) cache.put("key" + k, k);
stopAllGrids();
startGrids(2);
awaitPartitionMapExchange();
Ignite ignite = grid(1);
cache = ignite.cache(DEFAULT_CACHE_NAME);
for (TransactionConcurrency txConcurrency : TransactionConcurrency.values()) {
for (TransactionIsolation txIsolation : TransactionIsolation.values()) {
try (Transaction tx = ignite.transactions().txStart(txConcurrency, txIsolation, 100000, 1000)) {
for (int k = 0; k < 1000; k++) {
String key = "key" + k;
if (needVer) {
assertNotNull("Null value for key: " + key, cache.getEntry(key));
assertNotNull("Null value for key: " + key, cache.getEntry(key));
} else {
assertNotNull("Null value for key: " + key, cache.get(key));
assertNotNull("Null value for key: " + key, cache.get(key));
}
}
tx.commit();
}
}
}
}
use of org.apache.ignite.transactions.TransactionConcurrency in project ignite by apache.
the class GridCacheBinaryObjectsAbstractSelfTest method testKeepBinaryTxOverwrite.
/**
* @throws Exception if failed.
*/
public void testKeepBinaryTxOverwrite() throws Exception {
if (atomicityMode() != TRANSACTIONAL)
return;
IgniteCache<Integer, TestObject> cache = ignite(0).cache(DEFAULT_CACHE_NAME);
cache.put(0, new TestObject(1));
for (TransactionConcurrency conc : TransactionConcurrency.values()) {
for (TransactionIsolation iso : TransactionIsolation.values()) {
try (Transaction tx = ignite(0).transactions().txStart(conc, iso)) {
cache.withKeepBinary().get(0);
cache.invoke(0, new ObjectEntryProcessor());
tx.commit();
}
}
}
}
use of org.apache.ignite.transactions.TransactionConcurrency in project ignite by apache.
the class CacheLateAffinityAssignmentTest method cacheOperations.
/**
* @param cache Cache
*/
private void cacheOperations(IgniteCache<Object, Object> cache) {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
final int KEYS = 10_000;
try {
cache.get(rnd.nextInt(KEYS));
cache.put(rnd.nextInt(KEYS), rnd.nextInt(10));
cache.getAndPut(rnd.nextInt(KEYS), rnd.nextInt(10));
cache.remove(rnd.nextInt(KEYS));
cache.getAndRemove(rnd.nextInt(KEYS));
cache.remove(rnd.nextInt(KEYS), rnd.nextInt(10));
cache.putIfAbsent(rnd.nextInt(KEYS), rnd.nextInt(10));
cache.replace(rnd.nextInt(KEYS), rnd.nextInt(10));
cache.replace(rnd.nextInt(KEYS), rnd.nextInt(10), rnd.nextInt(10));
cache.invoke(rnd.nextInt(KEYS), new TestEntryProcessor(rnd.nextInt(10)));
if (cache.getConfiguration(CacheConfiguration.class).getAtomicityMode() == TRANSACTIONAL) {
IgniteTransactions txs = cache.unwrap(Ignite.class).transactions();
for (TransactionConcurrency concurrency : TransactionConcurrency.values()) {
for (TransactionIsolation isolation : TransactionIsolation.values()) {
try (Transaction tx = txs.txStart(concurrency, isolation)) {
Integer key = rnd.nextInt(KEYS);
cache.getAndPut(key, rnd.nextInt(10));
cache.invoke(key + 1, new TestEntryProcessor(rnd.nextInt(10)));
cache.get(key + 2);
tx.commit();
}
}
}
}
} catch (Exception e) {
log.info("Cache operation failed: " + e);
}
}
Aggregations