Search in sources :

Example 66 with ClusterTopologyCheckedException

use of org.apache.ignite.internal.cluster.ClusterTopologyCheckedException in project ignite by apache.

the class GridCacheTxRecoveryFuture method prepare.

/**
 * Initializes future.
 */
@SuppressWarnings("ConstantConditions")
public void prepare() {
    if (nearTxCheck) {
        UUID nearNodeId = tx.eventNodeId();
        if (cctx.localNodeId().equals(nearNodeId)) {
            IgniteInternalFuture<Boolean> fut = cctx.tm().txCommitted(tx.nearXidVersion());
            fut.listen(new CI1<IgniteInternalFuture<Boolean>>() {

                @Override
                public void apply(IgniteInternalFuture<Boolean> fut) {
                    try {
                        onDone(fut.get());
                    } catch (IgniteCheckedException e) {
                        onDone(e);
                    }
                }
            });
        } else {
            MiniFuture fut = new MiniFuture(tx.eventNodeId());
            add(fut);
            GridCacheTxRecoveryRequest req = new GridCacheTxRecoveryRequest(tx, 0, true, futureId(), fut.futureId(), tx.activeCachesDeploymentEnabled());
            try {
                cctx.io().send(nearNodeId, req, tx.ioPolicy());
                if (msgLog.isDebugEnabled()) {
                    msgLog.debug("Recovery fut, sent request near tx [txId=" + tx.nearXidVersion() + ", dhtTxId=" + tx.xidVersion() + ", node=" + nearNodeId + ']');
                }
            } catch (ClusterTopologyCheckedException ignore) {
                fut.onNodeLeft(nearNodeId);
            } catch (IgniteCheckedException e) {
                if (msgLog.isDebugEnabled()) {
                    msgLog.debug("Recovery fut, failed to send request near tx [txId=" + tx.nearXidVersion() + ", dhtTxId=" + tx.xidVersion() + ", node=" + nearNodeId + ", err=" + e + ']');
                }
                fut.onError(e);
            }
            markInitialized();
        }
        return;
    }
    // First check transactions on local node.
    int locTxNum = nodeTransactions(cctx.localNodeId());
    if (locTxNum > 1) {
        IgniteInternalFuture<Boolean> fut = cctx.tm().txsPreparedOrCommitted(tx.nearXidVersion(), locTxNum);
        if (fut == null || fut.isDone()) {
            boolean prepared;
            try {
                prepared = fut == null ? true : fut.get();
            } catch (IgniteCheckedException e) {
                U.error(log, "Check prepared transaction future failed: " + e, e);
                prepared = false;
            }
            if (!prepared) {
                onDone(false);
                markInitialized();
                return;
            }
        } else {
            fut.listen(new CI1<IgniteInternalFuture<Boolean>>() {

                @Override
                public void apply(IgniteInternalFuture<Boolean> fut) {
                    boolean prepared;
                    try {
                        prepared = fut.get();
                    } catch (IgniteCheckedException e) {
                        U.error(log, "Check prepared transaction future failed: " + e, e);
                        prepared = false;
                    }
                    if (!prepared) {
                        onDone(false);
                        markInitialized();
                    } else
                        proceedPrepare();
                }
            });
            return;
        }
    }
    proceedPrepare();
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) UUID(java.util.UUID) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 67 with ClusterTopologyCheckedException

use of org.apache.ignite.internal.cluster.ClusterTopologyCheckedException in project ignite by apache.

the class DataStreamProcessor method localUpdate.

/**
 * @param nodeId Node id.
 * @param req Request.
 * @param updater Updater.
 * @param topic Topic.
 */
