Search in sources :

Example 1 with GridCompoundFuture

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

the class IgniteKernal method onReconnected.

/**
     * @param clusterRestarted {@code True} if all cluster nodes restarted while client was disconnected.
     */
@SuppressWarnings("unchecked")
public void onReconnected(final boolean clusterRestarted) {
    Throwable err = null;
    try {
        ctx.disconnected(false);
        GridCompoundFuture curReconnectFut = reconnectState.curReconnectFut = new GridCompoundFuture<>();
        reconnectState.reconnectDone = new GridFutureAdapter<>();
        for (GridComponent comp : ctx.components()) {
            IgniteInternalFuture<?> fut = comp.onReconnected(clusterRestarted);
            if (fut != null)
                curReconnectFut.add(fut);
        }
        curReconnectFut.add(ctx.cache().context().exchange().reconnectExchangeFuture());
        curReconnectFut.markInitialized();
        final GridFutureAdapter reconnectDone = reconnectState.reconnectDone;
        curReconnectFut.listen(new CI1<IgniteInternalFuture<?>>() {

            @Override
            public void apply(IgniteInternalFuture<?> fut) {
                try {
                    Object res = fut.get();
                    if (res == STOP_RECONNECT)
                        return;
                    ctx.gateway().onReconnected();
                    reconnectState.firstReconnectFut.onDone();
                } catch (IgniteCheckedException e) {
                    if (!X.hasCause(e, IgniteNeedReconnectException.class, IgniteClientDisconnectedCheckedException.class)) {
                        U.error(log, "Failed to reconnect, will stop node.", e);
                        reconnectState.firstReconnectFut.onDone(e);
                        close();
                    } else {
                        assert ctx.discovery().reconnectSupported();
                        U.error(log, "Failed to finish reconnect, will retry [locNodeId=" + ctx.localNodeId() + ", err=" + e.getMessage() + ']');
                    }
                } finally {
                    reconnectDone.onDone();
                }
            }
        });
    } catch (IgniteCheckedException e) {
        err = e;
    } catch (Throwable e) {
        err = e;
        if (e instanceof Error)
            throw e;
    }
    if (err != null) {
        U.error(log, "Failed to reconnect, will stop node", err);
        close();
    }
}
Also used : GridCompoundFuture(org.apache.ignite.internal.util.future.GridCompoundFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter)

Example 2 with GridCompoundFuture

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

the class IgniteClusterImpl method startNodesAsync0.

/**
     * @param hosts Startup parameters.
     * @param dflts Default values.
     * @param restart Whether to stop existing nodes
     * @param timeout Connection timeout in milliseconds.
     * @param maxConn Number of parallel SSH connections to one host.
     * @return Future with results.
     * @see IgniteCluster#startNodes(java.util.Collection, java.util.Map, boolean, int, int)
     */
