use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class EvictionAbstractTest method checkPartitionedMultiThreaded.
/**
* @throws Exception If failed.
*/
protected void checkPartitionedMultiThreaded() throws Exception {
try {
startGridsMultiThreaded(gridCnt);
final Random rand = new Random();
final AtomicInteger cntr = new AtomicInteger();
multithreaded(new Callable() {
@Nullable
@Override
public Object call() throws Exception {
int cnt = 100;
for (int i = 0; i < cnt && !Thread.currentThread().isInterrupted(); i++) {
IgniteEx grid = grid(rand.nextInt(2));
IgniteCache<Integer, String> cache = grid.cache(DEFAULT_CACHE_NAME);
int key = rand.nextInt(1000);
String val = Integer.toString(key);
try (Transaction tx = grid.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
String v = cache.get(key);
assert v == null || v.equals(Integer.toString(key)) : "Invalid value for key [key=" + key + ", val=" + v + ']';
cache.put(key, val);
tx.commit();
}
if (cntr.incrementAndGet() % 100 == 0)
info("Stored cache object for key [key=" + key + ", idx=" + i + ']');
}
return null;
}
}, 10);
} finally {
stopAllGrids();
}
}
use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class GridCacheEvictionTouchSelfTest method testPolicyConsistency.
/**
* @throws Exception If failed.
*/
public void testPolicyConsistency() throws Exception {
FifoEvictionPolicy<Object, Object> plc = new FifoEvictionPolicy<>();
plc.setMaxSize(500);
this.plc = plc;
try {
Ignite ignite = startGrid(1);
final IgniteCache<Integer, Integer> cache = ignite.cache(DEFAULT_CACHE_NAME);
final Random rnd = new Random();
try (Transaction tx = ignite.transactions().txStart()) {
int iterCnt = 20;
int keyCnt = 5000;
for (int i = 0; i < iterCnt; i++) {
int j = rnd.nextInt(keyCnt);
// Put or remove?
if (rnd.nextBoolean())
cache.put(j, j);
else
cache.remove(j);
if (i != 0 && i % 1000 == 0)
info("Stats [iterCnt=" + i + ", size=" + cache.size() + ']');
}
FifoEvictionPolicy<Integer, Integer> plc0 = (FifoEvictionPolicy<Integer, Integer>) this.plc;
if (!plc0.queue().isEmpty()) {
for (Cache.Entry<Integer, Integer> e : plc0.queue()) U.warn(log, "Policy queue item: " + e);
fail("Test failed, see logs for details.");
}
tx.commit();
}
} catch (Throwable t) {
error("Test failed.", t);
fail("Test failed, see logs for details.");
} finally {
stopAllGrids();
}
}
use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class WithKeepBinaryCacheFullApiTest method runInAllTxModes.
/**
* @param task Task.
* @throws Exception If failed.
*/
protected void runInAllTxModes(TestRunnable task) throws Exception {
info("Executing implicite tx");
task.run();
if (txShouldBeUsed()) {
for (TransactionConcurrency conc : TransactionConcurrency.values()) {
for (TransactionIsolation isolation : TransactionIsolation.values()) {
try (Transaction tx = testedGrid().transactions().txStart(conc, isolation)) {
info("Executing explicite tx [isolation" + isolation + ", concurrency=" + conc + "]");
task.run();
tx.commit();
}
}
}
}
}
use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class IgniteCacheNoWriteThroughAbstractTest method testNoWriteThrough.
/**
* @throws Exception If failed.
*/
@SuppressWarnings("UnnecessaryLocalVariable")
public void testNoWriteThrough() throws Exception {
IgniteCache<Integer, Integer> cache = jcache(0);
for (Integer key : keys()) {
log.info("Test [key=" + key + ']');
final Integer storeVal = key;
storeMap.put(key, storeVal);
assertEquals(key, cache.get(key));
cache.remove(key);
assertEquals(storeVal, storeMap.get(key));
storeMap.remove(key);
assertNull(cache.get(key));
assertTrue(cache.putIfAbsent(key, key));
assertNull(storeMap.get(key));
assertEquals(key, cache.get(key));
cache.remove(key);
storeMap.put(key, storeVal);
Integer val = key + 1;
assertFalse(cache.putIfAbsent(key, val));
assertEquals(storeVal, storeMap.get(key));
cache.put(key, val);
assertEquals(val, cache.get(key));
assertEquals(storeVal, storeMap.get(key));
val = val + 1;
assertTrue(cache.replace(key, val));
assertEquals(val, cache.get(key));
assertEquals(storeVal, storeMap.get(key));
cache.remove(key);
assertEquals(storeVal, storeMap.get(key));
storeMap.remove(key);
assertNull(cache.get(key));
storeMap.put(key, storeVal);
val = val + 1;
assertEquals(storeVal, cache.getAndPut(key, val));
assertEquals(storeVal, storeMap.get(key));
assertEquals(val, cache.get(key));
cache.remove(key);
assertEquals(storeVal, storeMap.get(key));
assertEquals(storeVal, cache.getAndRemove(key));
cache.remove(key);
assertEquals(storeVal, storeMap.get(key));
Object ret = cache.invoke(key, new EntryProcessor<Integer, Integer, Object>() {
@Override
public Object process(MutableEntry<Integer, Integer> entry, Object... args) {
Integer val = entry.getValue();
entry.setValue(val + 1);
return String.valueOf(val);
}
});
assertEquals(String.valueOf(storeVal), ret);
assertEquals(storeVal + 1, (int) cache.get(key));
assertEquals(storeVal, storeMap.get(key));
assertTrue(cache.replace(key, storeVal + 1, storeVal + 2));
assertEquals(storeVal, storeMap.get(key));
assertEquals(storeVal + 2, (int) cache.get(key));
}
Map<Integer, Integer> expData = new HashMap<>();
for (int i = 1000_0000; i < 1000_0000 + 1000; i++) {
storeMap.put(i, i);
expData.put(i, i);
}
assertEquals(expData, cache.getAll(expData.keySet()));
storeMap.clear();
cache.putAll(expData);
assertTrue(storeMap.isEmpty());
assertEquals(expData, cache.getAll(expData.keySet()));
Map<Integer, Integer> expData0 = new HashMap<>();
for (int i = 1000_0000; i < 1000_0000 + 1000; i++) expData0.put(i, 1);
cache.invokeAll(expData.keySet(), new EntryProcessor<Integer, Integer, Object>() {
@Override
public Object process(MutableEntry<Integer, Integer> entry, Object... args) {
entry.setValue(1);
return null;
}
});
assertEquals(expData0, cache.getAll(expData0.keySet()));
assertTrue(storeMap.isEmpty());
storeMap.putAll(expData);
cache.removeAll(expData.keySet());
assertEquals(1000, storeMap.size());
storeMap.clear();
assertTrue(cache.getAll(expData.keySet()).isEmpty());
if (atomicityMode() == TRANSACTIONAL) {
for (TransactionConcurrency concurrency : TransactionConcurrency.values()) {
for (TransactionIsolation isolation : TransactionIsolation.values()) {
for (Integer key : keys()) {
log.info("Test tx [key=" + key + ", concurrency=" + concurrency + ", isolation=" + isolation + ']');
storeMap.put(key, key);
try (Transaction tx = ignite(0).transactions().txStart(concurrency, isolation)) {
assertEquals("For concurrency=" + concurrency + ", isolation=" + isolation + ']', key, cache.getAndPut(key, -1));
tx.commit();
}
assertEquals(-1, (int) cache.get(key));
assertEquals(key, storeMap.get(key));
try (Transaction tx = ignite(0).transactions().txStart(concurrency, isolation)) {
cache.put(key, -2);
tx.commit();
}
assertEquals(-2, (int) cache.get(key));
assertEquals(key, storeMap.get(key));
try (Transaction tx = ignite(0).transactions().txStart(concurrency, isolation)) {
assertEquals(-2, (int) cache.getAndRemove(key));
tx.commit();
}
assertEquals(key, storeMap.get(key));
storeMap.remove(key);
assertNull(cache.get(key));
storeMap.put(key, key);
cache.put(key, key);
try (Transaction tx = ignite(0).transactions().txStart(concurrency, isolation)) {
assertTrue(cache.replace(key, -1));
tx.commit();
}
assertEquals(-1, (int) cache.get(key));
assertEquals(key, storeMap.get(key));
cache.remove(key);
storeMap.clear();
try (Transaction tx = ignite(0).transactions().txStart(concurrency, isolation)) {
cache.putAll(expData);
tx.commit();
}
assertTrue(storeMap.isEmpty());
assertEquals(expData, cache.getAll(expData.keySet()));
try (Transaction tx = ignite(0).transactions().txStart(concurrency, isolation)) {
cache.invokeAll(expData.keySet(), new EntryProcessor<Integer, Integer, Object>() {
@Override
public Object process(MutableEntry<Integer, Integer> entry, Object... args) {
entry.setValue(1);
return null;
}
});
tx.commit();
}
assertEquals(expData0, cache.getAll(expData.keySet()));
assertTrue(storeMap.isEmpty());
storeMap.putAll(expData);
try (Transaction tx = ignite(0).transactions().txStart(concurrency, isolation)) {
cache.removeAll(expData.keySet());
tx.commit();
}
assertEquals(1000, storeMap.size());
storeMap.clear();
assertTrue(cache.getAll(expData.keySet()).isEmpty());
}
}
}
}
}
use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class CacheEntryProcessorNonSerializableTest method doTestInvokeTest.
/**
* @param ccfg Cache configuration.
* @throws Exception If failed.
*/
private void doTestInvokeTest(CacheConfiguration ccfg, TransactionConcurrency txConcurrency, TransactionIsolation txIsolation) throws Exception {
IgniteEx cln = grid(getServerNodeCount());
grid(0).createCache(ccfg);
IgniteCache clnCache;
if (ccfg.getNearConfiguration() != null)
clnCache = cln.createNearCache(ccfg.getName(), ccfg.getNearConfiguration());
else
clnCache = cln.cache(ccfg.getName());
putKeys(clnCache, EXPECTED_VALUE);
try {
// Explicit tx.
for (int i = 0; i < ITERATION_CNT; i++) {
try (final Transaction tx = cln.transactions().txStart(txConcurrency, txIsolation)) {
putKeys(clnCache, WRONG_VALUE);
clnCache.invoke(KEYS, new NonSerialazibleEntryProcessor());
GridTestUtils.assertThrowsWithCause(new Callable<Object>() {
@Override
public Object call() throws Exception {
tx.commit();
return null;
}
}, NotSerializableException.class);
}
checkKeys(clnCache, EXPECTED_VALUE);
}
// From affinity node.
Ignite grid = grid(ThreadLocalRandom.current().nextInt(NODES));
final IgniteCache cache = grid.cache(ccfg.getName());
// Explicit tx.
for (int i = 0; i < ITERATION_CNT; i++) {
try (final Transaction tx = grid.transactions().txStart(txConcurrency, txIsolation)) {
putKeys(cache, WRONG_VALUE);
cache.invoke(KEYS, new NonSerialazibleEntryProcessor());
GridTestUtils.assertThrowsWithCause(new Callable<Object>() {
@Override
public Object call() throws Exception {
tx.commit();
return null;
}
}, NotSerializableException.class);
}
checkKeys(cache, EXPECTED_VALUE);
}
final IgniteCache clnCache0 = clnCache;
// Implicit tx.
for (int i = 0; i < ITERATION_CNT; i++) {
GridTestUtils.assertThrowsWithCause(new Callable<Object>() {
@Override
public Object call() throws Exception {
clnCache0.invoke(KEYS, new NonSerialazibleEntryProcessor());
return null;
}
}, NotSerializableException.class);
}
checkKeys(clnCache, EXPECTED_VALUE);
} finally {
grid(0).destroyCache(ccfg.getName());
}
}
Aggregations