Search in sources :

Example 1 with GridFutureAdapter

use of org.apache.ignite.internal.util.future.GridFutureAdapter 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 2 with GridFutureAdapter

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

the class CacheAffinitySharedManager method initCoordinatorCaches.

/**
     * @param fut Exchange future.
     * @throws IgniteCheckedException If failed.
     * @return Future completed when caches initialization is done.
     */
private IgniteInternalFuture<?> initCoordinatorCaches(final GridDhtPartitionsExchangeFuture fut) throws IgniteCheckedException {
    final List<IgniteInternalFuture<AffinityTopologyVersion>> futs = new ArrayList<>();
    forAllRegisteredCaches(new IgniteInClosureX<DynamicCacheDescriptor>() {

        @Override
        public void applyx(DynamicCacheDescriptor desc) throws IgniteCheckedException {
            CacheHolder cache = caches.get(desc.cacheId());
            if (cache != null) {
                if (cache.client())
                    cache.affinity().calculate(fut.topologyVersion(), fut.discoveryEvent(), fut.discoCache());
                return;
            }
            final Integer cacheId = desc.cacheId();
            GridCacheContext cacheCtx = cctx.cacheContext(cacheId);
            if (cacheCtx == null) {
                cctx.io().addHandler(desc.cacheId(), GridDhtAffinityAssignmentResponse.class, new IgniteBiInClosure<UUID, GridDhtAffinityAssignmentResponse>() {

                    @Override
                    public void apply(UUID nodeId, GridDhtAffinityAssignmentResponse res) {
                        processAffinityAssignmentResponse(nodeId, res);
                    }
                });
                cache = CacheHolder2.create(cctx, desc, fut, null);
                final GridAffinityAssignmentCache aff = cache.affinity();
                List<GridDhtPartitionsExchangeFuture> exchFuts = cctx.exchange().exchangeFutures();
                int idx = exchFuts.indexOf(fut);
                assert idx >= 0 && idx < exchFuts.size() - 1 : "Invalid exchange futures state [cur=" + idx + ", total=" + exchFuts.size() + ']';
                final GridDhtPartitionsExchangeFuture prev = exchFuts.get(idx + 1);
                if (log.isDebugEnabled()) {
                    log.debug("Need initialize affinity on coordinator [" + "cache=" + desc.cacheConfiguration().getName() + "prevAff=" + prev.topologyVersion() + ']');
                }
                assert prev.topologyVersion().compareTo(fut.topologyVersion()) < 0 : prev;
                GridDhtAssignmentFetchFuture fetchFut = new GridDhtAssignmentFetchFuture(cctx, desc, prev.topologyVersion(), prev.discoCache());
                fetchFut.init();
                final GridFutureAdapter<AffinityTopologyVersion> affFut = new GridFutureAdapter<>();
                fetchFut.listen(new IgniteInClosureX<IgniteInternalFuture<GridDhtAffinityAssignmentResponse>>() {

                    @Override
                    public void applyx(IgniteInternalFuture<GridDhtAffinityAssignmentResponse> fetchFut) throws IgniteCheckedException {
                        fetchAffinity(prev, aff, (GridDhtAssignmentFetchFuture) fetchFut);
                        aff.calculate(fut.topologyVersion(), fut.discoveryEvent(), fut.discoCache());
                        affFut.onDone(fut.topologyVersion());
                    }
                });
                futs.add(affFut);
            } else
                cache = new CacheHolder1(cacheCtx, null);
            CacheHolder old = caches.put(cache.cacheId(), cache);
            assert old == null : old;
        }
    });
    if (!futs.isEmpty()) {
        GridCompoundFuture<AffinityTopologyVersion, ?> affFut = new GridCompoundFuture<>();
        for (IgniteInternalFuture<AffinityTopologyVersion> f : futs) affFut.add(f);
        affFut.markInitialized();
        return affFut;
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) IgniteBiInClosure(org.apache.ignite.lang.IgniteBiInClosure) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteInClosureX(org.apache.ignite.internal.util.lang.IgniteInClosureX) GridCompoundFuture(org.apache.ignite.internal.util.future.GridCompoundFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridAffinityAssignmentCache(org.apache.ignite.internal.processors.affinity.GridAffinityAssignmentCache) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) GridDhtAssignmentFetchFuture(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtAssignmentFetchFuture) ArrayList(java.util.ArrayList) List(java.util.List) UUID(java.util.UUID) GridDhtPartitionsExchangeFuture(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsExchangeFuture) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridDhtAffinityAssignmentResponse(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtAffinityAssignmentResponse)

Example 3 with GridFutureAdapter

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

the class GridCachePartitionExchangeManager method forceRebalance.

/**
     * Forces preload exchange.
     *
     * @param exchFut Exchange future.
     */
public IgniteInternalFuture<Boolean> forceRebalance(GridDhtPartitionsExchangeFuture exchFut) {
    GridFutureAdapter<Boolean> fut = new GridFutureAdapter<>();
    exchWorker.addExchangeFuture(new GridDhtPartitionsExchangeFuture(cctx, exchFut.discoveryEvent(), exchFut.exchangeId(), fut));
    return fut;
}
Also used : GridDhtPartitionsExchangeFuture(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsExchangeFuture) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 4 with GridFutureAdapter

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

the class IgfsIpcHandler method handleAsync.

/**
 * {@inheritDoc}
 */
@Override
public IgniteInternalFuture<IgfsMessage> handleAsync(final IgfsClientSession ses, final IgfsMessage msg, final DataInput in) {
    try {
        // Even if will be closed right after this call, response write error will be ignored.
        if (stopping)
            return null;
        final IgfsIpcCommand cmd = msg.command();
        IgniteInternalFuture<IgfsMessage> fut;
        switch(cmd) {
            // Execute not-blocking command synchronously in worker thread.
            case WRITE_BLOCK:
            case MAKE_DIRECTORIES:
            case LIST_FILES:
            case LIST_PATHS:
                {
                    fut = executeSynchronously(ses, cmd, msg, in);
                    break;
                }
            // Execute command asynchronously in pool.
            default:
                {
                    try {
                        final GridFutureAdapter<IgfsMessage> fut0 = new GridFutureAdapter<>();
                        pool.execute(new Runnable() {

                            @Override
                            public void run() {
                                try {
                                    fut0.onDone(execute(ses, cmd, msg, in));
                                } catch (Exception e) {
                                    fut0.onDone(e);
                                }
                            }
                        });
                        fut = fut0;
                    } catch (RejectedExecutionException ignored) {
                        fut = executeSynchronously(ses, cmd, msg, in);
                    }
                }
        }
        // Pack result object into response format.
        return fut;
    } catch (Exception e) {
        return new GridFinishedFuture<>(e);
    }
}
Also used : IgfsIpcCommand(org.apache.ignite.internal.igfs.common.IgfsIpcCommand) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) IgfsMessage(org.apache.ignite.internal.igfs.common.IgfsMessage) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) IgfsOutOfSpaceException(org.apache.ignite.igfs.IgfsOutOfSpaceException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IOException(java.io.IOException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 5 with GridFutureAdapter

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

the class GridClusterStateProcessor method changeGlobalState0.

/**
 */
private IgniteInternalFuture<?> changeGlobalState0(final boolean activate, BaselineTopology blt, boolean forceChangeBaselineTopology) {
    if (ctx.isDaemon() || ctx.clientNode()) {
        GridFutureAdapter<Void> fut = new GridFutureAdapter<>();
        sendComputeChangeGlobalState(activate, blt, forceChangeBaselineTopology, fut);
        return fut;
    }
    if (cacheProc.transactions().tx() != null || sharedCtx.lockedTopologyVersion(null) != null) {
        return new GridFinishedFuture<>(new IgniteCheckedException("Failed to " + prettyStr(activate) + " cluster (must invoke the method outside of an active transaction)."));
    }
    DiscoveryDataClusterState curState = globalState;
    if (!curState.transition() && curState.active() == activate && BaselineTopology.equals(curState.baselineTopology(), blt))
        return new GridFinishedFuture<>();
    GridChangeGlobalStateFuture startedFut = null;
    GridChangeGlobalStateFuture fut = stateChangeFut.get();
    while (fut == null || fut.isDone()) {
        fut = new GridChangeGlobalStateFuture(UUID.randomUUID(), activate, ctx);
        if (stateChangeFut.compareAndSet(null, fut)) {
            startedFut = fut;
            break;
        } else
            fut = stateChangeFut.get();
    }
    if (startedFut == null) {
        if (fut.activate != activate) {
            return new GridFinishedFuture<>(new IgniteCheckedException("Failed to " + prettyStr(activate) + ", because another state change operation is currently in progress: " + prettyStr(fut.activate)));
        } else
            return fut;
    }
    List<StoredCacheData> storedCfgs = null;
    if (activate && CU.isPersistenceEnabled(ctx.config())) {
        try {
            Map<String, StoredCacheData> cfgs = ctx.cache().context().pageStore().readCacheConfigurations();
            if (!F.isEmpty(cfgs))
                storedCfgs = new ArrayList<>(cfgs.values());
        } catch (IgniteCheckedException e) {
            U.error(log, "Failed to read stored cache configurations: " + e, e);
            startedFut.onDone(e);
            return startedFut;
        }
    }
    ChangeGlobalStateMessage msg = new ChangeGlobalStateMessage(startedFut.requestId, ctx.localNodeId(), storedCfgs, activate, blt, forceChangeBaselineTopology, System.currentTimeMillis());
    try {
        if (log.isInfoEnabled())
            U.log(log, "Sending " + prettyStr(activate) + " request with BaselineTopology " + blt);
        ctx.discovery().sendCustomEvent(msg);
        if (ctx.isStopping())
            startedFut.onDone(new IgniteCheckedException("Failed to execute " + prettyStr(activate) + " request, " + "node is stopping."));
    } catch (IgniteCheckedException e) {
        U.error(log, "Failed to send global state change request: " + activate, e);
        startedFut.onDone(e);
    }
    return wrapStateChangeFuture(startedFut, msg);
}
Also used : ArrayList(java.util.ArrayList) StoredCacheData(org.apache.ignite.internal.processors.cache.StoredCacheData) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter)

Aggregations

GridFutureAdapter (org.apache.ignite.internal.util.future.GridFutureAdapter)110 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)59 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)34 IgniteException (org.apache.ignite.IgniteException)23 ArrayList (java.util.ArrayList)21 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)21 List (java.util.List)20 UUID (java.util.UUID)20 ClusterNode (org.apache.ignite.cluster.ClusterNode)20 IgniteEx (org.apache.ignite.internal.IgniteEx)20 HashMap (java.util.HashMap)19 Map (java.util.Map)19 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)18 Test (org.junit.Test)18 Nullable (org.jetbrains.annotations.Nullable)17 Ignite (org.apache.ignite.Ignite)15 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)15 HashSet (java.util.HashSet)14 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)14 T2 (org.apache.ignite.internal.util.typedef.T2)13