use of org.apache.ignite.IgniteTransactions in project ignite by apache.
the class IgnitePessimisticTxSuspendResumeTest method testSuspendPessimisticTx.
/**
* Test for suspension on pessimistic transaction.
*
* @throws Exception If failed.
*/
public void testSuspendPessimisticTx() throws Exception {
try (Ignite g = startGrid()) {
IgniteCache<Integer, String> cache = jcache();
IgniteTransactions txs = g.transactions();
for (TransactionIsolation isolation : TransactionIsolation.values()) {
final Transaction tx = txs.txStart(TransactionConcurrency.PESSIMISTIC, isolation);
cache.put(1, "1");
GridTestUtils.assertThrowsWithCause(new Callable<Object>() {
@Override
public Object call() throws Exception {
tx.suspend();
return null;
}
}, UnsupportedOperationException.class);
tx.close();
assertNull(cache.get(1));
}
}
}
use of org.apache.ignite.IgniteTransactions in project ignite by apache.
the class IgniteClientReconnectCacheTest method testReconnectTransactions.
/**
* @throws Exception If failed.
*/
public void testReconnectTransactions() throws Exception {
clientMode = true;
IgniteEx client = startGrid(SRV_CNT);
Ignite srv = clientRouter(client);
CacheConfiguration<Object, Object> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
ccfg.setAtomicityMode(TRANSACTIONAL);
ccfg.setCacheMode(PARTITIONED);
ccfg.setBackups(1);
IgniteCache<Object, Object> cache = client.getOrCreateCache(ccfg);
final IgniteTransactions txs = client.transactions();
final Transaction tx = txs.txStart(OPTIMISTIC, REPEATABLE_READ);
cache.put(1, 1);
reconnectClientNode(client, srv, new Runnable() {
@Override
public void run() {
try {
tx.commit();
fail();
} catch (IgniteClientDisconnectedException e) {
log.info("Expected error: " + e);
assertNotNull(e.reconnectFuture());
}
try {
txs.txStart();
fail();
} catch (IgniteClientDisconnectedException e) {
log.info("Expected error: " + e);
assertNotNull(e.reconnectFuture());
}
}
});
assertNull(txs.tx());
try (Transaction tx0 = txs.txStart(OPTIMISTIC, REPEATABLE_READ)) {
cache.put(1, 1);
assertEquals(1, cache.get(1));
tx0.commit();
}
try (Transaction tx0 = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
cache.put(2, 2);
assertEquals(2, cache.get(2));
tx0.commit();
}
}
use of org.apache.ignite.IgniteTransactions in project ignite by apache.
the class IgniteClientReconnectFailoverTest method testReconnectTxCache.
/**
* @throws Exception If failed.
*/
public void testReconnectTxCache() throws Exception {
final Ignite client = grid(serverCount());
final IgniteCache<Integer, Integer> cache = client.cache(TX_CACHE);
assertNotNull(cache);
assertEquals(TRANSACTIONAL, cache.getConfiguration(CacheConfiguration.class).getAtomicityMode());
final IgniteTransactions txs = client.transactions();
reconnectFailover(new Callable<Void>() {
@Override
public Void call() throws Exception {
try {
TreeMap<Integer, Integer> map = new TreeMap<>();
ThreadLocalRandom rnd = ThreadLocalRandom.current();
for (int i = 0; i < 5; i++) {
Integer key = rnd.nextInt(0, 100_000);
cache.put(key, key);
assertEquals(key, cache.get(key));
map.put(key, key);
}
for (TransactionConcurrency txConcurrency : TransactionConcurrency.values()) {
try (Transaction tx = txs.txStart(txConcurrency, REPEATABLE_READ)) {
for (Map.Entry<Integer, Integer> e : map.entrySet()) {
cache.put(e.getKey(), e.getValue());
assertNotNull(cache.get(e.getKey()));
}
tx.commit();
}
}
cache.putAll(map);
Map<Integer, Integer> res = cache.getAll(map.keySet());
assertEquals(map, res);
} catch (IgniteClientDisconnectedException e) {
throw e;
} catch (IgniteException e) {
log.info("Ignore error: " + e);
} catch (CacheException e) {
if (e.getCause() instanceof IgniteClientDisconnectedException)
throw e;
else
log.info("Ignore error: " + e);
}
return null;
}
});
}
use of org.apache.ignite.IgniteTransactions in project ignite by apache.
the class IgniteCachePutRetryAtomicSelfTest method testPutInsideTransaction.
/**
* @throws Exception If failed.
*/
public void testPutInsideTransaction() throws Exception {
ignite(0).createCache(cacheConfiguration(false, false));
CacheConfiguration<Integer, Integer> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
ccfg.setName("tx-cache");
ccfg.setAtomicityMode(TRANSACTIONAL);
try (IgniteCache<Integer, Integer> txCache = ignite(0).getOrCreateCache(ccfg)) {
final AtomicBoolean finished = new AtomicBoolean();
IgniteInternalFuture<Object> fut = GridTestUtils.runAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
while (!finished.get()) {
stopGrid(3);
U.sleep(300);
startGrid(3);
}
return null;
}
});
try {
IgniteTransactions txs = ignite(0).transactions();
IgniteCache<Object, Object> cache = ignite(0).cache(DEFAULT_CACHE_NAME);
long stopTime = System.currentTimeMillis() + 60_000;
while (System.currentTimeMillis() < stopTime) {
for (int i = 0; i < 10_000; i++) {
try {
try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
txCache.put(0, 0);
cache.put(i, i);
tx.commit();
}
} catch (IgniteException | CacheException e) {
log.info("Ignore exception: " + e);
}
}
}
finished.set(true);
fut.get();
} finally {
finished.set(true);
}
}
}
use of org.apache.ignite.IgniteTransactions in project ignite by apache.
the class GridCachePartitionedEvictionSelfTest method doTestEviction.
/**
* @throws Exception If failed.
* @param concurrency Tx concurrency.
* @param isolation Tx isolation.
*/
private void doTestEviction(TransactionConcurrency concurrency, TransactionIsolation isolation) throws Exception {
assert concurrency != null;
assert isolation != null;
// This condition should be "true", otherwise the test doesn't make sense.
assert KEY_CNT >= EVICT_CACHE_SIZE;
GridDhtCacheAdapter<String, Integer> dht0 = dht(jcache(0));
GridDhtCacheAdapter<String, Integer> dht1 = dht(jcache(1));
Affinity<String> aff = dht0.affinity();
TouchedExpiryPolicy plc = new TouchedExpiryPolicy(new Duration(MILLISECONDS, 10));
for (int kv = 0; kv < KEY_CNT; kv++) {
String key = String.valueOf(kv);
ClusterNode node = aff.mapKeyToNode(key);
IgniteCache<String, Integer> c = cache(node);
IgniteTransactions txs = G.ignite(node.id()).transactions();
try (Transaction tx = txs.txStart(concurrency, isolation)) {
assert c.get(key) == null;
c.withExpiryPolicy(plc).put(key, 1);
assertEquals(Integer.valueOf(kv), c.get(key));
tx.commit();
}
}
if (TEST_INFO) {
info("Printing keys in dht0...");
for (String key : dht0.keySet()) info("[key=" + key + ", primary=" + F.eqNodes(grid(0).localNode(), aff.mapKeyToNode(key)) + ']');
info("Printing keys in dht1...");
for (String key : dht1.keySet()) info("[key=" + key + ", primary=" + F.eqNodes(grid(1).localNode(), aff.mapKeyToNode(key)) + ']');
}
assertEquals(EVICT_CACHE_SIZE, dht0.size());
assertEquals(EVICT_CACHE_SIZE, dht1.size());
assertEquals(0, near(jcache(0)).nearSize());
assertEquals(0, near(jcache(1)).nearSize());
}
Aggregations