use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.
the class IgniteClientReconnectApiExceptionTest method igniteOperationsTest.
/**
* @throws Exception If failed.
*/
@SuppressWarnings("unchecked")
public void igniteOperationsTest() throws Exception {
clientMode = true;
final Ignite client = startGrid(serverCount());
final IgniteCache<Object, Object> dfltCache = client.cache(DEFAULT_CACHE_NAME);
final CountDownLatch recvLatch = new CountDownLatch(1);
assertNotNull(dfltCache);
doTestIgniteOperationOnDisconnect(client, Arrays.asList(// Check compute.
new T2<Callable, C1<Object, Boolean>>(new Callable() {
@Override
public Object call() throws Exception {
boolean failed = false;
try {
client.compute();
} catch (IgniteClientDisconnectedException e) {
failed = true;
checkAndWait(e);
}
assertTrue(failed);
return client.compute();
}
}, new C1<Object, Boolean>() {
@Override
public Boolean apply(Object o) {
IgniteCompute comp = (IgniteCompute) o;
Collection<UUID> uuids = comp.broadcast(new IgniteCallable<UUID>() {
@IgniteInstanceResource
private Ignite ignite;
@Override
public UUID call() throws Exception {
return ignite.cluster().localNode().id();
}
});
assertFalse(uuids.isEmpty());
for (UUID uuid : uuids) assertNotNull(uuid);
return true;
}
}), // Check ping node.
new T2<Callable, C1<Object, Boolean>>(new Callable() {
@Override
public Object call() throws Exception {
boolean failed = false;
try {
client.cluster().pingNode(new UUID(0, 0));
} catch (IgniteClientDisconnectedException e) {
failed = true;
checkAndWait(e);
}
assertTrue(failed);
return client.cluster().pingNode(new UUID(0, 0));
}
}, new C1<Object, Boolean>() {
@Override
public Boolean apply(Object o) {
Boolean pingNode = (Boolean) o;
assertFalse(pingNode);
return true;
}
}), // Check register remote listener.
new T2<Callable, C1<Object, Boolean>>(new Callable() {
@Override
public Object call() throws Exception {
boolean failed = false;
try {
client.events().remoteListen(null, new IgnitePredicate<Event>() {
@Override
public boolean apply(Event event) {
return true;
}
});
} catch (IgniteClientDisconnectedException e) {
failed = true;
checkAndWait(e);
}
assertTrue(failed);
return client.events().remoteListen(null, new IgnitePredicate<Event>() {
@Override
public boolean apply(Event event) {
return true;
}
});
}
}, new C1<Object, Boolean>() {
@Override
public Boolean apply(Object o) {
UUID remoteId = (UUID) o;
assertNotNull(remoteId);
client.events().stopRemoteListen(remoteId);
return true;
}
}), // Check message operation.
new T2<Callable, C1<Object, Boolean>>(new Callable() {
@Override
public Object call() throws Exception {
boolean failed = false;
try {
client.message().remoteListen(null, new IgniteBiPredicate<UUID, Object>() {
@Override
public boolean apply(UUID uuid, Object o) {
if (o.equals("Test message."))
recvLatch.countDown();
return true;
}
});
} catch (IgniteClientDisconnectedException e) {
failed = true;
checkAndWait(e);
}
assertTrue(failed);
return client.message().remoteListen(null, new IgniteBiPredicate<UUID, Object>() {
@Override
public boolean apply(UUID uuid, Object o) {
if (o.equals("Test message."))
recvLatch.countDown();
return true;
}
});
}
}, new C1<Object, Boolean>() {
@Override
public Boolean apply(Object o) {
assertNotNull(o);
IgniteMessaging msg = client.message();
msg.send(null, "Test message.");
try {
assertTrue(recvLatch.await(2, SECONDS));
} catch (InterruptedException ignored) {
fail("Message wasn't received.");
}
return true;
}
}), // Check executor.
new T2<Callable, C1<Object, Boolean>>(new Callable() {
@Override
public Object call() throws Exception {
boolean failed = false;
try {
client.executorService().submit(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
return 42;
}
});
} catch (IgniteClientDisconnectedException e) {
failed = true;
checkAndWait(e);
}
assertTrue(failed);
return client.executorService().submit(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
return 42;
}
});
}
}, new C1<Object, Boolean>() {
@Override
public Boolean apply(Object o) {
assertNotNull(o);
Future<Integer> fut = (Future<Integer>) o;
try {
assertEquals(42, (int) fut.get());
} catch (Exception ignored) {
fail("Failed submit task.");
}
return true;
}
})));
clientMode = false;
}
use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.
the class IgniteClientReconnectContinuousProcessorTest method continuousQueryReconnect.
/**
* @param client Client.
* @param clientCache Client cache.
* @param lsnr Continuous query listener.
* @throws Exception If failed.
*/
private void continuousQueryReconnect(Ignite client, IgniteCache<Object, Object> clientCache, CacheEventListener lsnr) throws Exception {
Ignite srv = clientRouter(client);
TestTcpDiscoverySpi srvSpi = spi(srv);
final CountDownLatch reconnectLatch = new CountDownLatch(1);
IgnitePredicate<Event> p = new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
if (evt.type() == EVT_CLIENT_NODE_RECONNECTED) {
info("Reconnected: " + evt);
reconnectLatch.countDown();
}
return true;
}
};
client.events().localListen(p, EVT_CLIENT_NODE_RECONNECTED);
srvSpi.failNode(client.cluster().localNode().id(), null);
waitReconnectEvent(reconnectLatch);
client.events().stopLocalListen(p);
lsnr.latch = new CountDownLatch(1);
clientCache.put(1, 1);
assertTrue(lsnr.latch.await(5000, MILLISECONDS));
lsnr.latch = new CountDownLatch(1);
srv.cache(DEFAULT_CACHE_NAME).put(2, 2);
assertTrue(lsnr.latch.await(5000, MILLISECONDS));
}
use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.
the class GridDiscoverySelfTest method testDiscoveryListener.
/**
* @throws Exception If failed.
*/
public void testDiscoveryListener() throws Exception {
ClusterNode node = ignite.cluster().localNode();
assert node != null;
final AtomicInteger cnt = new AtomicInteger();
/**
* Joined nodes counter.
*/
final CountDownLatch joinedCnt = new CountDownLatch(NODES_CNT);
/**
* Left nodes counter.
*/
final CountDownLatch leftCnt = new CountDownLatch(NODES_CNT);
IgnitePredicate<Event> lsnr = new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
if (EVT_NODE_JOINED == evt.type()) {
cnt.incrementAndGet();
joinedCnt.countDown();
} else if (EVT_NODE_LEFT == evt.type()) {
int i = cnt.decrementAndGet();
assert i >= 0;
leftCnt.countDown();
} else
assert false;
return true;
}
};
ignite.events().localListen(lsnr, EVT_NODE_LEFT, EVT_NODE_JOINED);
try {
for (int i = 0; i < NODES_CNT; i++) startGrid(i);
joinedCnt.await(MAX_TIMEOUT_IN_MINS, MINUTES);
assert cnt.get() == NODES_CNT;
for (int i = 0; i < NODES_CNT; i++) stopGrid(i);
leftCnt.await(MAX_TIMEOUT_IN_MINS, MINUTES);
assert cnt.get() == 0;
ignite.events().stopLocalListen(lsnr);
assert cnt.get() == 0;
} finally {
for (int i = 0; i < NODES_CNT; i++) stopAndCancelGrid(i);
}
}
use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.
the class IgniteCacheConfigVariationsFullApiTest method checkGetOutTx.
/**
* @param async Use async API.
* @param oldAsync Uase old style async API.
* @throws Exception If failed.
*/
private void checkGetOutTx(boolean async, boolean oldAsync) throws Exception {
final AtomicInteger lockEvtCnt = new AtomicInteger();
IgnitePredicate<Event> lsnr = new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
lockEvtCnt.incrementAndGet();
return true;
}
};
try {
IgniteCache<String, Integer> cache = grid(0).cache(cacheName());
List<String> keys = primaryKeysForCache(0, 2, 1);
assertEquals(2, keys.size());
cache.put(keys.get(0), 0);
cache.put(keys.get(1), 1);
grid(0).events().localListen(lsnr, EVT_CACHE_OBJECT_LOCKED, EVT_CACHE_OBJECT_UNLOCKED);
if (async && oldAsync)
cache = cache.withAsync();
try (Transaction tx = transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
Integer val0;
if (async) {
if (oldAsync) {
cache.get(keys.get(0));
val0 = cache.<Integer>future().get();
} else
val0 = cache.getAsync(keys.get(0)).get();
} else
val0 = cache.get(keys.get(0));
assertEquals(0, val0.intValue());
Map<String, Integer> allOutTx;
if (async) {
if (oldAsync) {
cache.getAllOutTx(F.asSet(keys.get(1)));
allOutTx = cache.<Map<String, Integer>>future().get();
} else
allOutTx = cache.getAllOutTxAsync(F.asSet(keys.get(1))).get();
} else
allOutTx = cache.getAllOutTx(F.asSet(keys.get(1)));
assertEquals(1, allOutTx.size());
assertTrue(allOutTx.containsKey(keys.get(1)));
assertEquals(1, allOutTx.get(keys.get(1)).intValue());
}
assertTrue(GridTestUtils.waitForCondition(new PA() {
@Override
public boolean apply() {
info("Lock event count: " + lockEvtCnt.get());
if (atomicityMode() == ATOMIC)
return lockEvtCnt.get() == 0;
if (cacheMode() == PARTITIONED && nearEnabled()) {
if (!grid(0).configuration().isClientMode())
return lockEvtCnt.get() == 4;
}
return lockEvtCnt.get() == 2;
}
}, 15000));
} finally {
grid(0).events().stopLocalListen(lsnr, EVT_CACHE_OBJECT_LOCKED, EVT_CACHE_OBJECT_UNLOCKED);
}
}
use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.
the class IgniteDynamicCacheStartSelfTest method testEvents.
/**
* @throws Exception If failed.
*/
public void testEvents() throws Exception {
CacheConfiguration cfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
cfg.setName(DYNAMIC_CACHE_NAME);
cfg.setCacheMode(CacheMode.REPLICATED);
// This cache will not fire any cache events.
CacheConfiguration cfgEvtsDisabled = new CacheConfiguration(cfg);
cfgEvtsDisabled.setName("DynamicCacheEvtsDisabled");
cfgEvtsDisabled.setEventsDisabled(true);
final CountDownLatch[] starts = new CountDownLatch[nodeCount()];
final CountDownLatch[] stops = new CountDownLatch[nodeCount()];
IgnitePredicate[] lsnrs = new IgnitePredicate[nodeCount()];
for (int i = 0; i < nodeCount(); i++) {
final int idx = i;
starts[i] = new CountDownLatch(1);
stops[i] = new CountDownLatch(1);
lsnrs[i] = new IgnitePredicate<CacheEvent>() {
@Override
public boolean apply(CacheEvent e) {
switch(e.type()) {
case EventType.EVT_CACHE_STARTED:
starts[idx].countDown();
break;
case EventType.EVT_CACHE_STOPPED:
stops[idx].countDown();
break;
default:
assert false;
}
assertEquals(DYNAMIC_CACHE_NAME, e.cacheName());
return true;
}
};
ignite(i).events().localListen(lsnrs[i], EventType.EVTS_CACHE_LIFECYCLE);
}
IgniteCache<Object, Object> cacheEvtsDisabled = ignite(0).createCache(cfgEvtsDisabled);
IgniteCache<Object, Object> cache = ignite(0).createCache(cfg);
try {
for (CountDownLatch start : starts) start.await();
} finally {
cacheEvtsDisabled.destroy();
cache.destroy();
}
for (CountDownLatch stop : stops) stop.await();
for (int i = 0; i < nodeCount(); i++) ignite(i).events().stopLocalListen(lsnrs[i]);
}
Aggregations