use of org.apache.ignite.events.DiscoveryEvent in project ignite by apache.
the class GridDiscoveryEventSelfTest method testJoinSequenceEvents.
/**
* @throws Exception If failed.
*/
@Test
public void testJoinSequenceEvents() throws Exception {
try {
Ignite g0 = startGrid(0);
UUID id0 = g0.cluster().localNode().id();
final ConcurrentMap<Integer, Collection<ClusterNode>> evts = new ConcurrentHashMap<>();
final CountDownLatch latch = new CountDownLatch(3);
g0.events().localListen(new IgnitePredicate<Event>() {
private AtomicInteger cnt = new AtomicInteger();
@Override
public boolean apply(Event evt) {
assert evt.type() == EVT_NODE_JOINED : evt;
evts.put(cnt.getAndIncrement(), ((DiscoveryEvent) evt).topologyNodes());
latch.countDown();
return true;
}
}, EVT_NODE_JOINED);
UUID id1 = startGrid(1).cluster().localNode().id();
UUID id2 = startGrid(2).cluster().localNode().id();
UUID id3 = startGrid(3).cluster().localNode().id();
assertTrue("Wrong count of events received: " + evts, latch.await(3000, MILLISECONDS));
Collection<ClusterNode> top0 = evts.get(0);
assertNotNull(top0);
assertEquals(2, top0.size());
assertTrue(F.viewReadOnly(top0, NODE_2ID).contains(id0));
assertTrue(F.viewReadOnly(top0, NODE_2ID).contains(id1));
Collection<ClusterNode> top1 = evts.get(1);
assertNotNull(top1);
assertEquals(3, top1.size());
assertTrue(F.viewReadOnly(top1, NODE_2ID).contains(id0));
assertTrue(F.viewReadOnly(top1, NODE_2ID).contains(id1));
assertTrue(F.viewReadOnly(top1, NODE_2ID).contains(id2));
Collection<ClusterNode> top2 = evts.get(2);
assertNotNull(top2);
assertEquals(4, top2.size());
assertTrue(F.viewReadOnly(top2, NODE_2ID).contains(id0));
assertTrue(F.viewReadOnly(top2, NODE_2ID).contains(id1));
assertTrue(F.viewReadOnly(top2, NODE_2ID).contains(id2));
assertTrue(F.viewReadOnly(top2, NODE_2ID).contains(id3));
} finally {
stopAllGrids();
}
}
use of org.apache.ignite.events.DiscoveryEvent in project ignite by apache.
the class GridSameVmStartupSelfTest method testSameVmStartup.
/**
* JUnit.
*
* @throws Exception If failed.
*/
@Test
public void testSameVmStartup() throws Exception {
Ignite ignite1 = startGrid(1);
Collection<ClusterNode> top1 = ignite1.cluster().forRemotes().nodes();
try {
assert top1.isEmpty() : "Grid1 topology is not empty: " + top1;
// Start another grid.
Ignite ignite2 = startGrid(2);
final CountDownLatch latch = new CountDownLatch(1);
int size1 = ignite1.cluster().forRemotes().nodes().size();
int size2 = ignite2.cluster().forRemotes().nodes().size();
assert size1 == 1 : "Invalid number of remote nodes discovered: " + size1;
assert size2 == 1 : "Invalid number of remote nodes discovered: " + size2;
final UUID grid1LocNodeId = ignite1.cluster().localNode().id();
ignite2.events().localListen(new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
boolean tcpDiscovery = tcpDiscovery();
if (tcpDiscovery)
assert evt.type() != EVT_NODE_FAILED : "Node1 did not exit gracefully.";
if (evt instanceof DiscoveryEvent) {
// Local node can send METRICS_UPDATED event.
assert ((DiscoveryEvent) evt).eventNode().id().equals(grid1LocNodeId) || evt.type() == EVT_NODE_METRICS_UPDATED : "Received event about invalid node [received=" + ((DiscoveryEvent) evt).eventNode().id() + ", expected=" + grid1LocNodeId + ", type=" + evt.type() + ']';
if (tcpDiscovery) {
if (evt.type() == EVT_NODE_LEFT)
latch.countDown();
} else {
if (evt.type() == EVT_NODE_LEFT || evt.type() == EVT_NODE_FAILED)
latch.countDown();
}
}
return true;
}
}, EVTS_DISCOVERY);
stopGrid(1);
assertTrue(latch.await(10, TimeUnit.SECONDS));
Collection<ClusterNode> top2 = ignite2.cluster().forRemotes().nodes();
assert top2.isEmpty() : "Grid2 topology is not empty: " + top2;
} finally {
stopGrid(1);
stopGrid(2);
}
assert G.allGrids().isEmpty();
}
use of org.apache.ignite.events.DiscoveryEvent in project ignite by apache.
the class GridEventConsumeSelfTest method testApiAsync.
/**
* @throws Exception If failed.
*/
@Test
public void testApiAsync() throws Exception {
IgniteEvents evt = grid(0).events();
try {
evt.stopRemoteListenAsync(null).get();
} catch (NullPointerException ignored) {
// No-op.
}
evt.stopRemoteListenAsync(UUID.randomUUID()).get();
UUID consumeId = null;
try {
consumeId = evt.remoteListenAsync(new P2<UUID, DiscoveryEvent>() {
@Override
public boolean apply(UUID uuid, DiscoveryEvent evt) {
return false;
}
}, new P1<DiscoveryEvent>() {
@Override
public boolean apply(DiscoveryEvent e) {
return false;
}
}, EVTS_DISCOVERY).get();
assertNotNull(consumeId);
} finally {
evt.stopRemoteListenAsync(consumeId).get();
}
try {
consumeId = evt.remoteListenAsync(new P2<UUID, DiscoveryEvent>() {
@Override
public boolean apply(UUID uuid, DiscoveryEvent evt) {
return false;
}
}, new P1<DiscoveryEvent>() {
@Override
public boolean apply(DiscoveryEvent e) {
return false;
}
}).get();
assertNotNull(consumeId);
} finally {
evt.stopRemoteListenAsync(consumeId).get();
}
try {
consumeId = evt.remoteListenAsync(new P2<UUID, Event>() {
@Override
public boolean apply(UUID uuid, Event evt) {
return false;
}
}, new P1<Event>() {
@Override
public boolean apply(Event e) {
return false;
}
}).get();
assertNotNull(consumeId);
} finally {
evt.stopRemoteListenAsync(consumeId).get();
}
}
use of org.apache.ignite.events.DiscoveryEvent in project ignite by apache.
the class GridEventConsumeSelfTest method testMasterNodeLeave.
/**
* @throws Exception If failed.
*/
@Test
public void testMasterNodeLeave() throws Exception {
final CountDownLatch latch = new CountDownLatch(GRID_CNT);
Ignite g = startGrid("anotherGridMasterNodeLeave");
try {
final UUID nodeId = g.cluster().localNode().id();
for (int i = 0; i < GRID_CNT; i++) {
grid(i).events().localListen(new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
if (nodeId.equals(((DiscoveryEvent) evt).eventNode().id()))
latch.countDown();
return true;
}
}, EVT_NODE_LEFT, EVT_NODE_FAILED);
}
g.events().remoteListen(null, new P1<Event>() {
@Override
public boolean apply(Event evt) {
return true;
}
}, EVTS_ALL);
} finally {
stopGrid("anotherGridMasterNodeLeave");
}
assert latch.await(3000, MILLISECONDS);
}
use of org.apache.ignite.events.DiscoveryEvent in project ignite by apache.
the class CommunicationDiscoveryEventListener method onEvent.
/**
* {@inheritDoc}
*/
@Override
public void onEvent(Event evt) {
assert evt instanceof DiscoveryEvent : evt;
assert evt.type() == EVT_NODE_LEFT || evt.type() == EVT_NODE_FAILED;
ClusterNode node = ((DiscoveryEvent) evt).eventNode();
onNodeLeft(node.consistentId(), node.id());
}
Aggregations