Search in sources :

Example 61 with Event

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));
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteException(org.apache.ignite.IgniteException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) CacheException(javax.cache.CacheException) Event(org.apache.ignite.events.Event) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) Ignite(org.apache.ignite.Ignite)

Example 62 with Event

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));
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) IgniteException(org.apache.ignite.IgniteException) Event(org.apache.ignite.events.Event) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) Ignite(org.apache.ignite.Ignite) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 63 with Event

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);
    }
}
Also used : CacheException(javax.cache.CacheException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) TimeoutException(java.util.concurrent.TimeoutException) CacheException(javax.cache.CacheException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Event(org.apache.ignite.events.Event) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID) TimeoutException(java.util.concurrent.TimeoutException)

Example 64 with Event

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());
}
Also used : IgniteException(org.apache.ignite.IgniteException) IgfsEvent(org.apache.ignite.events.IgfsEvent) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) ArrayList(java.util.ArrayList) Event(org.apache.ignite.events.Event) IgfsEvent(org.apache.ignite.events.IgfsEvent) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 65 with Event

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());
}
Also used : IgfsEvent(org.apache.ignite.events.IgfsEvent) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) ArrayList(java.util.ArrayList) Event(org.apache.ignite.events.Event) IgfsEvent(org.apache.ignite.events.IgfsEvent) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

Event (org.apache.ignite.events.Event)226 CountDownLatch (java.util.concurrent.CountDownLatch)127 Ignite (org.apache.ignite.Ignite)112 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)96 UUID (java.util.UUID)71 IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)44 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)43 ClusterNode (org.apache.ignite.cluster.ClusterNode)38 GridLocalEventListener (org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener)28 ArrayList (java.util.ArrayList)27 JobEvent (org.apache.ignite.events.JobEvent)27 IgniteException (org.apache.ignite.IgniteException)20 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)19 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)16 HashSet (java.util.HashSet)15 TaskEvent (org.apache.ignite.events.TaskEvent)15 Collection (java.util.Collection)13 HashMap (java.util.HashMap)12 GridMessageListener (org.apache.ignite.internal.managers.communication.GridMessageListener)12 GridConcurrentHashSet (org.apache.ignite.internal.util.GridConcurrentHashSet)12