use of org.apache.ignite.internal.util.lang.GridAbsPredicate in project ignite by apache.
the class GridSelfTest method testAsyncListen.
/**
* @throws Exception If failed.
*/
@Test
public void testAsyncListen() throws Exception {
final String hello = "HELLO!";
final String bye = "BYE!";
final Ignite g = grid(0);
final UUID locNodeId = g.cluster().localNode().id();
g.message().remoteListen(null, new MessagingListenActor<String>() {
@Override
protected void receive(UUID nodeId, String rcvMsg) throws Throwable {
if (hello.equals(rcvMsg)) {
assertEquals(locNodeId, nodeId);
assertEquals(hello, rcvMsg);
stop(bye);
}
}
});
final AtomicInteger cnt = new AtomicInteger();
g.message().localListen(null, new P2<UUID, String>() {
@Override
public boolean apply(UUID nodeId, String msg) {
if (msg.equals(bye))
cnt.incrementAndGet();
return true;
}
});
g.message().send(null, hello);
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
return cnt.get() == g.cluster().nodes().size();
}
}, 5000);
assertEquals(cnt.get(), g.cluster().nodes().size());
}
use of org.apache.ignite.internal.util.lang.GridAbsPredicate in project ignite by apache.
the class GridProjectionForCachesSelfTest method testProjectionForNamedCache.
/**
* @throws Exception If failed.
*/
@Test
public void testProjectionForNamedCache() throws Exception {
final ClusterGroup prj = ignite.cluster().forCacheNodes(CACHE_NAME);
assertNotNull(prj);
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
return prj.nodes().size() == 3;
}
}, 5000);
assertEquals("Invalid projection: " + prj.nodes(), 3, prj.nodes().size());
assert !prj.nodes().contains(grid(0).localNode());
assert prj.nodes().contains(grid(1).localNode());
assert prj.nodes().contains(grid(2).localNode());
assert prj.nodes().contains(grid(3).localNode());
assert !prj.nodes().contains(grid(4).localNode());
}
use of org.apache.ignite.internal.util.lang.GridAbsPredicate in project ignite by apache.
the class GridCacheAbstractFullApiSelfTest method testPeekExpiredTx.
/**
* @throws Exception If failed.
*/
@Test
public void testPeekExpiredTx() throws Exception {
if (txShouldBeUsed()) {
final IgniteCache<String, Integer> c = jcache();
final String key = "1";
int ttl = 500;
try (Transaction tx = grid(0).transactions().txStart()) {
final ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
grid(0).cache(DEFAULT_CACHE_NAME).withExpiryPolicy(expiry).put(key, 1);
tx.commit();
}
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
return peek(c, key) == null;
}
}, 2000);
assertNull(peek(c, key));
assert c.localSize() == 0;
}
}
use of org.apache.ignite.internal.util.lang.GridAbsPredicate in project ignite by apache.
the class IgniteClientCacheStartFailoverTest method testRebalanceStateConcurrentStart.
/**
* @throws Exception If failed.
*/
@Test
public void testRebalanceStateConcurrentStart() throws Exception {
final int SRVS1 = 3;
final int CLIENTS = 5;
final int SRVS2 = 5;
startGrids(SRVS1);
Ignite srv0 = ignite(0);
final int KEYS = 1000;
final List<String> cacheNames = startCaches(srv0, KEYS);
final List<Ignite> clients = new ArrayList<>();
for (int i = 0; i < CLIENTS; i++) clients.add(startClientGrid(SRVS1 + i));
final CyclicBarrier barrier = new CyclicBarrier(clients.size() + SRVS2);
final AtomicInteger clientIdx = new AtomicInteger();
final Set<Integer> keys = new HashSet<>();
for (int i = 0; i < KEYS; i++) keys.add(i);
IgniteInternalFuture<?> fut1 = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
barrier.await();
Ignite client = clients.get(clientIdx.getAndIncrement());
for (String cacheName : cacheNames) client.cache(cacheName);
ThreadLocalRandom rnd = ThreadLocalRandom.current();
for (int i = 0; i < 10; i++) {
for (String cacheName : cacheNames) {
IgniteCache<Object, Object> cache = client.cache(cacheName);
Map<Object, Object> map0 = cache.getAll(keys);
assertEquals("[cache=" + cacheName + ", expected=" + KEYS + ", actual=" + map0.size() + ']', KEYS, map0.size());
int key = rnd.nextInt(KEYS);
try {
cache.put(key, i);
} catch (CacheException e) {
log.error("It couldn't put a value [cache=" + cacheName + ", key=" + key + ", val=" + i + ']', e);
CacheConfiguration ccfg = cache.getConfiguration(CacheConfiguration.class);
TransactionSerializationException txEx = X.cause(e, TransactionSerializationException.class);
boolean notContains = !txEx.getMessage().contains("Cannot serialize transaction due to write conflict (transaction is marked for rollback)");
if (txEx == null || ccfg.getAtomicityMode() != TRANSACTIONAL_SNAPSHOT || notContains)
fail("Assert violated because exception was thrown [e=" + e.getMessage() + ']');
}
}
}
return null;
}
}, clients.size(), "client-cache-start");
final AtomicInteger srvIdx = new AtomicInteger(SRVS1 + CLIENTS);
IgniteInternalFuture<?> fut2 = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
barrier.await();
startGrid(srvIdx.incrementAndGet());
return null;
}
}, SRVS2, "node-start");
fut1.get();
fut2.get();
final AffinityTopologyVersion topVer = new AffinityTopologyVersion(SRVS1 + SRVS2 + CLIENTS, 1);
for (Ignite client : clients) {
for (String cacheName : cacheNames) {
final GridDhtPartitionTopology top = ((IgniteKernal) client).context().cache().internalCache(cacheName).context().topology();
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
return top.rebalanceFinished(topVer);
}
}, 5000);
assertTrue(top.rebalanceFinished(topVer));
}
}
}
use of org.apache.ignite.internal.util.lang.GridAbsPredicate in project ignite by apache.
the class IgniteCacheStoreValueAbstractTest method testValueNotStored.
/**
* @throws Exception If failed.
*/
@Test
public void testValueNotStored() throws Exception {
cpyOnRead = true;
startGrids();
IgniteCache<TestKey, TestValue> cache = grid(0).cache(DEFAULT_CACHE_NAME);
Affinity<Object> aff = grid(0).affinity(DEFAULT_CACHE_NAME);
final List<WeakReference<Object>> refs = new ArrayList<>();
for (int i = 0; i < 100; i++) {
TestKey key = new TestKey(i);
TestValue val = new TestValue(i);
refs.add(new WeakReference<Object>(val));
cache.put(key, val);
checkNoValue(aff, key);
for (int g = 0; g < gridCount(); g++) assertNotNull(grid(g).cache(DEFAULT_CACHE_NAME).get(key));
checkNoValue(aff, key);
cache.invoke(key, new CacheEntryProcessor<TestKey, TestValue, Object>() {
@Override
public Object process(MutableEntry<TestKey, TestValue> entry, Object... args) {
assertNotNull(entry.getValue());
entry.setValue(new TestValue(10_000));
return new TestValue(20_000);
}
});
checkNoValue(aff, key);
for (int g = 0; g < gridCount(); g++) assertNotNull(grid(g).cache(DEFAULT_CACHE_NAME).get(key));
checkNoValue(aff, key);
cache.remove(key);
for (int g = 0; g < gridCount(); g++) assertNull(grid(g).cache(DEFAULT_CACHE_NAME).get(key));
try (IgniteDataStreamer<TestKey, TestValue> streamer = grid(0).dataStreamer(DEFAULT_CACHE_NAME)) {
streamer.addData(key, val);
}
checkNoValue(aff, key);
cache.remove(key);
try (IgniteDataStreamer<TestKey, TestValue> streamer = grid(0).dataStreamer(DEFAULT_CACHE_NAME)) {
streamer.allowOverwrite(true);
streamer.addData(key, val);
}
checkNoValue(aff, key);
if (aff.isPrimaryOrBackup(grid(0).localNode(), key)) {
cache.localEvict(Collections.singleton(key));
assertNull(cache.localPeek(key, CachePeekMode.ONHEAP));
cache.get(key);
assertNotNull(cache.localPeek(key, CachePeekMode.ONHEAP));
checkNoValue(aff, key);
}
}
// Should load TestKey(100_000).
cache.loadCache(null);
TestKey key = new TestKey(100_000);
checkNoValue(aff, key);
for (int g = 0; g < gridCount(); g++) assertNotNull(grid(g).cache(DEFAULT_CACHE_NAME).get(key));
checkNoValue(aff, key);
boolean wait = GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
System.gc();
boolean pass = true;
for (Iterator<WeakReference<Object>> it = refs.iterator(); it.hasNext(); ) {
WeakReference<Object> ref = it.next();
if (ref.get() == null)
it.remove();
else {
pass = false;
log.info("Not collected value: " + ref.get());
}
}
return pass;
}
}, 60_000);
assertTrue("Failed to wait for when values are collected", wait);
}
Aggregations