Search in sources :

Example 1 with Event

use of org.apache.ignite.events.Event in project ignite by apache.

the class GridCachePartitionedPreloadEventsSelfTest method testForcePreload.

/**
     * Test events fired from
     * {@link GridDhtForceKeysFuture}
     *
     * @throws Exception if failed.
     */
public void testForcePreload() throws Exception {
    replicatedAffinity = false;
    rebalanceDelay = -1;
    Ignite g1 = startGrid("g1");
    Collection<Integer> keys = new HashSet<>();
    IgniteCache<Integer, String> cache = g1.cache(DEFAULT_CACHE_NAME);
    for (int i = 0; i < 100; i++) {
        keys.add(i);
        cache.put(i, "val");
    }
    Ignite g2 = startGrid("g2");
    Map<ClusterNode, Collection<Object>> keysMap = g1.affinity(DEFAULT_CACHE_NAME).mapKeysToNodes(keys);
    Collection<Object> g2Keys = keysMap.get(g2.cluster().localNode());
    assertNotNull(g2Keys);
    assertFalse("There are no keys assigned to g2", g2Keys.isEmpty());
    for (Object key : g2Keys) // Need to force keys loading.
    assertEquals("val", g2.cache(DEFAULT_CACHE_NAME).getAndPut(key, "changed val"));
    Collection<Event> evts = g2.events().localQuery(F.<Event>alwaysTrue(), EVT_CACHE_REBALANCE_OBJECT_LOADED);
    checkPreloadEvents(evts, g2, g2Keys);
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) Collection(java.util.Collection) Event(org.apache.ignite.events.Event) Ignite(org.apache.ignite.Ignite) HashSet(java.util.HashSet)

Example 2 with Event

use of org.apache.ignite.events.Event in project ignite by apache.

the class GridEventStorageManager method waitForEvent.

/**
     *
     * @param timeout Timeout.
     * @param c Optional continuation.
     * @param p Optional predicate.
     * @param types Event types to wait for.
     * @return Event.
     * @throws IgniteCheckedException Thrown in case of any errors.
     */
public Event waitForEvent(long timeout, @Nullable Runnable c, @Nullable final IgnitePredicate<? super Event> p, int... types) throws IgniteCheckedException {
    assert timeout >= 0;
    final GridFutureAdapter<Event> fut = new GridFutureAdapter<>();
    addLocalEventListener(new GridLocalEventListener() {

        @Override
        public void onEvent(Event evt) {
            if (p == null || p.apply(evt)) {
                fut.onDone(evt);
                removeLocalEventListener(this);
            }
        }
    }, types);
    try {
        if (c != null)
            c.run();
    } catch (Exception e) {
        throw new IgniteCheckedException(e);
    }
    return fut.get(timeout);
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) Event(org.apache.ignite.events.Event) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) IgniteDeploymentCheckedException(org.apache.ignite.internal.IgniteDeploymentCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException)

Example 3 with Event

use of org.apache.ignite.events.Event in project ignite by apache.

the class GridDhtPartitionsExchangeFuture method warnNoAffinityNodes.

/**
     *
     */
private void warnNoAffinityNodes() {
    List<String> cachesWithoutNodes = null;
    for (String name : cctx.cache().cacheNames()) {
        if (discoCache.cacheAffinityNodes(name).isEmpty()) {
            if (cachesWithoutNodes == null)
                cachesWithoutNodes = new ArrayList<>();
            cachesWithoutNodes.add(name);
            // Fire event even if there is no client cache started.
            if (cctx.gridEvents().isRecordable(EventType.EVT_CACHE_NODES_LEFT)) {
                Event evt = new CacheEvent(name, cctx.localNode(), cctx.localNode(), "All server nodes have left the cluster.", EventType.EVT_CACHE_NODES_LEFT, 0, false, null, null, null, null, false, null, false, null, null, null);
                cctx.gridEvents().record(evt);
            }
        }
    }
    if (cachesWithoutNodes != null) {
        StringBuilder sb = new StringBuilder("All server nodes for the following caches have left the cluster: ");
        for (int i = 0; i < cachesWithoutNodes.size(); i++) {
            String cache = cachesWithoutNodes.get(i);
            sb.append('\'').append(cache).append('\'');
            if (i != cachesWithoutNodes.size() - 1)
                sb.append(", ");
        }
        U.quietAndWarn(log, sb.toString());
        U.quietAndWarn(log, "Must have server nodes for caches to operate.");
    }
}
Also used : ArrayList(java.util.ArrayList) CacheEvent(org.apache.ignite.events.CacheEvent) DiscoveryCustomEvent(org.apache.ignite.internal.events.DiscoveryCustomEvent) CacheEvent(org.apache.ignite.events.CacheEvent) Event(org.apache.ignite.events.Event) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent)