private void localUpdate(final UUID nodeId, final DataStreamerRequest req, final StreamReceiver<K, V> updater, final Object topic) {
    final boolean allowOverwrite = !(updater instanceof DataStreamerImpl.IsolatedUpdater);
    try {
        GridCacheAdapter cache = ctx.cache().internalCache(req.cacheName());
        if (cache == null) {
            throw new IgniteCheckedException("Cache not created or already destroyed: " + req.cacheName());
        }
        GridCacheContext cctx = cache.context();
        DataStreamerUpdateJob job = null;
        GridFutureAdapter waitFut = null;
        if (!allowOverwrite)
            cctx.topology().readLock();
        GridDhtTopologyFuture topWaitFut = null;
        try {
            Exception remapErr = null;
            AffinityTopologyVersion streamerFutTopVer = null;
            if (!allowOverwrite) {
                GridDhtTopologyFuture topFut = cctx.topologyVersionFuture();
                AffinityTopologyVersion topVer = topFut.isDone() ? topFut.topologyVersion() : topFut.initialVersion();
                if (topVer.compareTo(req.topologyVersion()) > 0) {
                    remapErr = new ClusterTopologyCheckedException("DataStreamer will retry " + "data transfer at stable topology [reqTop=" + req.topologyVersion() + ", topVer=" + topFut.initialVersion() + ", node=remote]");
                } else if (!topFut.isDone())
                    topWaitFut = topFut;
                else
                    streamerFutTopVer = topFut.topologyVersion();
            }
            if (remapErr != null) {
                sendResponse(nodeId, topic, req.requestId(), remapErr, req.forceLocalDeployment());
                return;
            } else if (topWaitFut == null) {
                job = new DataStreamerUpdateJob(ctx, log, req.cacheName(), req.entries(), req.ignoreDeploymentOwnership(), req.skipStore(), req.keepBinary(), updater);
                waitFut = allowOverwrite ? null : cctx.mvcc().addDataStreamerFuture(streamerFutTopVer);
            }
        } finally {
            if (!allowOverwrite)
                cctx.topology().readUnlock();
        }
        if (topWaitFut != null) {
            // Need call 'listen' after topology read lock is released.
            topWaitFut.listen(new IgniteInClosure<IgniteInternalFuture<AffinityTopologyVersion>>() {

                @Override
                public void apply(IgniteInternalFuture<AffinityTopologyVersion> e) {
                    localUpdate(nodeId, req, updater, topic);
                }
            });
            return;
        }
        try {
            job.call();
            sendResponse(nodeId, topic, req.requestId(), null, req.forceLocalDeployment());
        } finally {
            if (waitFut != null)
                waitFut.onDone();
        }
    } catch (Throwable e) {
        sendResponse(nodeId, topic, req.requestId(), e, req.forceLocalDeployment());
        if (e instanceof Error)
            throw (Error) e;
    }
}
Also used : GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) GridDhtTopologyFuture(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTopologyFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCacheAdapter(org.apache.ignite.internal.processors.cache.GridCacheAdapter) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 68 with ClusterTopologyCheckedException

use of org.apache.ignite.internal.cluster.ClusterTopologyCheckedException in project ignite by apache.

the class GridJobSiblingImpl method cancel.

/**
 * {@inheritDoc}
 */
@Override
public void cancel() {
    GridTaskSessionImpl ses = ctx.session().getSession(sesId);
    Collection<ClusterNode> nodes = ses == null ? ctx.discovery().remoteNodes() : ctx.discovery().nodes(ses.getTopology());
    for (ClusterNode node : nodes) {
        if (!ctx.localNodeId().equals(node.id())) {
            try {
                ctx.io().sendToGridTopic(node, TOPIC_JOB_CANCEL, new GridJobCancelRequest(sesId, jobId), SYSTEM_POOL);
            } catch (ClusterTopologyCheckedException e) {
                IgniteLogger log = ctx.log(GridJobSiblingImpl.class);
                if (log.isDebugEnabled())
                    log.debug("Failed to send cancel request, node left [nodeId=" + node.id() + ", ses=" + ses + ']');
            } catch (IgniteCheckedException e) {
                // Avoid stack trace for left nodes.
                if (ctx.discovery().node(node.id()) != null && ctx.discovery().pingNodeNoError(node.id()))
                    U.error(ctx.log(GridJobSiblingImpl.class), "Failed to send cancel request to node " + "[nodeId=" + node.id() + ", ses=" + ses + ']', e);
            }
        }
    }
    // Cancel local jobs directly.
    ctx.job().cancelJob(sesId, jobId, false);
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteLogger(org.apache.ignite.IgniteLogger) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 69 with ClusterTopologyCheckedException

use of org.apache.ignite.internal.cluster.ClusterTopologyCheckedException in project ignite by apache.

the class GridServiceProcessor method reassign.

/**
 * Reassigns service to nodes.
 *
 * @param dep Service deployment.
 * @param topVer Topology version.
 * @throws IgniteCheckedException If failed.
 */
private void reassign(GridServiceDeployment dep, AffinityTopologyVersion topVer) throws IgniteCheckedException {
    IgniteInternalCache<Object, Object> cache = serviceCache();
    ServiceConfiguration cfg = dep.configuration();
    Object nodeFilter = cfg.getNodeFilter();
    if (nodeFilter != null)
        ctx.resource().injectGeneric(nodeFilter);
    int totalCnt = cfg.getTotalCount();
    int maxPerNodeCnt = cfg.getMaxPerNodeCount();
    String cacheName = cfg.getCacheName();
    Object affKey = cfg.getAffinityKey();
    while (true) {
        GridServiceAssignments assigns = new GridServiceAssignments(cfg, dep.nodeId(), topVer.topologyVersion());
        Collection<ClusterNode> nodes;
        // Call node filter outside of transaction.
        if (affKey == null) {
            nodes = ctx.discovery().nodes(topVer);
            if (assigns.nodeFilter() != null) {
                Collection<ClusterNode> nodes0 = new ArrayList<>();
                for (ClusterNode node : nodes) {
                    if (assigns.nodeFilter().apply(node))
                        nodes0.add(node);
                }
                nodes = nodes0;
            }
        } else
            nodes = null;
        try (GridNearTxLocal tx = cache.txStartEx(PESSIMISTIC, REPEATABLE_READ)) {
            GridServiceAssignmentsKey key = new GridServiceAssignmentsKey(cfg.getName());
            GridServiceAssignments oldAssigns = (GridServiceAssignments) cache.get(key);
            Map<UUID, Integer> cnts = new HashMap<>();
            if (affKey != null) {
                ClusterNode n = ctx.affinity().mapKeyToNode(cacheName, affKey, topVer);
                if (n != null) {
                    int cnt = maxPerNodeCnt == 0 ? totalCnt == 0 ? 1 : totalCnt : maxPerNodeCnt;
                    cnts.put(n.id(), cnt);
                }
            } else {
                if (!nodes.isEmpty()) {
                    int size = nodes.size();
                    int perNodeCnt = totalCnt != 0 ? totalCnt / size : maxPerNodeCnt;
                    int remainder = totalCnt != 0 ? totalCnt % size : 0;
                    if (perNodeCnt >= maxPerNodeCnt && maxPerNodeCnt != 0) {
                        perNodeCnt = maxPerNodeCnt;
                        remainder = 0;
                    }
                    for (ClusterNode n : nodes) cnts.put(n.id(), perNodeCnt);
                    assert perNodeCnt >= 0;
                    assert remainder >= 0;
                    if (remainder > 0) {
                        int cnt = perNodeCnt + 1;
                        if (oldAssigns != null) {
                            Collection<UUID> used = new HashSet<>();
                            // Avoid redundant moving of services.
                            for (Map.Entry<UUID, Integer> e : oldAssigns.assigns().entrySet()) {
                                // Do not assign services to left nodes.
                                if (ctx.discovery().node(e.getKey()) == null)
                                    continue;
                                // If old count and new count match, then reuse the assignment.
                                if (e.getValue() == cnt) {
                                    cnts.put(e.getKey(), cnt);
                                    used.add(e.getKey());
                                    if (--remainder == 0)
                                        break;
                                }
                            }
                            if (remainder > 0) {
                                List<Map.Entry<UUID, Integer>> entries = new ArrayList<>(cnts.entrySet());
                                // Randomize.
                                Collections.shuffle(entries);
                                for (Map.Entry<UUID, Integer> e : entries) {
                                    // Assign only the ones that have not been reused from previous assignments.
                                    if (!used.contains(e.getKey())) {
                                        if (e.getValue() < maxPerNodeCnt || maxPerNodeCnt == 0) {
                                            e.setValue(e.getValue() + 1);
                                            if (--remainder == 0)
                                                break;
                                        }
                                    }
                                }
                            }
                        } else {
                            List<Map.Entry<UUID, Integer>> entries = new ArrayList<>(cnts.entrySet());
                            // Randomize.
                            Collections.shuffle(entries);
                            for (Map.Entry<UUID, Integer> e : entries) {
                                e.setValue(e.getValue() + 1);
                                if (--remainder == 0)
                                    break;
                            }
                        }
                    }
                }
            }
            assigns.assigns(cnts);
            cache.put(key, assigns);
            tx.commit();
            break;
        } catch (ClusterTopologyCheckedException e) {
            if (log.isDebugEnabled())
                log.debug("Topology changed while reassigning (will retry): " + e.getMessage());
            U.sleep(10);
        }
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) IgniteSystemProperties.getString(org.apache.ignite.IgniteSystemProperties.getString) ServiceConfiguration(org.apache.ignite.services.ServiceConfiguration) GridTimeoutObject(org.apache.ignite.internal.processors.timeout.GridTimeoutObject) UUID(java.util.UUID) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 70 with ClusterTopologyCheckedException

use of org.apache.ignite.internal.cluster.ClusterTopologyCheckedException in project ignite by apache.

the class GridServiceProxy method invokeMethod.

/**
 * Invoek the method.
 *
 * @param mtd Method.
 * @param args Arugments.
 * @return Result.
 */
@SuppressWarnings("BusyWait")
public Object invokeMethod(final Method mtd, final Object[] args) {
    if (U.isHashCodeMethod(mtd))
        return System.identityHashCode(proxy);
    else if (U.isEqualsMethod(mtd))
        return proxy == args[0];
    else if (U.isToStringMethod(mtd))
        return GridServiceProxy.class.getSimpleName() + " [name=" + name + ", sticky=" + sticky + ']';
    ctx.gateway().readLock();
    try {
        final long startTime = U.currentTimeMillis();
        while (true) {
            ClusterNode node = null;
            try {
                node = nodeForService(name, sticky);
                if (node == null)
                    throw new IgniteException("Failed to find deployed service: " + name);
                // If service is deployed locally, then execute locally.
                if (node.isLocal()) {
                    ServiceContextImpl svcCtx = ctx.service().serviceContext(name);
                    if (svcCtx != null) {
                        Service svc = svcCtx.service();
                        if (svc != null)
                            return mtd.invoke(svc, args);
                    }
                } else {
                    if (node.version().compareTo(SVC_POOL_SINCE_VER) >= 0)
                        ctx.task().setThreadContext(TC_IO_POLICY, GridIoPolicy.SERVICE_POOL);
                    // Execute service remotely.
                    return ctx.closure().callAsyncNoFailover(GridClosureCallMode.BROADCAST, new ServiceProxyCallable(mtd.getName(), name, mtd.getParameterTypes(), args), Collections.singleton(node), false, waitTimeout, true).get();
                }
            } catch (GridServiceNotFoundException | ClusterTopologyCheckedException e) {
                if (log.isDebugEnabled())
                    log.debug("Service was not found or topology changed (will retry): " + e.getMessage());
            } catch (RuntimeException | Error e) {
                throw e;
            } catch (IgniteCheckedException e) {
                throw U.convertException(e);
            } catch (Exception e) {
                throw new IgniteException(e);
            }
            // If we are here, that means that service was not found
            // or topology was changed. In this case, we erase the
            // previous sticky node and try again.
            rmtNode.compareAndSet(node, null);
            // Add sleep between retries to avoid busy-wait loops.
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throw new IgniteException(e);
            }
            if (waitTimeout > 0 && U.currentTimeMillis() - startTime >= waitTimeout)
                throw new IgniteException("Service acquire timeout was reached, stopping. [timeout=" + waitTimeout + "]");
        }
    } finally {
        ctx.gateway().readUnlock();
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) Service(org.apache.ignite.services.Service) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Aggregations

ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)79 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)59 ClusterNode (org.apache.ignite.cluster.ClusterNode)49 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)19 UUID (java.util.UUID)17 Map (java.util.Map)16 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)16 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)14 GridCacheEntryRemovedException (org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)13 HashMap (java.util.HashMap)12 GridCacheVersion (org.apache.ignite.internal.processors.cache.version.GridCacheVersion)12 ArrayList (java.util.ArrayList)11 ClusterTopologyServerNotFoundException (org.apache.ignite.internal.cluster.ClusterTopologyServerNotFoundException)10 Collection (java.util.Collection)8 IgniteException (org.apache.ignite.IgniteException)8 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)8 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)7 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)7 Event (org.apache.ignite.events.Event)6 IgniteClientDisconnectedCheckedException (org.apache.ignite.internal.IgniteClientDisconnectedCheckedException)6