use of org.apache.ignite.IgniteCache in project ignite by apache.
the class CacheSerializableTransactionsTest method incrementTx.
/**
* @param nearCache If {@code true} near cache is enabled.
* @param store If {@code true} cache store is enabled.
* @param restart If {@code true} restarts one node.
* @throws Exception If failed.
*/
private void incrementTx(boolean nearCache, boolean store, final boolean restart) throws Exception {
final Ignite srv = ignite(1);
CacheConfiguration<Integer, Integer> ccfg = cacheConfiguration(PARTITIONED, FULL_SYNC, 1, store, false);
final List<Ignite> clients = clients();
final String cacheName = srv.createCache(ccfg).getName();
final AtomicBoolean stop = new AtomicBoolean();
try {
final List<IgniteCache<Integer, Integer>> caches = new ArrayList<>();
for (Ignite client : clients) {
if (nearCache)
caches.add(client.createNearCache(cacheName, new NearCacheConfiguration<Integer, Integer>()));
else
caches.add(client.<Integer, Integer>cache(cacheName));
}
IgniteInternalFuture<?> restartFut = restart ? restartFuture(stop, null) : null;
final long stopTime = U.currentTimeMillis() + getTestTimeout() - 30_000;
for (int i = 0; i < 30; i++) {
final AtomicInteger cntr = new AtomicInteger();
final Integer key = i;
final AtomicInteger threadIdx = new AtomicInteger();
final int THREADS = 10;
final CyclicBarrier barrier = new CyclicBarrier(THREADS);
GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
int idx = threadIdx.getAndIncrement() % caches.size();
IgniteCache<Integer, Integer> cache = caches.get(idx);
Ignite ignite = cache.unwrap(Ignite.class);
IgniteTransactions txs = ignite.transactions();
log.info("Started update thread: " + ignite.name());
barrier.await();
for (int i = 0; i < 1000; i++) {
if (i % 100 == 0 && U.currentTimeMillis() > stopTime)
break;
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
Integer val = cache.get(key);
cache.put(key, val == null ? 1 : val + 1);
tx.commit();
}
cntr.incrementAndGet();
} catch (TransactionOptimisticException ignore) {
// Retry.
} catch (IgniteException | CacheException e) {
assertTrue("Unexpected exception [err=" + e + ", cause=" + e.getCause() + ']', restart && X.hasCause(e, ClusterTopologyCheckedException.class));
}
}
return null;
}
}, THREADS, "update-thread").get();
log.info("Iteration [iter=" + i + ", val=" + cntr.get() + ']');
assertTrue(cntr.get() > 0);
checkValue(key, cntr.get(), cacheName, restart);
if (U.currentTimeMillis() > stopTime)
break;
}
stop.set(true);
if (restartFut != null)
restartFut.get();
} finally {
stop.set(true);
destroyCache(cacheName);
}
}
use of org.apache.ignite.IgniteCache in project ignite by apache.
the class CacheSerializableTransactionsTest method testNoOptimisticExceptionOnChangingTopology.
/**
* @throws Exception If failed.
*/
public void testNoOptimisticExceptionOnChangingTopology() throws Exception {
if (FAST)
return;
final AtomicBoolean finished = new AtomicBoolean();
final List<String> cacheNames = new ArrayList<>();
Ignite srv = ignite(1);
try {
{
CacheConfiguration<Integer, Integer> ccfg = cacheConfiguration(PARTITIONED, FULL_SYNC, 1, false, false);
ccfg.setName("cache1");
ccfg.setRebalanceMode(SYNC);
srv.createCache(ccfg);
cacheNames.add(ccfg.getName());
}
{
// Store enabled.
CacheConfiguration<Integer, Integer> ccfg = cacheConfiguration(PARTITIONED, FULL_SYNC, 1, true, false);
ccfg.setName("cache2");
ccfg.setRebalanceMode(SYNC);
srv.createCache(ccfg);
cacheNames.add(ccfg.getName());
}
{
// Eviction.
CacheConfiguration<Integer, Integer> ccfg = cacheConfiguration(PARTITIONED, FULL_SYNC, 1, false, false);
ccfg.setName("cache3");
ccfg.setRebalanceMode(SYNC);
LruEvictionPolicy plc = new LruEvictionPolicy();
plc.setMaxSize(100);
ccfg.setEvictionPolicy(plc);
ccfg.setOnheapCacheEnabled(true);
srv.createCache(ccfg);
cacheNames.add(ccfg.getName());
}
IgniteInternalFuture<?> restartFut = restartFuture(finished, null);
List<IgniteInternalFuture<?>> futs = new ArrayList<>();
final int KEYS_PER_THREAD = 100;
for (int i = 1; i < SRVS + CLIENTS; i++) {
final Ignite node = ignite(i);
final int minKey = i * KEYS_PER_THREAD;
final int maxKey = minKey + KEYS_PER_THREAD;
// Threads update non-intersecting keys, optimistic exception should not be thrown.
futs.add(GridTestUtils.runAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
try {
log.info("Started update thread [node=" + node.name() + ", minKey=" + minKey + ", maxKey=" + maxKey + ']');
final ThreadLocalRandom rnd = ThreadLocalRandom.current();
List<IgniteCache<Integer, Integer>> caches = new ArrayList<>();
for (String cacheName : cacheNames) caches.add(node.<Integer, Integer>cache(cacheName));
assertEquals(3, caches.size());
int iter = 0;
while (!finished.get()) {
int keyCnt = rnd.nextInt(1, 10);
final Set<Integer> keys = new LinkedHashSet<>();
while (keys.size() < keyCnt) keys.add(rnd.nextInt(minKey, maxKey));
for (final IgniteCache<Integer, Integer> cache : caches) {
doInTransaction(node, OPTIMISTIC, SERIALIZABLE, new Callable<Void>() {
@Override
public Void call() throws Exception {
for (Integer key : keys) randomOperation(rnd, cache, key);
return null;
}
});
}
if (iter % 100 == 0)
log.info("Iteration: " + iter);
iter++;
}
return null;
} catch (Throwable e) {
log.error("Unexpected error: " + e, e);
throw e;
}
}
}, "update-thread-" + i));
}
U.sleep(60_000);
finished.set(true);
restartFut.get();
for (IgniteInternalFuture<?> fut : futs) fut.get();
} finally {
finished.set(true);
for (String cacheName : cacheNames) destroyCache(cacheName);
}
}
use of org.apache.ignite.IgniteCache in project ignite by apache.
the class CacheSerializableTransactionsTest method testTxCommitReadOnly2.
/**
* @throws Exception If failed.
*/
public void testTxCommitReadOnly2() throws Exception {
Ignite ignite0 = ignite(0);
final IgniteTransactions txs = ignite0.transactions();
for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
logCacheInfo(ccfg);
try {
IgniteCache<Integer, Integer> cache = ignite0.createCache(ccfg);
List<Integer> keys = testKeys(cache);
for (final Integer key : keys) {
log.info("Test key: " + key);
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
Integer val = cache.get(key);
assertNull(val);
txAsync(cache, OPTIMISTIC, SERIALIZABLE, new IgniteClosure<IgniteCache<Integer, Integer>, Void>() {
@Override
public Void apply(IgniteCache<Integer, Integer> cache) {
cache.get(key);
return null;
}
});
tx.commit();
}
checkValue(key, null, cache.getName());
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
Integer val = cache.get(key);
assertNull(val);
txAsync(cache, PESSIMISTIC, REPEATABLE_READ, new IgniteClosure<IgniteCache<Integer, Integer>, Void>() {
@Override
public Void apply(IgniteCache<Integer, Integer> cache) {
cache.get(key);
return null;
}
});
tx.commit();
}
checkValue(key, null, cache.getName());
}
} finally {
destroyCache(ccfg.getName());
}
}
}
use of org.apache.ignite.IgniteCache in project ignite by apache.
the class CacheSerializableTransactionsTest method testTxConflictCasReplace.
/**
* @throws Exception If failed.
*/
public void testTxConflictCasReplace() throws Exception {
Ignite ignite0 = ignite(0);
final IgniteTransactions txs = ignite0.transactions();
for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
logCacheInfo(ccfg);
try {
IgniteCache<Integer, Integer> cache = ignite0.createCache(ccfg);
List<Integer> keys = testKeys(cache);
for (final Integer key : keys) {
log.info("Test key: " + key);
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean replace = cache.replace(key, 1, 2);
assertFalse(replace);
updateKey(cache, key, 1);
tx.commit();
}
fail();
} catch (TransactionOptimisticException e) {
log.info("Expected exception: " + e);
}
checkValue(key, 1, cache.getName());
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean replace = cache.replace(key, 1, 2);
assertTrue(replace);
tx.commit();
}
checkValue(key, 2, cache.getName());
cache.remove(key);
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean replace = cache.replace(key, 1, 2);
assertFalse(replace);
tx.commit();
}
checkValue(key, null, cache.getName());
cache.put(key, 2);
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean replace = cache.replace(key, 2, 1);
assertTrue(replace);
updateKey(cache, key, 3);
tx.commit();
}
fail();
} catch (TransactionOptimisticException e) {
log.info("Expected exception: " + e);
}
checkValue(key, 3, cache.getName());
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean replace = cache.replace(key, 3, 4);
assertTrue(replace);
txAsync(cache, OPTIMISTIC, SERIALIZABLE, new IgniteClosure<IgniteCache<Integer, Integer>, Void>() {
@Override
public Void apply(IgniteCache<Integer, Integer> cache) {
cache.remove(key);
return null;
}
});
tx.commit();
}
fail();
} catch (TransactionOptimisticException e) {
log.info("Expected exception: " + e);
}
checkValue(key, null, cache.getName());
cache.put(key, 1);
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean replace = cache.replace(key, 2, 3);
assertFalse(replace);
tx.commit();
}
checkValue(key, 1, cache.getName());
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean replace = cache.replace(key, 1, 3);
assertTrue(replace);
tx.commit();
}
checkValue(key, 3, cache.getName());
}
} finally {
destroyCache(ccfg.getName());
}
}
}
use of org.apache.ignite.IgniteCache in project ignite by apache.
the class CacheSerializableTransactionsTest method checkReadWriteTransactionsNoDeadlock.
/**
* @param multiNode Multi-node test flag.
* @throws Exception If failed.
*/
private void checkReadWriteTransactionsNoDeadlock(final boolean multiNode) throws Exception {
final Ignite ignite0 = ignite(0);
for (final CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
logCacheInfo(ccfg);
ignite0.createCache(ccfg);
try {
final long stopTime = U.currentTimeMillis() + 10_000;
final AtomicInteger idx = new AtomicInteger();
GridTestUtils.runMultiThreaded(new Callable<Void>() {
@Override
public Void call() throws Exception {
Ignite ignite = multiNode ? ignite(idx.incrementAndGet() % (SRVS + CLIENTS)) : ignite0;
IgniteCache<Integer, Integer> cache = ignite.cache(ccfg.getName());
ThreadLocalRandom rnd = ThreadLocalRandom.current();
while (U.currentTimeMillis() < stopTime) {
try {
try (Transaction tx = ignite.transactions().txStart(OPTIMISTIC, SERIALIZABLE)) {
for (int i = 0; i < 10; i++) {
Integer key = rnd.nextInt(30);
if (rnd.nextBoolean())
cache.get(key);
else
cache.put(key, key);
}
tx.commit();
}
} catch (TransactionOptimisticException ignore) {
// No-op.
}
}
return null;
}
}, 32, "test-thread");
} finally {
destroyCache(ccfg.getName());
}
}
}
Aggregations