Example 4 with Event

use of org.apache.ignite.events.Event in project ignite by apache.

the class CacheMetricsForClusterGroupSelfTest method awaitMetricsUpdate.

/**
 * Wait for {@link EventType#EVT_NODE_METRICS_UPDATED} event will be receieved.
 */
private void awaitMetricsUpdate() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch((GRID_CNT + 1) * 2);
    IgnitePredicate<Event> lsnr = new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event ignore) {
            latch.countDown();
            return true;
        }
    };
    for (int i = 0; i < GRID_CNT; i++) grid(i).events().localListen(lsnr, EVT_NODE_METRICS_UPDATED);
    latch.await();
}
Also used : IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) Event(org.apache.ignite.events.Event) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 5 with Event

use of org.apache.ignite.events.Event in project ignite by apache.

the class IgniteCacheGroupsPartitionLossPolicySelfTest method prepareTopology.

/**
 * @return Lost partition ID.
 * @throws Exception If failed.
 */
private int prepareTopology() throws Exception {
    startGrids(4);
    final String cacheName = ThreadLocalRandom.current().nextBoolean() ? CACHE_1 : CACHE_2;
    Affinity<Object> aff = ignite(0).affinity(cacheName);
    for (int i = 0; i < aff.partitions(); i++) {
        ignite(0).cache(CACHE_1).put(i, i);
        ignite(0).cache(CACHE_2).put(i, i);
    }
    client = true;
    startGrid(4);
    client = false;
    for (int i = 0; i < 5; i++) info(">>> Node [idx=" + i + ", nodeId=" + ignite(i).cluster().localNode().id() + ']');
    awaitPartitionMapExchange();
    ClusterNode killNode = ignite(3).cluster().localNode();
    int part = -1;
    for (int i = 0; i < aff.partitions(); i++) {
        if (aff.isPrimary(killNode, i)) {
            part = i;
            break;
        }
    }
    if (part == -1)
        throw new IllegalStateException("No partition on node: " + killNode);
    final CountDownLatch[] partLost = new CountDownLatch[3];
    // Check events.
    for (int i = 0; i < 3; i++) {
        final CountDownLatch latch = new CountDownLatch(1);
        partLost[i] = latch;
        final int part0 = part;
        grid(i).events().localListen(new P1<Event>() {

            @Override
            public boolean apply(Event evt) {
                assert evt.type() == EventType.EVT_CACHE_REBALANCE_PART_DATA_LOST;
                CacheRebalancingEvent cacheEvt = (CacheRebalancingEvent) evt;
                if (cacheEvt.partition() == part0 && F.eq(cacheName, cacheEvt.cacheName())) {
                    latch.countDown();
                    // Auto-unsubscribe.
                    return false;
                }
                return true;
            }
        }, EventType.EVT_CACHE_REBALANCE_PART_DATA_LOST);
    }
    ignite(3).close();
    for (CountDownLatch latch : partLost) assertTrue("Failed to wait for partition LOST event", latch.await(10, TimeUnit.SECONDS));
    return part;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) Event(org.apache.ignite.events.Event) CacheRebalancingEvent(org.apache.ignite.events.CacheRebalancingEvent) CountDownLatch(java.util.concurrent.CountDownLatch) CacheRebalancingEvent(org.apache.ignite.events.CacheRebalancingEvent)

Aggregations

Event (org.apache.ignite.events.Event)273 Test (org.junit.Test)148 CountDownLatch (java.util.concurrent.CountDownLatch)146 Ignite (org.apache.ignite.Ignite)144 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)125 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)103 UUID (java.util.UUID)87 IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)76 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)50 ClusterNode (org.apache.ignite.cluster.ClusterNode)48 IgniteException (org.apache.ignite.IgniteException)34 ArrayList (java.util.ArrayList)33 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)31 JobEvent (org.apache.ignite.events.JobEvent)28 GridLocalEventListener (org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener)28 HashMap (java.util.HashMap)27 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)26 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)26 Collection (java.util.Collection)23 Map (java.util.Map)23