use of org.apache.ignite.events.Event in project ignite by apache.
the class IgniteClientReconnectCacheTest method testReconnectClusterRestart.
/**
* @throws Exception If failed.
*/
public void testReconnectClusterRestart() throws Exception {
clientMode = true;
final Ignite client = startGrid(SRV_CNT);
assertTrue(client.cluster().localNode().isClient());
final CountDownLatch disconnectLatch = new CountDownLatch(1);
final CountDownLatch reconnectLatch = new CountDownLatch(1);
final IgniteCache<Object, Object> clientCache = client.getOrCreateCache(new CacheConfiguration<>(DEFAULT_CACHE_NAME));
clientCache.put(1, new TestClass1());
client.events().localListen(new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
if (evt.type() == EVT_CLIENT_NODE_DISCONNECTED) {
info("Disconnected: " + evt);
disconnectLatch.countDown();
} else if (evt.type() == EVT_CLIENT_NODE_RECONNECTED) {
info("Reconnected: " + evt);
reconnectLatch.countDown();
}
return true;
}
}, EVT_CLIENT_NODE_DISCONNECTED, EVT_CLIENT_NODE_RECONNECTED);
for (int i = 0; i < SRV_CNT; i++) stopGrid(i);
assertTrue(disconnectLatch.await(30_000, MILLISECONDS));
clientMode = false;
Ignite srv = startGrid(0);
assertTrue(reconnectLatch.await(10_000, MILLISECONDS));
GridTestUtils.assertThrows(log, new Callable<Object>() {
@Override
public Object call() throws Exception {
return clientCache.get(1);
}
}, IllegalStateException.class, null);
IgniteCache<Object, Object> srvCache = srv.getOrCreateCache(new CacheConfiguration<>(DEFAULT_CACHE_NAME));
srvCache.put(1, new TestClass1());
srvCache.put(2, new TestClass2());
IgniteCache<Object, Object> clientCache2 = client.cache(DEFAULT_CACHE_NAME);
assertNotNull(clientCache2);
assertNotNull(clientCache2.get(1));
assertNotNull(clientCache2.get(2));
}
use of org.apache.ignite.events.Event in project ignite by apache.
the class IgniteClientReconnectCacheTest method testReconnectExchangeInProgress.
/**
* @throws Exception If failed.
*/
public void testReconnectExchangeInProgress() throws Exception {
clientMode = true;
IgniteEx client = startGrid(SRV_CNT);
Ignite srv = clientRouter(client);
TestTcpDiscoverySpi srvSpi = spi(srv);
TestCommunicationSpi coordCommSpi = (TestCommunicationSpi) grid(0).configuration().getCommunicationSpi();
coordCommSpi.blockMessages(GridDhtPartitionsFullMessage.class, client.localNode().id());
clientMode = false;
startGrid(SRV_CNT + 1);
final CountDownLatch reconnectLatch = new CountDownLatch(1);
client.events().localListen(new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
if (evt.type() == EVT_CLIENT_NODE_RECONNECTED) {
info("Reconnected: " + evt);
reconnectLatch.countDown();
}
return true;
}
}, EVT_CLIENT_NODE_RECONNECTED);
srvSpi.failNode(client.cluster().localNode().id(), null);
assertTrue(reconnectLatch.await(5000, MILLISECONDS));
try {
coordCommSpi.stopBlock(true);
fail();
} catch (IgniteException e) {
log.info("Expected error: " + e);
}
CacheConfiguration<Object, Object> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
ccfg.setName("newCache");
ccfg.setCacheMode(REPLICATED);
log.info("Start new cache.");
IgniteCache<Object, Object> cache = client.getOrCreateCache(ccfg);
cache.put(1, 1);
assertEquals(1, cache.get(1));
}
use of org.apache.ignite.events.Event in project ignite by apache.
the class IgniteClientReconnectFailoverAbstractTest method reconnectFailover.
/**
* @param c Test closure.
* @throws Exception If failed.
*/
protected final void reconnectFailover(final Callable<Void> c) throws Exception {
final Ignite client = grid(serverCount());
assertTrue(client.cluster().localNode().isClient());
Ignite srv = clientRouter(client);
TestTcpDiscoverySpi srvSpi = spi(srv);
final AtomicBoolean stop = new AtomicBoolean(false);
final IgniteInternalFuture<Long> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
try {
int iter = 0;
while (!stop.get()) {
try {
c.call();
} catch (CacheException e) {
checkAndWait(e);
} catch (IgniteClientDisconnectedException e) {
checkAndWait(e);
}
if (++iter % 100 == 0)
log.info("Iteration: " + iter);
if (barrier != null)
barrier.await();
}
return null;
} catch (Throwable e) {
log.error("Unexpected error in operation thread: " + e, e);
stop.set(true);
throw e;
}
}
}, THREADS, "test-operation-thread");
final AtomicReference<CountDownLatch> disconnected = new AtomicReference<>();
final AtomicReference<CountDownLatch> reconnected = new AtomicReference<>();
IgnitePredicate<Event> p = new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
if (evt.type() == EVT_CLIENT_NODE_RECONNECTED) {
info("Reconnected: " + evt);
CountDownLatch latch = reconnected.get();
assertNotNull(latch);
assertEquals(1, latch.getCount());
latch.countDown();
} else if (evt.type() == EVT_CLIENT_NODE_DISCONNECTED) {
info("Disconnected: " + evt);
CountDownLatch latch = disconnected.get();
assertNotNull(latch);
assertEquals(1, latch.getCount());
latch.countDown();
}
return true;
}
};
client.events().localListen(p, EVT_CLIENT_NODE_DISCONNECTED, EVT_CLIENT_NODE_RECONNECTED);
try {
long stopTime = System.currentTimeMillis() + TEST_TIME;
String err = null;
while (System.currentTimeMillis() < stopTime && !fut.isDone()) {
U.sleep(500);
CountDownLatch disconnectLatch = new CountDownLatch(1);
CountDownLatch reconnectLatch = new CountDownLatch(1);
disconnected.set(disconnectLatch);
reconnected.set(reconnectLatch);
UUID nodeId = client.cluster().localNode().id();
log.info("Fail client: " + nodeId);
srvSpi.failNode(nodeId, null);
if (!disconnectLatch.await(10_000, MILLISECONDS)) {
err = "Failed to wait for disconnect";
break;
}
if (!reconnectLatch.await(10_000, MILLISECONDS)) {
err = "Failed to wait for reconnect";
break;
}
barrier = new CyclicBarrier(THREADS + 1, new Runnable() {
@Override
public void run() {
barrier = null;
}
});
try {
barrier.await(10, SECONDS);
} catch (TimeoutException ignored) {
err = "Operations hang or fail with unexpected error.";
break;
}
}
if (err != null) {
log.error(err);
U.dumpThreads(log);
CyclicBarrier barrier0 = barrier;
if (barrier0 != null)
barrier0.reset();
stop.set(true);
fut.get();
fail(err);
}
stop.set(true);
fut.get();
} finally {
client.events().stopLocalListen(p);
stop.set(true);
}
}
use of org.apache.ignite.events.Event in project ignite by apache.
the class IgfsEventsAbstractSelfTest method testDeleteNonRecursive.
/**
* Checks events on CRUD operations with non-recursive
* directory deletion.
*
* @throws Exception If failed.
*/
public void testDeleteNonRecursive() throws Exception {
final List<Event> evtList = new ArrayList<>();
final int evtsCnt = 2 + 0 + 1;
final CountDownLatch latch = new CountDownLatch(evtsCnt);
grid(1).events().localListen(lsnr = new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
log.info("Received event [evt=" + evt + ']');
evtList.add(evt);
latch.countDown();
return true;
}
}, EVTS_IGFS);
IgfsPath dir = new IgfsPath("/dir1/dir2");
// Will generate 2 EVT_IGFS_DIR_CREATED events.
igfs.mkdirs(dir);
try {
// Will generate no events.
igfs.delete(dir.parent(), false);
} catch (IgniteException ignore) {
// No-op.
}
// Will generate 1 EVT_IGFS_DIR_DELETED event.
assertTrue(igfs.delete(dir, false));
assertTrue(latch.await(10, TimeUnit.SECONDS));
assertEquals(evtsCnt, evtList.size());
IgfsEvent evt = (IgfsEvent) evtList.get(0);
assertEquals(EVT_IGFS_DIR_CREATED, evt.type());
assertEquals(new IgfsPath("/dir1"), evt.path());
evt = (IgfsEvent) evtList.get(1);
assertEquals(EVT_IGFS_DIR_CREATED, evt.type());
assertEquals(new IgfsPath("/dir1/dir2"), evt.path());
IgfsEvent evt3 = (IgfsEvent) evtList.get(2);
assertEquals(EVT_IGFS_DIR_DELETED, evt3.type());
assertEquals(new IgfsPath("/dir1/dir2"), evt3.path());
}
use of org.apache.ignite.events.Event in project ignite by apache.
the class IgfsEventsAbstractSelfTest method testDirWithFiles.
/**
* Checks events on CRUD operations on a single directory
* with some files.
*
* @throws Exception If failed.
*/
public void testDirWithFiles() throws Exception {
final List<Event> evtList = new ArrayList<>();
final int evtsCnt = 4 + 3 + 1;
final CountDownLatch latch = new CountDownLatch(evtsCnt);
grid(1).events().localListen(lsnr = new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
log.info("Received event [evt=" + evt + ']');
evtList.add(evt);
latch.countDown();
return true;
}
}, EVTS_IGFS);
IgfsPath dir = new IgfsPath("/dir1");
IgfsPath file1 = new IgfsPath(dir, "file1");
IgfsPath file2 = new IgfsPath(dir, "file2");
// Will generate EVT_IGFS_DIR_CREATED + EVT_IGFS_FILE_CREATED + EVT_IGFS_FILE_OPENED_WRITE +
// EVT_IGFS_FILE_CLOSED_WRITE.
igfs.create(file1, true).close();
// Will generate EVT_IGFS_FILE_CREATED + EVT_IGFS_FILE_OPENED_WRITE +
// EVT_IGFS_FILE_CLOSED.
igfs.create(file2, true).close();
// Will generate EVT_IGFS_DIR_DELETED event.
assertTrue(igfs.delete(dir, true));
assertTrue(latch.await(10, TimeUnit.SECONDS));
assertEquals(evtsCnt, evtList.size());
IgfsEvent evt = (IgfsEvent) evtList.get(0);
assertEquals(EVT_IGFS_DIR_CREATED, evt.type());
assertEquals(new IgfsPath("/dir1"), evt.path());
assertTrue(evt.isDirectory());
evt = (IgfsEvent) evtList.get(1);
assertEquals(EVT_IGFS_FILE_CREATED, evt.type());
assertEquals(new IgfsPath("/dir1/file1"), evt.path());
assertFalse(evt.isDirectory());
evt = (IgfsEvent) evtList.get(2);
assertEquals(EVT_IGFS_FILE_OPENED_WRITE, evt.type());
assertEquals(new IgfsPath("/dir1/file1"), evt.path());
evt = (IgfsEvent) evtList.get(3);
assertEquals(EVT_IGFS_FILE_CLOSED_WRITE, evt.type());
assertEquals(new IgfsPath("/dir1/file1"), evt.path());
evt = (IgfsEvent) evtList.get(4);
assertEquals(EVT_IGFS_FILE_CREATED, evt.type());
assertEquals(new IgfsPath("/dir1/file2"), evt.path());
assertFalse(evt.isDirectory());
evt = (IgfsEvent) evtList.get(5);
assertEquals(EVT_IGFS_FILE_OPENED_WRITE, evt.type());
assertEquals(new IgfsPath("/dir1/file2"), evt.path());
evt = (IgfsEvent) evtList.get(6);
assertEquals(EVT_IGFS_FILE_CLOSED_WRITE, evt.type());
assertEquals(new IgfsPath("/dir1/file2"), evt.path());
evt = (IgfsEvent) evtList.get(7);
assertEquals(EVT_IGFS_DIR_DELETED, evt.type());
assertEquals(new IgfsPath("/dir1"), evt.path());
}
Aggregations