Search in sources :

Example 16 with GridFutureAdapter

use of org.apache.ignite.internal.util.future.GridFutureAdapter in project ignite by apache.

the class IgniteRoundRobinErrorAfterClientReconnectTest method testClientReconnect.

/**
     * @throws Exception If failed.
     */
public void testClientReconnect() throws Exception {
    final Ignite cli = grid(CLI_IDX);
    final GridFutureAdapter<Boolean> fut = new GridFutureAdapter<>();
    cli.events().localListen(new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event event) {
            try {
                cli.compute().apply(new IgniteClosure<String, Void>() {

                    @Override
                    public Void apply(String arg) {
                        return null;
                    }
                }, "Hello!");
                fut.onDone(true);
                return true;
            } catch (Exception e) {
                fut.onDone(e);
                return false;
            }
        }
    }, EventType.EVT_CLIENT_NODE_RECONNECTED);
    stopGrid(SRV_IDX);
    startGrid(SRV_IDX);
    assert fut.get();
}
Also used : IgniteClosure(org.apache.ignite.lang.IgniteClosure) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) Event(org.apache.ignite.events.Event) Ignite(org.apache.ignite.Ignite)

Example 17 with GridFutureAdapter

use of org.apache.ignite.internal.util.future.GridFutureAdapter in project ignite by apache.

the class GridFutureListenPerformanceTest method main.

/**
     * @param args Args.
     * @throws InterruptedException If failed.
     */
public static void main(String[] args) throws InterruptedException {
    final LongAdder8 cnt = new LongAdder8();
    final ConcurrentLinkedDeque8<GridFutureAdapter<Object>> futs = new ConcurrentLinkedDeque8<>();
    ExecutorService pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    Thread statThread = new Thread() {

        @SuppressWarnings("BusyWait")
        @Override
        public void run() {
            while (!done) {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException ignored) {
                    return;
                }
                System.out.println(new Date() + " Notifications per sec: " + (cnt.sumThenReset() / 5));
            }
        }
    };
    statThread.setDaemon(true);
    statThread.start();
    for (int i = 0; i < Runtime.getRuntime().availableProcessors(); i++) {
        pool.submit(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                Random rnd = new Random();
                while (!done) {
                    for (int j = 0; j < rnd.nextInt(10); j++) {
                        GridFutureAdapter<Object> fut = new GridFutureAdapter<>();
                        futs.add(fut);
                        for (int k = 1; k < rnd.nextInt(3); k++) {
                            fut.listen(new IgniteInClosure<IgniteInternalFuture<Object>>() {

                                @Override
                                public void apply(IgniteInternalFuture<Object> t) {
                                    try {
                                        t.get();
                                    } catch (IgniteCheckedException e) {
                                        e.printStackTrace();
                                    }
                                    cnt.increment();
                                }
                            });
                        }
                    }
                    GridFutureAdapter<Object> fut;
                    while ((fut = futs.poll()) != null) fut.onDone();
                }
                return null;
            }
        });
    }
    Thread.sleep(5 * 60 * 1000);
    done = true;
    pool.shutdownNow();
    pool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
}
Also used : ConcurrentLinkedDeque8(org.jsr166.ConcurrentLinkedDeque8) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) Date(java.util.Date) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Random(java.util.Random) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) ExecutorService(java.util.concurrent.ExecutorService) LongAdder8(org.jsr166.LongAdder8)

Example 18 with GridFutureAdapter

use of org.apache.ignite.internal.util.future.GridFutureAdapter in project ignite by apache.

the class GridEventStorageManager method remoteEventsAsync.

/**
     * @param p Grid event predicate.
     * @param nodes Collection of nodes.
     * @param timeout Maximum time to wait for result, if {@code 0}, then wait until result is received.
     * @return Collection of events.
     */
public <T extends Event> IgniteInternalFuture<List<T>> remoteEventsAsync(final IgnitePredicate<T> p, final Collection<? extends ClusterNode> nodes, final long timeout) {
    assert p != null;
    assert nodes != null;
    final GridFutureAdapter<List<T>> fut = new GridFutureAdapter<>();
    ctx.closure().runLocalSafe(new GPR() {

        @Override
        public void run() {
            try {
                fut.onDone(query(p, nodes, timeout));
            } catch (IgniteCheckedException e) {
                fut.onDone(e);
            }
        }
    }, true);
    return fut;
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) GPR(org.apache.ignite.internal.util.typedef.internal.GPR) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList)

Example 19 with GridFutureAdapter

use of org.apache.ignite.internal.util.future.GridFutureAdapter in project ignite by apache.

the class GridAffinityProcessor method affinityCache.

/**
     * @param cacheName Cache name.
     * @param topVer Topology version.
     * @return Affinity cache.
     * @throws IgniteCheckedException In case of error.
     */
