use of java.util.concurrent.ThreadLocalRandom in project ignite by apache.
the class GridCacheAbstractRemoveFailureTest method putAndRemove.
/**
* @param duration Test duration.
* @param txConcurrency Transaction concurrency if test explicit transaction.
* @param txIsolation Transaction isolation if test explicit transaction.
* @throws Exception If failed.
*/
private void putAndRemove(long duration, final TransactionConcurrency txConcurrency, final TransactionIsolation txIsolation) throws Exception {
assertEquals(testClientNode(), (boolean) grid(0).configuration().isClientMode());
grid(0).destroyCache(DEFAULT_CACHE_NAME);
CacheConfiguration<Integer, Integer> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
ccfg.setWriteSynchronizationMode(FULL_SYNC);
ccfg.setCacheMode(cacheMode());
if (cacheMode() == PARTITIONED)
ccfg.setBackups(1);
ccfg.setAtomicityMode(atomicityMode());
ccfg.setNearConfiguration(nearCache());
final IgniteCache<Integer, Integer> sndCache0 = grid(0).createCache(ccfg);
final AtomicBoolean stop = new AtomicBoolean();
final AtomicLong cntr = new AtomicLong();
final AtomicLong errCntr = new AtomicLong();
// Expected values in cache.
final Map<Integer, GridTuple<Integer>> expVals = new ConcurrentHashMap<>();
final AtomicReference<CyclicBarrier> cmp = new AtomicReference<>();
IgniteInternalFuture<?> updateFut = GridTestUtils.runAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
Thread.currentThread().setName("update-thread");
ThreadLocalRandom rnd = ThreadLocalRandom.current();
IgniteTransactions txs = sndCache0.unwrap(Ignite.class).transactions();
while (!stop.get()) {
for (int i = 0; i < 100; i++) {
int key = rnd.nextInt(KEYS_CNT);
boolean put = rnd.nextInt(0, 100) > 10;
while (true) {
try {
if (put) {
boolean failed = false;
if (txConcurrency != null) {
try (Transaction tx = txs.txStart(txConcurrency, txIsolation)) {
sndCache0.put(key, i);
tx.commit();
} catch (CacheException | IgniteException e) {
if (!X.hasCause(e, ClusterTopologyCheckedException.class)) {
log.error("Unexpected error: " + e);
throw e;
}
failed = true;
}
} else
sndCache0.put(key, i);
if (!failed)
expVals.put(key, F.t(i));
} else {
boolean failed = false;
if (txConcurrency != null) {
try (Transaction tx = txs.txStart(txConcurrency, txIsolation)) {
sndCache0.remove(key);
tx.commit();
} catch (CacheException | IgniteException e) {
if (!X.hasCause(e, ClusterTopologyCheckedException.class)) {
log.error("Unexpected error: " + e);
throw e;
}
failed = true;
}
} else
sndCache0.remove(key);
if (!failed)
expVals.put(key, F.<Integer>t(null));
}
break;
} catch (CacheException e) {
if (put)
log.error("Put failed [key=" + key + ", val=" + i + ']', e);
else
log.error("Remove failed [key=" + key + ']', e);
errCntr.incrementAndGet();
}
}
}
cntr.addAndGet(100);
CyclicBarrier barrier = cmp.get();
if (barrier != null) {
log.info("Wait data check.");
barrier.await(60_000, TimeUnit.MILLISECONDS);
log.info("Finished wait data check.");
}
}
return null;
}
});
IgniteInternalFuture killFut = createAndRunConcurrentAction(stop, cmp);
try {
long stopTime = duration + U.currentTimeMillis();
long nextAssert = U.currentTimeMillis() + ASSERT_FREQ;
while (U.currentTimeMillis() < stopTime) {
long start = System.nanoTime();
long ops = cntr.longValue();
U.sleep(1000);
long diff = cntr.longValue() - ops;
double time = (System.nanoTime() - start) / 1_000_000_000d;
long opsPerSecond = (long) (diff / time);
log.info("Operations/second: " + opsPerSecond);
if (U.currentTimeMillis() >= nextAssert) {
CyclicBarrier barrier = new CyclicBarrier(3, new Runnable() {
@Override
public void run() {
try {
cmp.set(null);
log.info("Checking cache content.");
assertCacheContent(expVals);
log.info("Finished check cache content.");
} catch (Throwable e) {
log.error("Unexpected error: " + e, e);
throw e;
}
}
});
log.info("Start cache content check.");
cmp.set(barrier);
try {
barrier.await(60_000, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
U.dumpThreads(log);
fail("Failed to check cache content: " + e);
}
log.info("Cache content check done.");
nextAssert = System.currentTimeMillis() + ASSERT_FREQ;
}
}
} finally {
stop.set(true);
}
killFut.get();
updateFut.get();
log.info("Test finished. Update errors: " + errCntr.get());
}
use of java.util.concurrent.ThreadLocalRandom in project ignite by apache.
the class CachePutIfAbsentTest method testTxConflictGetAndPutIfAbsent.
/**
* @throws Exception If failed.
*/
public void testTxConflictGetAndPutIfAbsent() throws Exception {
Ignite ignite0 = ignite(0);
final IgniteTransactions txs = ignite0.transactions();
for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
try {
IgniteCache<Integer, Integer> cache = ignite0.createCache(ccfg);
ThreadLocalRandom rnd = ThreadLocalRandom.current();
for (int i = 0; i < 10; i++) {
Integer key = rnd.nextInt(10_000);
cache.put(key, 2);
for (TransactionConcurrency concurrency : TransactionConcurrency.values()) {
for (TransactionIsolation isolation : TransactionIsolation.values()) {
try (Transaction tx = txs.txStart(concurrency, isolation)) {
Object old = cache.getAndPutIfAbsent(key, 3);
assertEquals(2, old);
Object val = cache.get(key);
assertEquals(2, val);
tx.commit();
}
assertEquals((Integer) 2, cache.get(key));
}
}
}
} finally {
ignite0.destroyCache(ccfg.getName());
}
}
}
use of java.util.concurrent.ThreadLocalRandom in project ignite by apache.
the class CacheSerializableTransactionsTest method testRandomOperations.
/**
* @throws Exception If failed.
*/
public void testRandomOperations() throws Exception {
Ignite ignite0 = ignite(0);
long stopTime = U.currentTimeMillis() + getTestTimeout() - 30_000;
for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
logCacheInfo(ccfg);
try {
IgniteCache<Integer, Integer> cache0 = ignite0.createCache(ccfg);
ThreadLocalRandom rnd = ThreadLocalRandom.current();
for (Ignite ignite : G.allGrids()) {
log.info("Test node: " + ignite.name());
IgniteCache<Integer, Integer> cache = ignite.cache(ccfg.getName());
IgniteTransactions txs = ignite.transactions();
final int KEYS = 100;
for (int i = 0; i < 1000; i++) {
Integer key1 = rnd.nextInt(KEYS);
Integer key2;
if (rnd.nextBoolean()) {
key2 = rnd.nextInt(KEYS);
while (key2.equals(key1)) key2 = rnd.nextInt(KEYS);
} else
key2 = key1 + 1;
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
randomOperation(rnd, cache, key1);
randomOperation(rnd, cache, key2);
tx.commit();
}
if (i % 100 == 0 && U.currentTimeMillis() > stopTime)
break;
}
for (int key = 0; key < KEYS; key++) {
Integer val = cache0.get(key);
for (int node = 1; node < SRVS + CLIENTS; node++) assertEquals(val, ignite(node).cache(cache.getName()).get(key));
}
if (U.currentTimeMillis() > stopTime)
break;
}
} finally {
destroyCache(ccfg.getName());
}
}
}
use of java.util.concurrent.ThreadLocalRandom in project ignite by apache.
the class IgnitePdsCheckpointSimulationWithRealCpDisabledTest method writePageData.
/**
* @throws Exception if failed.
*/
private void writePageData(FullPageId fullId, PageMemory mem) throws Exception {
long page = mem.acquirePage(fullId.groupId(), fullId.pageId());
try {
long pageAddr = mem.writeLock(fullId.groupId(), fullId.pageId(), page);
try {
DataPageIO.VERSIONS.latest().initNewPage(pageAddr, fullId.pageId(), mem.pageSize());
ThreadLocalRandom rnd = ThreadLocalRandom.current();
for (int i = PageIO.COMMON_HEADER_END; i < mem.pageSize(); i++) PageUtils.putByte(pageAddr, i, (byte) rnd.nextInt(255));
} finally {
mem.writeUnlock(fullId.groupId(), fullId.pageId(), page, null, true);
}
} finally {
mem.releasePage(fullId.groupId(), fullId.pageId(), page);
}
}
use of java.util.concurrent.ThreadLocalRandom in project ignite by apache.
the class CacheContinuousQueryAsyncFilterListenerTest method testNonDeadLockInFilter.
/**
* @param ccfg Cache configuration.
* @param asyncFilter Async filter.
* @param asyncLsnr Async listener.
* @param jcacheApi Use JCache api for start update listener.
* @throws Exception If failed.
*/
private void testNonDeadLockInFilter(CacheConfiguration ccfg, final boolean asyncFilter, final boolean asyncLsnr, boolean jcacheApi) throws Exception {
ignite(0).createCache(ccfg);
ThreadLocalRandom rnd = ThreadLocalRandom.current();
try {
for (int i = 0; i < ITERATION_CNT; i++) {
log.info("Start iteration: " + i);
int nodeIdx = i % NODES;
final String cacheName = ccfg.getName();
final IgniteCache cache = grid(nodeIdx).cache(cacheName);
final QueryTestKey key = NODES - 1 != nodeIdx ? affinityKey(cache) : new QueryTestKey(1);
final QueryTestValue val0 = new QueryTestValue(1);
final QueryTestValue newVal = new QueryTestValue(2);
final CountDownLatch latch = new CountDownLatch(1);
final CountDownLatch evtFromLsnrLatch = new CountDownLatch(1);
IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> fltrClsr = new IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>>() {
@Override
public void apply(Ignite ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue> e) {
if (asyncFilter) {
assertFalse("Failed: " + Thread.currentThread().getName(), Thread.currentThread().getName().contains("sys-"));
assertTrue("Failed: " + Thread.currentThread().getName(), Thread.currentThread().getName().contains("callback-"));
}
IgniteCache<Object, Object> cache0 = ignite.cache(cacheName);
QueryTestValue val = e.getValue();
if (val == null)
return;
else if (val.equals(newVal)) {
evtFromLsnrLatch.countDown();
return;
} else if (!val.equals(val0))
return;
Transaction tx = null;
try {
if (cache0.getConfiguration(CacheConfiguration.class).getAtomicityMode() == TRANSACTIONAL)
tx = ignite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ);
assertEquals(val, val0);
cache0.put(key, newVal);
if (tx != null)
tx.commit();
latch.countDown();
} catch (Exception exp) {
log.error("Failed: ", exp);
throw new IgniteException(exp);
} finally {
if (tx != null)
tx.close();
}
}
};
IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> lsnrClsr = new IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>>() {
@Override
public void apply(Ignite ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue> e) {
if (asyncLsnr) {
assertFalse("Failed: " + Thread.currentThread().getName(), Thread.currentThread().getName().contains("sys-"));
assertTrue("Failed: " + Thread.currentThread().getName(), Thread.currentThread().getName().contains("callback-"));
}
QueryTestValue val = e.getValue();
if (val == null || !val.equals(new QueryTestValue(1)))
return;
assertEquals(val, val0);
latch.countDown();
}
};
QueryCursor qry = null;
MutableCacheEntryListenerConfiguration<QueryTestKey, QueryTestValue> lsnrCfg = null;
CacheInvokeListener locLsnr = asyncLsnr ? new CacheInvokeListenerAsync(lsnrClsr) : new CacheInvokeListener(lsnrClsr);
CacheEntryEventSerializableFilter<QueryTestKey, QueryTestValue> rmtFltr = asyncFilter ? new CacheTestRemoteFilterAsync(fltrClsr) : new CacheTestRemoteFilter(fltrClsr);
if (jcacheApi) {
lsnrCfg = new MutableCacheEntryListenerConfiguration<>(FactoryBuilder.factoryOf(locLsnr), FactoryBuilder.factoryOf(rmtFltr), true, false);
cache.registerCacheEntryListener(lsnrCfg);
} else {
ContinuousQuery<QueryTestKey, QueryTestValue> conQry = new ContinuousQuery<>();
conQry.setLocalListener(locLsnr);
conQry.setRemoteFilterFactory(FactoryBuilder.factoryOf(rmtFltr));
qry = cache.query(conQry);
}
try {
if (rnd.nextBoolean())
cache.put(key, val0);
else
cache.invoke(key, new CacheEntryProcessor() {
@Override
public Object process(MutableEntry entry, Object... arguments) throws EntryProcessorException {
entry.setValue(val0);
return null;
}
});
assert U.await(latch, 3, SECONDS) : "Failed to waiting event.";
assertEquals(cache.get(key), new QueryTestValue(2));
assertTrue("Failed to waiting event from filter.", U.await(latch, 3, SECONDS));
} finally {
if (qry != null)
qry.close();
if (lsnrCfg != null)
cache.deregisterCacheEntryListener(lsnrCfg);
}
log.info("Iteration finished: " + i);
}
} finally {
ignite(0).destroyCache(ccfg.getName());
}
}
Aggregations