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());
}
}
}
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 testNoReadLockConflictMultiNode.
/**
* @throws Exception If failed.
*/
public void testNoReadLockConflictMultiNode() throws Exception {
Ignite ignite0 = ignite(0);
for (final CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
logCacheInfo(ccfg);
final AtomicInteger putKey = new AtomicInteger(1_000_000);
ignite0.createCache(ccfg);
try {
final int THREADS = 64;
IgniteCache<Integer, Integer> cache0 = ignite0.cache(ccfg.getName());
List<Integer> readKeys = testKeys(cache0);
for (final Integer readKey : readKeys) {
final CyclicBarrier barrier = new CyclicBarrier(THREADS);
cache0.put(readKey, Integer.MIN_VALUE);
final AtomicInteger idx = new AtomicInteger();
GridTestUtils.runMultiThreaded(new Callable<Void>() {
@Override
public Void call() throws Exception {
Ignite ignite = ignite(idx.incrementAndGet() % (CLIENTS + SRVS));
IgniteCache<Integer, Integer> cache = ignite.cache(ccfg.getName());
try (Transaction tx = ignite.transactions().txStart(OPTIMISTIC, SERIALIZABLE)) {
cache.get(readKey);
barrier.await();
cache.put(putKey.incrementAndGet(), 0);
tx.commit();
}
return null;
}
}, THREADS, "test-thread");
assertEquals((Integer) Integer.MIN_VALUE, cache0.get(readKey));
cache0.put(readKey, readKey);
assertEquals(readKey, cache0.get(readKey));
}
} finally {
destroyCache(ccfg.getName());
}
}
}
use of org.apache.ignite.IgniteCache in project ignite by apache.
the class CacheStoreUsageMultinodeDynamicStartAbstractTest method checkStoreWithDynamicStart.
/**
* @param clientStart {@code True} if start cache from client node.
* @throws Exception If failed.
*/
private void checkStoreWithDynamicStart(boolean clientStart) throws Exception {
cacheStore = true;
CacheConfiguration ccfg = cacheConfiguration();
assertNotNull(ccfg.getCacheStoreFactory());
Ignite srv = ignite(0);
Ignite client = ignite(3);
Ignite node = clientStart ? client : srv;
IgniteCache cache = nearCache ? node.createCache(ccfg, new NearCacheConfiguration()) : node.createCache(ccfg);
assertNotNull(cache);
try {
if (nearCache)
client.createNearCache(DEFAULT_CACHE_NAME, new NearCacheConfiguration<>());
checkStoreUpdate(true);
} finally {
cache = srv.cache(DEFAULT_CACHE_NAME);
if (cache != null)
cache.destroy();
}
}
use of org.apache.ignite.IgniteCache in project ignite by apache.
the class CacheStartupInDeploymentModesTest method doCheckStarted.
/**
* @param mode Deployment mode.
* @throws Exception If failed.
*/
private void doCheckStarted(DeploymentMode mode) throws Exception {
startGridsMultiThreaded(2);
checkTopology(2);
assertEquals(mode, ignite(0).configuration().getDeploymentMode());
assert ignite(0).configuration().getMarshaller() instanceof BinaryMarshaller;
IgniteCache rCache = ignite(0).cache(REPLICATED_CACHE);
checkPutCache(rCache);
IgniteCache pCache = ignite(0).cache(PARTITIONED_CACHE);
checkPutCache(pCache);
}
Aggregations