use of org.apache.ignite.IgniteCache in project ignite by apache.
the class GridCacheAbstractFailoverSelfTest method testConstantTopologyChange.
/**
* @param concurrency Concurrency control.
* @param isolation Isolation level.
* @throws Exception If failed.
*/
protected void testConstantTopologyChange(@Nullable final TransactionConcurrency concurrency, @Nullable final TransactionIsolation isolation) throws Exception {
final boolean tx = concurrency != null && isolation != null;
if (tx)
put(ignite(0), jcache(), ENTRY_CNT, concurrency, isolation);
else
put(jcache(), ENTRY_CNT);
check(jcache(), ENTRY_CNT);
final int half = ENTRY_CNT / 2;
final AtomicReference<Exception> err = new AtomicReference<>();
IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new CA() {
@Override
public void apply() {
info("Run topology change.");
try {
String name = "new-node-" + Thread.currentThread().getName();
for (int i = 0; i < TOP_CHANGE_CNT && err.get() == null; i++) {
info("Topology change " + i);
try {
final Ignite g = startGrid(name);
IgniteCache<String, Object> cache = g.cache(DEFAULT_CACHE_NAME);
for (int k = half; k < ENTRY_CNT; k++) {
String key = "key" + k;
assertNotNull("Failed to get key: 'key" + k + "'", cache.getAsync(key).get(30_000));
}
} finally {
G.stop(name, false);
}
}
} catch (Exception e) {
err.set(e);
log.error("Unexpected exception in topology-change-thread: " + e, e);
}
}
}, TOP_CHANGE_THREAD_CNT, "topology-change-thread");
try {
while (!fut.isDone()) {
if (tx) {
remove(grid(0), jcache(), half, concurrency, isolation);
put(grid(0), jcache(), half, concurrency, isolation);
} else {
remove(jcache(), half);
put(jcache(), half);
}
}
} catch (Exception e) {
err.set(e);
log.error("Unexpected exception: " + e, e);
throw e;
}
fut.get();
Exception err0 = err.get();
if (err0 != null)
throw err0;
}
use of org.apache.ignite.IgniteCache in project ignite by apache.
the class CacheSerializableTransactionsTest method testTxConflictReplace.
/**
* @throws Exception If failed.
*/
public void testTxConflictReplace() 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, 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, 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, 2);
assertFalse(replace);
tx.commit();
}
checkValue(key, null, cache.getName());
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean replace = cache.replace(key, 2);
assertFalse(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, 2);
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);
assertTrue(replace);
tx.commit();
}
checkValue(key, 2, cache.getName());
}
} finally {
destroyCache(ccfg.getName());
}
}
}
use of org.apache.ignite.IgniteCache in project ignite by apache.
the class CacheSerializableTransactionsTest method txNoConflictUpdate.
/**
* @throws Exception If failed.
* @param noVal If {@code true} there is no cache value when do update in tx.
* @param rmv If {@code true} tests remove, otherwise put.
* @param getAfterUpdate If {@code true} tries to get value in tx after update.
*/
private void txNoConflictUpdate(boolean noVal, boolean rmv, boolean getAfterUpdate) 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 (Integer key : keys) {
log.info("Test key: " + key);
if (!noVal)
cache.put(key, -1);
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
if (rmv)
cache.remove(key);
else
cache.put(key, 2);
if (getAfterUpdate) {
Object val = cache.get(key);
if (rmv)
assertNull(val);
else
assertEquals(2, val);
}
if (!rmv)
updateKey(cache, key, 1);
tx.commit();
}
checkValue(key, rmv ? null : 2, cache.getName());
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
cache.put(key, 3);
tx.commit();
}
checkValue(key, 3, cache.getName());
}
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < 100; i++) map.put(i, i);
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
if (rmv)
cache.removeAll(map.keySet());
else
cache.putAll(map);
if (getAfterUpdate) {
Map<Integer, Integer> res = cache.getAll(map.keySet());
if (rmv) {
for (Integer key : map.keySet()) assertNull(res.get(key));
} else {
for (Integer key : map.keySet()) assertEquals(map.get(key), res.get(key));
}
}
txAsync(cache, PESSIMISTIC, REPEATABLE_READ, new IgniteClosure<IgniteCache<Integer, Integer>, Void>() {
@Override
public Void apply(IgniteCache<Integer, Integer> cache) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < 100; i++) map.put(i, -1);
cache.putAll(map);
return null;
}
});
tx.commit();
}
for (int i = 0; i < 100; i++) checkValue(i, rmv ? null : i, cache.getName());
} finally {
destroyCache(ccfg.getName());
}
}
}
use of org.apache.ignite.IgniteCache in project ignite by apache.
the class CacheSerializableTransactionsTest method testTxConflictRemoveWithOldValue.
/**
* @throws Exception If failed.
*/
public void testTxConflictRemoveWithOldValue() 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 rmv = cache.remove(key, 2);
assertFalse(rmv);
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 rmv = cache.remove(key, 1);
assertTrue(rmv);
tx.commit();
}
checkValue(key, null, cache.getName());
cache.remove(key);
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean rmv = cache.remove(key, 2);
assertFalse(rmv);
tx.commit();
}
checkValue(key, null, cache.getName());
cache.put(key, 2);
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean rmv = cache.remove(key, 2);
assertTrue(rmv);
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 rmv = cache.remove(key, 3);
assertTrue(rmv);
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 rmv = cache.remove(key, 2);
assertFalse(rmv);
tx.commit();
}
checkValue(key, 1, cache.getName());
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
boolean rmv = cache.remove(key, 1);
assertTrue(rmv);
tx.commit();
}
checkValue(key, null, cache.getName());
}
} finally {
destroyCache(ccfg.getName());
}
}
}
use of org.apache.ignite.IgniteCache in project ignite by apache.
the class IgniteWalRecoveryTest method testHugeCheckpointRecord.
/**
* @throws Exception if failed.
*/
public void testHugeCheckpointRecord() throws Exception {
try {
final IgniteEx ignite = startGrid(1);
ignite.active(true);
for (int i = 0; i < 50; i++) {
CacheConfiguration<Object, Object> ccfg = new CacheConfiguration<>("cache-" + i);
// We can get 'too many open files' with default number of partitions.
ccfg.setAffinity(new RendezvousAffinityFunction(false, 128));
IgniteCache<Object, Object> cache = ignite.getOrCreateCache(ccfg);
cache.put(i, i);
}
final long endTime = System.currentTimeMillis() + 30_000;
IgniteInternalFuture<Long> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
Random rnd = ThreadLocalRandom.current();
while (U.currentTimeMillis() < endTime) {
IgniteCache<Object, Object> cache = ignite.cache("cache-" + rnd.nextInt(50));
cache.put(rnd.nextInt(50_000), rnd.nextInt());
}
return null;
}
}, 16, "put-thread");
while (System.currentTimeMillis() < endTime) {
ignite.context().cache().context().database().wakeupForCheckpoint("test").get();
U.sleep(500);
}
fut.get();
} finally {
stopAllGrids();
}
}
Aggregations