use of org.apache.ignite.internal.util.lang.GridAbsPredicateX in project ignite by apache.
the class GridCacheAbstractSelfTest method afterTest.
/** {@inheritDoc} */
@Override
protected void afterTest() throws Exception {
Transaction tx = jcache().unwrap(Ignite.class).transactions().tx();
if (tx != null) {
tx.close();
fail("Cache transaction remained after test completion: " + tx);
}
for (int i = 0; i < gridCount(); i++) {
info("Checking grid: " + i);
while (true) {
try {
final int fi = i;
assertTrue("Cache is not empty: " + " localSize = " + jcache(fi).localSize(CachePeekMode.ALL) + ", local entries " + entrySet(jcache(fi).localEntries()), GridTestUtils.waitForCondition(// Preloading may happen as nodes leave, so we need to wait.
new GridAbsPredicateX() {
@Override
public boolean applyx() throws IgniteCheckedException {
jcache(fi).removeAll();
if (jcache(fi).size(CachePeekMode.ALL) > 0) {
for (Cache.Entry<String, ?> k : jcache(fi).localEntries()) jcache(fi).remove(k.getKey());
}
return jcache(fi).localSize(CachePeekMode.ALL) == 0;
}
}, getTestTimeout()));
int primaryKeySize = jcache(i).localSize(CachePeekMode.PRIMARY);
int keySize = jcache(i).localSize();
int size = jcache(i).localSize();
int globalSize = jcache(i).size();
int globalPrimarySize = jcache(i).size(CachePeekMode.PRIMARY);
info("Size after [idx=" + i + ", size=" + size + ", keySize=" + keySize + ", primarySize=" + primaryKeySize + ", globalSize=" + globalSize + ", globalPrimarySize=" + globalPrimarySize + ", entrySet=" + jcache(i).localEntries() + ']');
assertEquals("Cache is not empty [idx=" + i + ", entrySet=" + jcache(i).localEntries() + ']', 0, jcache(i).localSize(CachePeekMode.ALL));
break;
} catch (Exception e) {
if (X.hasCause(e, ClusterTopologyCheckedException.class)) {
info("Got topology exception while tear down (will retry in 1000ms).");
U.sleep(1000);
} else
throw e;
}
}
}
assert jcache().unwrap(Ignite.class).transactions().tx() == null;
assertEquals("Cache is not empty", 0, jcache().localSize(CachePeekMode.ALL));
if (storeStgy != null)
storeStgy.resetStore();
}
use of org.apache.ignite.internal.util.lang.GridAbsPredicateX in project ignite by apache.
the class IgniteCacheConfigVariationsFullApiTest method testContinuousQuery.
/**
* @throws Exception If failed.
*/
public void testContinuousQuery() throws Exception {
runInAllDataModes(new TestRunnable() {
@Override
public void run() throws Exception {
final AtomicInteger updCnt = new AtomicInteger();
ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
qry.setInitialQuery(new ScanQuery<>(new IgniteBiPredicate<Object, Object>() {
@Override
public boolean apply(Object key, Object val) {
return valueOf(key) >= 3;
}
}));
qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<? extends Object, ? extends Object>> evts) throws CacheEntryListenerException {
for (CacheEntryEvent<? extends Object, ? extends Object> evt : evts) {
int v = valueOf(evt.getKey());
// Check filter.
assertTrue("v=" + v, v >= 10 && v < 15);
updCnt.incrementAndGet();
}
}
});
qry.setRemoteFilter(new TestCacheEntryEventSerializableFilter());
IgniteCache<Object, Object> cache = jcache();
for (int i = 0; i < 10; i++) cache.put(key(i), value(i));
try (QueryCursor<Cache.Entry<Object, Object>> cur = cache.query(qry)) {
int cnt = 0;
for (Cache.Entry<Object, Object> e : cur) {
cnt++;
int val = valueOf(e.getKey());
assertTrue("v=" + val, val >= 3);
}
assertEquals(7, cnt);
for (int i = 10; i < 20; i++) cache.put(key(i), value(i));
GridTestUtils.waitForCondition(new GridAbsPredicateX() {
@Override
public boolean applyx() throws IgniteCheckedException {
return updCnt.get() == 5;
}
}, 30_000);
}
}
});
}
use of org.apache.ignite.internal.util.lang.GridAbsPredicateX in project ignite by apache.
the class IgniteCommunicationBalanceTest method waitNioBalanceStop.
/**
* @param nodes Node.
* @param timeout Timeout.
* @throws Exception If failed.
*/
private void waitNioBalanceStop(List<Ignite> nodes, long timeout) throws Exception {
final List<GridNioServer> srvs = new ArrayList<>();
for (Ignite node : nodes) {
TcpCommunicationSpi spi = (TcpCommunicationSpi) node.configuration().getCommunicationSpi();
GridNioServer srv = GridTestUtils.getFieldValue(spi, "nioSrvr");
srvs.add(srv);
}
assertTrue(GridTestUtils.waitForCondition(new GridAbsPredicateX() {
@Override
public boolean applyx() throws IgniteCheckedException {
List<Long> rCnts = new ArrayList<>();
List<Long> wCnts = new ArrayList<>();
for (GridNioServer srv : srvs) {
long readerMovCnt1 = srv.readerMoveCount();
long writerMovCnt1 = srv.writerMoveCount();
rCnts.add(readerMovCnt1);
wCnts.add(writerMovCnt1);
}
U.sleep(2000);
for (int i = 0; i < srvs.size(); i++) {
GridNioServer srv = srvs.get(i);
long readerMovCnt1 = rCnts.get(i);
long writerMovCnt1 = wCnts.get(i);
long readerMovCnt2 = srv.readerMoveCount();
long writerMovCnt2 = srv.writerMoveCount();
if (readerMovCnt1 != readerMovCnt2) {
log.info("Readers balance is in progress [node=" + i + ", cnt1=" + readerMovCnt1 + ", cnt2=" + readerMovCnt2 + ']');
return false;
}
if (writerMovCnt1 != writerMovCnt2) {
log.info("Writers balance is in progress [node=" + i + ", cnt1=" + writerMovCnt1 + ", cnt2=" + writerMovCnt2 + ']');
return false;
}
}
return true;
}
}, timeout));
}
use of org.apache.ignite.internal.util.lang.GridAbsPredicateX in project ignite by apache.
the class GridServiceProcessorMultiNodeConfigSelfTest method beforeTestsStarted.
/** {@inheritDoc} */
@Override
protected void beforeTestsStarted() throws Exception {
super.beforeTestsStarted();
GridTestUtils.waitForCondition(new GridAbsPredicateX() {
@Override
public boolean applyx() {
return DummyService.started(CLUSTER_SINGLE) == 1 && DummyService.cancelled(CLUSTER_SINGLE) == 0 && DummyService.started(NODE_SINGLE) == nodeCount() && DummyService.cancelled(NODE_SINGLE) == 0 && DummyService.started(NODE_SINGLE_BUT_CLIENT) == nodeCount() && DummyService.cancelled(NODE_SINGLE_BUT_CLIENT) == 0 && DummyService.started(NODE_SINGLE_WITH_LIMIT) >= nodeCount() && DummyService.cancelled(NODE_SINGLE_WITH_LIMIT) == 0 && actualCount(AFFINITY, randomGrid().services().serviceDescriptors()) == 1;
}
}, 2000);
}
use of org.apache.ignite.internal.util.lang.GridAbsPredicateX in project ignite by apache.
the class GridCacheAbstractFullApiSelfTest method checkTtl.
/**
* @param inTx In tx flag.
* @param oldEntry {@code True} to check TTL on old entry, {@code false} on new.
* @throws Exception If failed.
*/
private void checkTtl(boolean inTx, boolean oldEntry) throws Exception {
// TODO GG-11133.
if (true)
return;
int ttl = 1000;
final ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
final IgniteCache<String, Integer> c = jcache();
final String key = primaryKeysForCache(jcache(), 1).get(0);
IgnitePair<Long> entryTtl;
if (oldEntry) {
c.put(key, 1);
entryTtl = entryTtl(fullCache(), key);
assertNotNull(entryTtl.get1());
assertNotNull(entryTtl.get2());
assertEquals((Long) 0L, entryTtl.get1());
assertEquals((Long) 0L, entryTtl.get2());
}
long startTime = System.currentTimeMillis();
if (inTx) {
// Rollback transaction for the first time.
Transaction tx = transactions().txStart();
try {
jcache().withExpiryPolicy(expiry).put(key, 1);
} finally {
tx.rollback();
}
if (oldEntry) {
entryTtl = entryTtl(fullCache(), key);
assertEquals((Long) 0L, entryTtl.get1());
assertEquals((Long) 0L, entryTtl.get2());
}
}
// Now commit transaction and check that ttl and expire time have been saved.
Transaction tx = inTx ? transactions().txStart() : null;
try {
jcache().withExpiryPolicy(expiry).put(key, 1);
if (tx != null)
tx.commit();
} finally {
if (tx != null)
tx.close();
}
long[] expireTimes = new long[gridCount()];
for (int i = 0; i < gridCount(); i++) {
if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key)) {
IgnitePair<Long> curEntryTtl = entryTtl(jcache(i), key);
assertNotNull(curEntryTtl.get1());
assertNotNull(curEntryTtl.get2());
assertEquals(ttl, (long) curEntryTtl.get1());
assertTrue(curEntryTtl.get2() > startTime);
expireTimes[i] = curEntryTtl.get2();
}
}
// One more update from the same cache entry to ensure that expire time is shifted forward.
U.sleep(100);
tx = inTx ? transactions().txStart() : null;
try {
jcache().withExpiryPolicy(expiry).put(key, 2);
if (tx != null)
tx.commit();
} finally {
if (tx != null)
tx.close();
}
for (int i = 0; i < gridCount(); i++) {
if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key)) {
IgnitePair<Long> curEntryTtl = entryTtl(jcache(i), key);
assertNotNull(curEntryTtl.get1());
assertNotNull(curEntryTtl.get2());
assertEquals(ttl, (long) curEntryTtl.get1());
assertTrue(curEntryTtl.get2() > startTime);
expireTimes[i] = curEntryTtl.get2();
}
}
// And one more direct update to ensure that expire time is shifted forward.
U.sleep(100);
tx = inTx ? transactions().txStart() : null;
try {
jcache().withExpiryPolicy(expiry).put(key, 3);
if (tx != null)
tx.commit();
} finally {
if (tx != null)
tx.close();
}
for (int i = 0; i < gridCount(); i++) {
if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key)) {
IgnitePair<Long> curEntryTtl = entryTtl(jcache(i), key);
assertNotNull(curEntryTtl.get1());
assertNotNull(curEntryTtl.get2());
assertEquals(ttl, (long) curEntryTtl.get1());
assertTrue(curEntryTtl.get2() > startTime);
expireTimes[i] = curEntryTtl.get2();
}
}
// And one more update to ensure that ttl is not changed and expire time is not shifted forward.
U.sleep(100);
log.info("Put 4");
tx = inTx ? transactions().txStart() : null;
try {
jcache().put(key, 4);
if (tx != null)
tx.commit();
} finally {
if (tx != null)
tx.close();
}
log.info("Put 4 done");
for (int i = 0; i < gridCount(); i++) {
if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key)) {
IgnitePair<Long> curEntryTtl = entryTtl(jcache(i), key);
assertNotNull(curEntryTtl.get1());
assertNotNull(curEntryTtl.get2());
assertEquals(ttl, (long) curEntryTtl.get1());
assertEquals(expireTimes[i], (long) curEntryTtl.get2());
}
}
// Avoid reloading from store.
storeStgy.removeFromStore(key);
assertTrue(GridTestUtils.waitForCondition(new GridAbsPredicateX() {
@SuppressWarnings("unchecked")
@Override
public boolean applyx() {
try {
Integer val = c.get(key);
if (val != null) {
info("Value is in cache [key=" + key + ", val=" + val + ']');
return false;
}
// Get "cache" field from GridCacheProxyImpl.
GridCacheAdapter c0 = cacheFromCtx(c);
if (!c0.context().deferredDelete()) {
GridCacheEntryEx e0 = c0.peekEx(key);
return e0 == null || (e0.rawGet() == null && e0.valueBytes() == null);
} else
return true;
} catch (GridCacheEntryRemovedException e) {
throw new RuntimeException(e);
}
}
}, Math.min(ttl * 10, getTestTimeout())));
IgniteCache fullCache = fullCache();
if (!isMultiJvmObject(fullCache)) {
GridCacheAdapter internalCache = internalCache(fullCache);
if (internalCache.isLocal())
return;
}
assert c.get(key) == null;
// Ensure that old TTL and expire time are not longer "visible".
entryTtl = entryTtl(fullCache(), key);
assertNotNull(entryTtl.get1());
assertNotNull(entryTtl.get2());
assertEquals(0, (long) entryTtl.get1());
assertEquals(0, (long) entryTtl.get2());
// Ensure that next update will not pick old expire time.
tx = inTx ? transactions().txStart() : null;
try {
jcache().put(key, 10);
if (tx != null)
tx.commit();
} finally {
if (tx != null)
tx.close();
}
U.sleep(2000);
entryTtl = entryTtl(fullCache(), key);
assertEquals((Integer) 10, c.get(key));
assertNotNull(entryTtl.get1());
assertNotNull(entryTtl.get2());
assertEquals(0, (long) entryTtl.get1());
assertEquals(0, (long) entryTtl.get2());
}
Aggregations