@SuppressWarnings("ErrorNotRethrown")
@Nullable
private AffinityInfo affinityCache(final String cacheName, AffinityTopologyVersion topVer) throws IgniteCheckedException {
    AffinityAssignmentKey key = new AffinityAssignmentKey(cacheName, topVer);
    IgniteInternalFuture<AffinityInfo> fut = affMap.get(key);
    if (fut != null)
        return fut.get();
    ClusterNode loc = ctx.discovery().localNode();
    // Check local node.
    Collection<ClusterNode> cacheNodes = ctx.discovery().cacheNodes(cacheName, topVer);
    if (cacheNodes.contains(loc)) {
        GridCacheAdapter<Object, Object> cache = ctx.cache().internalCache(cacheName);
        // Cache is being stopped.
        if (cache == null)
            return null;
        GridCacheContext<Object, Object> cctx = cache.context();
        cctx.awaitStarted();
        try {
            cctx.gate().enter();
        } catch (IllegalStateException ignored) {
            return null;
        }
        try {
            AffinityAssignment assign0 = cctx.affinity().assignment(topVer);
            GridAffinityAssignment assign = assign0 instanceof GridAffinityAssignment ? (GridAffinityAssignment) assign0 : new GridAffinityAssignment(topVer, assign0.assignment(), assign0.idealAssignment());
            AffinityInfo info = new AffinityInfo(cctx.config().getAffinity(), cctx.config().getAffinityMapper(), assign, cctx.cacheObjectContext());
            IgniteInternalFuture<AffinityInfo> old = affMap.putIfAbsent(key, new GridFinishedFuture<>(info));
            if (old != null)
                info = old.get();
            return info;
        } finally {
            cctx.gate().leave();
        }
    }
    if (F.isEmpty(cacheNodes))
        return null;
    GridFutureAdapter<AffinityInfo> fut0 = new GridFutureAdapter<>();
    IgniteInternalFuture<AffinityInfo> old = affMap.putIfAbsent(key, fut0);
    if (old != null)
        return old.get();
    int max = ERROR_RETRIES;
    int cnt = 0;
    Iterator<ClusterNode> it = cacheNodes.iterator();
    // We are here because affinity has not been fetched yet, or cache mode is LOCAL.
    while (true) {
        cnt++;
        if (!it.hasNext())
            it = cacheNodes.iterator();
        // Double check since we deal with dynamic view.
        if (!it.hasNext())
            // Exception will be caught in this method.
            throw new IgniteCheckedException("No cache nodes in topology for cache name: " + cacheName);
        ClusterNode n = it.next();
        CacheMode mode = ctx.cache().cacheMode(cacheName);
        assert mode != null;
        // Map all keys to a single node, if the cache mode is LOCAL.
        if (mode == LOCAL) {
            fut0.onDone(new IgniteCheckedException("Failed to map keys for LOCAL cache."));
            // Will throw exception.
            fut0.get();
        }
        try {
            // Resolve cache context for remote node.
            // Set affinity function before counting down on latch.
            fut0.onDone(affinityInfoFromNode(cacheName, topVer, n));
            break;
        } catch (IgniteCheckedException e) {
            if (log.isDebugEnabled())
                log.debug("Failed to get affinity from node (will retry) [cache=" + cacheName + ", node=" + U.toShortString(n) + ", msg=" + e.getMessage() + ']');
            if (cnt < max) {
                U.sleep(ERROR_WAIT);
                continue;
            }
            affMap.remove(cacheName, fut0);
            fut0.onDone(new IgniteCheckedException("Failed to get affinity mapping from node: " + n, e));
            break;
        } catch (RuntimeException | Error e) {
            fut0.onDone(new IgniteCheckedException("Failed to get affinity mapping from node: " + n, e));
            break;
        }
    }
    return fut0.get();
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) CacheMode(org.apache.ignite.cache.CacheMode) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) BinaryObject(org.apache.ignite.binary.BinaryObject) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) Nullable(org.jetbrains.annotations.Nullable)

Example 20 with GridFutureAdapter

use of org.apache.ignite.internal.util.future.GridFutureAdapter in project ignite by apache.

the class GridDhtPartitionDemander method forceRebalance.

/**
     * Force Rebalance.
     */
IgniteInternalFuture<Boolean> forceRebalance() {
    GridTimeoutObject obj = lastTimeoutObj.getAndSet(null);
    if (obj != null)
        cctx.time().removeTimeoutObject(obj);
    final GridDhtPartitionsExchangeFuture exchFut = lastExchangeFut;
    if (exchFut != null) {
        if (log.isDebugEnabled())
            log.debug("Forcing rebalance event for future: " + exchFut);
        final GridFutureAdapter<Boolean> fut = new GridFutureAdapter<>();
        exchFut.listen(new CI1<IgniteInternalFuture<AffinityTopologyVersion>>() {

            @Override
            public void apply(IgniteInternalFuture<AffinityTopologyVersion> t) {
                IgniteInternalFuture<Boolean> fut0 = cctx.shared().exchange().forceRebalance(exchFut);
                fut0.listen(new IgniteInClosure<IgniteInternalFuture<Boolean>>() {

                    @Override
                    public void apply(IgniteInternalFuture<Boolean> future) {
                        try {
                            fut.onDone(future.get());
                        } catch (Exception e) {
                            fut.onDone(e);
                        }
                    }
                });
            }
        });
        return fut;
    } else if (log.isDebugEnabled())
        log.debug("Ignoring force rebalance request (no topology event happened yet).");
    return new GridFinishedFuture<>(true);
}
Also used : AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) GridDhtInvalidPartitionException(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtInvalidPartitionException) GridTimeoutObject(org.apache.ignite.internal.processors.timeout.GridTimeoutObject) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Aggregations

GridFutureAdapter (org.apache.ignite.internal.util.future.GridFutureAdapter)30 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)19 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)8 Map (java.util.Map)6 ConcurrentMap (java.util.concurrent.ConcurrentMap)6 HashMap (java.util.HashMap)5 UUID (java.util.UUID)5 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)5 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)5 IgniteException (org.apache.ignite.IgniteException)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)3 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)3 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)3 GridCompoundFuture (org.apache.ignite.internal.util.future.GridCompoundFuture)3 Nullable (org.jetbrains.annotations.Nullable)3 LinkedHashMap (java.util.LinkedHashMap)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Ignite (org.apache.ignite.Ignite)2