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());
}
}
}
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);
}
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();
}
}
Aggregations