IgniteInternalFuture<Collection<ClusterStartNodeResult>> startNodesAsync0(Collection<Map<String, Object>> hosts, @Nullable Map<String, Object> dflts, boolean restart, int timeout, int maxConn) {
    A.notNull(hosts, "hosts");
    guard();
    try {
        IgniteSshHelper sshHelper = IgniteComponentType.SSH.create(false);
        Map<String, Collection<IgniteRemoteStartSpecification>> specsMap = specifications(hosts, dflts);
        Map<String, ConcurrentLinkedQueue<StartNodeCallable>> runMap = new HashMap<>();
        int nodeCallCnt = 0;
        for (String host : specsMap.keySet()) {
            InetAddress addr;
            try {
                addr = InetAddress.getByName(host);
            } catch (UnknownHostException e) {
                throw new IgniteCheckedException("Invalid host name: " + host, e);
            }
            Collection<? extends ClusterNode> neighbors = null;
            if (addr.isLoopbackAddress())
                neighbors = neighbors();
            else {
                for (Collection<ClusterNode> p : U.neighborhood(nodes()).values()) {
                    ClusterNode node = F.first(p);
                    if (node.<String>attribute(ATTR_IPS).contains(addr.getHostAddress())) {
                        neighbors = p;
                        break;
                    }
                }
            }
            int startIdx = 1;
            if (neighbors != null) {
                if (restart && !neighbors.isEmpty()) {
                    try {
                        ctx.grid().compute(forNodes(neighbors)).execute(IgniteKillTask.class, false);
                    } catch (ClusterGroupEmptyException ignored) {
                    // No-op, nothing to restart.
                    }
                } else
                    startIdx = neighbors.size() + 1;
            }
            ConcurrentLinkedQueue<StartNodeCallable> nodeRuns = new ConcurrentLinkedQueue<>();
            runMap.put(host, nodeRuns);
            for (IgniteRemoteStartSpecification spec : specsMap.get(host)) {
                assert spec.host().equals(host);
                for (int i = startIdx; i <= spec.nodes(); i++) {
                    nodeRuns.add(sshHelper.nodeStartCallable(spec, timeout));
                    nodeCallCnt++;
                }
            }
        }
        // If there is nothing to start, return finished future with empty result.
        if (nodeCallCnt == 0)
            return new GridFinishedFuture<Collection<ClusterStartNodeResult>>(Collections.<ClusterStartNodeResult>emptyList());
        // Exceeding max line width for readability.
        GridCompoundFuture<ClusterStartNodeResult, Collection<ClusterStartNodeResult>> fut = new GridCompoundFuture<>(CU.<ClusterStartNodeResult>objectsReducer());
        AtomicInteger cnt = new AtomicInteger(nodeCallCnt);
        // Limit maximum simultaneous connection number per host.
        for (ConcurrentLinkedQueue<StartNodeCallable> queue : runMap.values()) {
            for (int i = 0; i < maxConn; i++) {
                if (!runNextNodeCallable(queue, fut, cnt))
                    break;
            }
        }
        return fut;
    } catch (IgniteCheckedException e) {
        return new GridFinishedFuture<>(e);
    } finally {
        unguard();
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteSshHelper(org.apache.ignite.internal.util.nodestart.IgniteSshHelper) UnknownHostException(java.net.UnknownHostException) HashMap(java.util.HashMap) ClusterGroupEmptyException(org.apache.ignite.cluster.ClusterGroupEmptyException) ClusterStartNodeResult(org.apache.ignite.cluster.ClusterStartNodeResult) GridCompoundFuture(org.apache.ignite.internal.util.future.GridCompoundFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteRemoteStartSpecification(org.apache.ignite.internal.util.nodestart.IgniteRemoteStartSpecification) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Collection(java.util.Collection) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) InetAddress(java.net.InetAddress) StartNodeCallable(org.apache.ignite.internal.util.nodestart.StartNodeCallable)

Example 3 with GridCompoundFuture

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

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

the class GridCacheProcessor method resetCacheState.

/**
     * Resets cache state after the cache has been moved to recovery state.
     *
     * @param cacheNames Cache names.
     * @return Future that will be completed when state is changed for all caches.
     */
public IgniteInternalFuture<?> resetCacheState(Collection<String> cacheNames) {
    checkEmptyTransactions();
    if (F.isEmpty(cacheNames))
        cacheNames = cachesInfo.registeredCaches().keySet();
    Collection<DynamicCacheChangeRequest> reqs = new ArrayList<>(cacheNames.size());
    for (String cacheName : cacheNames) {
        DynamicCacheDescriptor desc = cacheDescriptor(cacheName);
        if (desc == null) {
            U.warn(log, "Failed to find cache for reset lost partition request, cache does not exist: " + cacheName);
            continue;
        }
        DynamicCacheChangeRequest req = DynamicCacheChangeRequest.resetLostPartitions(ctx, cacheName);
        reqs.add(req);
    }
    GridCompoundFuture fut = new GridCompoundFuture();
    for (DynamicCacheStartFuture f : initiateCacheChanges(reqs, false)) fut.add(f);
    fut.markInitialized();
    return fut;
}
Also used : ArrayList(java.util.ArrayList) GridCompoundFuture(org.apache.ignite.internal.util.future.GridCompoundFuture)

Example 5 with GridCompoundFuture

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

the class IgniteTxHandler method processDhtTxPrepareRequest.

/**
     * @param nodeId Sender node ID.
     * @param req Request.
     */
private void processDhtTxPrepareRequest(final UUID nodeId, final GridDhtTxPrepareRequest req) {
    if (txPrepareMsgLog.isDebugEnabled()) {
        txPrepareMsgLog.debug("Received dht prepare request [txId=" + req.nearXidVersion() + ", dhtTxId=" + req.version() + ", node=" + nodeId + ']');
    }
    assert nodeId != null;
    assert req != null;
    assert req.transactionNodes() != null;
    GridDhtTxRemote dhtTx = null;
    GridNearTxRemote nearTx = null;
    GridDhtTxPrepareResponse res;
    try {
        res = new GridDhtTxPrepareResponse(req.partition(), req.version(), req.futureId(), req.miniId(), req.deployInfo() != null);
        // Start near transaction first.
        nearTx = !F.isEmpty(req.nearWrites()) ? startNearRemoteTx(ctx.deploy().globalLoader(), nodeId, req) : null;
        dhtTx = startRemoteTx(nodeId, req, res);
        // Set evicted keys from near transaction.
        if (nearTx != null)
            res.nearEvicted(nearTx.evicted());
        if (dhtTx != null)
            req.txState(dhtTx.txState());
        else if (nearTx != null)
            req.txState(nearTx.txState());
        if (dhtTx != null && !F.isEmpty(dhtTx.invalidPartitions()))
            res.invalidPartitionsByCacheId(dhtTx.invalidPartitions());
        if (req.onePhaseCommit()) {
            assert req.last();
            if (dhtTx != null) {
                dhtTx.onePhaseCommit(true);
                dhtTx.needReturnValue(req.needReturnValue());
                finish(dhtTx, req);
            }
            if (nearTx != null) {
                nearTx.onePhaseCommit(true);
                finish(nearTx, req);
            }
        }
    } catch (IgniteCheckedException e) {
        if (e instanceof IgniteTxRollbackCheckedException)
            U.error(log, "Transaction was rolled back before prepare completed: " + req, e);
        else if (e instanceof IgniteTxOptimisticCheckedException) {
            if (log.isDebugEnabled())
                log.debug("Optimistic failure for remote transaction (will rollback): " + req);
        } else if (e instanceof IgniteTxHeuristicCheckedException) {
            U.warn(log, "Failed to commit transaction (all transaction entries were invalidated): " + CU.txString(dhtTx));
        } else
            U.error(log, "Failed to process prepare request: " + req, e);
        if (nearTx != null)
            nearTx.rollbackRemoteTx();
        res = new GridDhtTxPrepareResponse(req.partition(), req.version(), req.futureId(), req.miniId(), e, req.deployInfo() != null);
    }
    if (req.onePhaseCommit()) {
        IgniteInternalFuture completeFut;
        IgniteInternalFuture<IgniteInternalTx> dhtFin = dhtTx == null ? null : dhtTx.done() ? null : dhtTx.finishFuture();
        final IgniteInternalFuture<IgniteInternalTx> nearFin = nearTx == null ? null : nearTx.done() ? null : nearTx.finishFuture();
        if (dhtFin != null && nearFin != null) {
            GridCompoundFuture fut = new GridCompoundFuture();
            fut.add(dhtFin);
            fut.add(nearFin);
            fut.markInitialized();
            completeFut = fut;
        } else
            completeFut = dhtFin != null ? dhtFin : nearFin;
        if (completeFut != null) {
            final GridDhtTxPrepareResponse res0 = res;
            final GridDhtTxRemote dhtTx0 = dhtTx;
            final GridNearTxRemote nearTx0 = nearTx;
            completeFut.listen(new CI1<IgniteInternalFuture<IgniteInternalTx>>() {

                @Override
                public void apply(IgniteInternalFuture<IgniteInternalTx> fut) {
                    sendReply(nodeId, req, res0, dhtTx0, nearTx0);
                }
            });
        } else
            sendReply(nodeId, req, res, dhtTx, nearTx);
    } else
        sendReply(nodeId, req, res, dhtTx, nearTx);
    assert req.txState() != null || res.error() != null || (ctx.tm().tx(req.version()) == null && ctx.tm().nearTx(req.version()) == null);
}
Also used : GridDhtTxRemote(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxRemote) IgniteTxRollbackCheckedException(org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException) GridDhtTxPrepareResponse(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxPrepareResponse) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteTxHeuristicCheckedException(org.apache.ignite.internal.transactions.IgniteTxHeuristicCheckedException) GridNearTxRemote(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxRemote) IgniteTxOptimisticCheckedException(org.apache.ignite.internal.transactions.IgniteTxOptimisticCheckedException) GridCompoundFuture(org.apache.ignite.internal.util.future.GridCompoundFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException)

Aggregations

GridCompoundFuture (org.apache.ignite.internal.util.future.GridCompoundFuture)15 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)6 ArrayList (java.util.ArrayList)3 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)3 GridFutureAdapter (org.apache.ignite.internal.util.future.GridFutureAdapter)3 ClusterNode (org.apache.ignite.cluster.ClusterNode)2 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)2 GridDhtTxRemote (org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxRemote)2 GridNearTxRemote (org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxRemote)2 InetAddress (java.net.InetAddress)1 UnknownHostException (java.net.UnknownHostException)1 List (java.util.List)1 UUID (java.util.UUID)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1