Search in sources :

Example 11 with CacheGroupContext

use of org.apache.ignite.internal.processors.cache.CacheGroupContext in project ignite by apache.

the class GridDhtPartitionsExchangeFuture method updatePartitionSingleMap.

/**
 * Updates partition map in all caches.
 *
 * @param nodeId Node message received from.
 * @param msg Partitions single message.
 */
private void updatePartitionSingleMap(UUID nodeId, GridDhtPartitionsSingleMessage msg) {
    msgs.put(nodeId, msg);
    for (Map.Entry<Integer, GridDhtPartitionMap> entry : msg.partitions().entrySet()) {
        Integer grpId = entry.getKey();
        CacheGroupContext grp = cctx.cache().cacheGroup(grpId);
        GridDhtPartitionTopology top = grp != null ? grp.topology() : cctx.exchange().clientTopology(grpId, events().discoveryCache());
        top.update(exchId, entry.getValue(), false);
    }
}
Also used : GridDhtPartitionTopology(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtPartitionTopology) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 12 with CacheGroupContext

use of org.apache.ignite.internal.processors.cache.CacheGroupContext in project ignite by apache.

the class GridDhtPartitionsExchangeFuture method onDone.

/**
 * {@inheritDoc}
 */
@Override
public boolean onDone(@Nullable AffinityTopologyVersion res, @Nullable Throwable err) {
    if (isDone() || !done.compareAndSet(false, true))
        return false;
    if (log.isInfoEnabled()) {
        log.info("Finish exchange future [startVer=" + initialVersion() + ", resVer=" + res + ", err=" + err + ']');
    }
    assert res != null || err != null;
    if (err == null && !cctx.kernalContext().clientNode() && (serverNodeDiscoveryEvent() || affChangeMsg != null)) {
        for (GridCacheContext cacheCtx : cctx.cacheContexts()) {
            if (!cacheCtx.affinityNode() || cacheCtx.isLocal())
                continue;
            cacheCtx.continuousQueries().flushBackupQueue(res);
        }
    }
    if (err == null) {
        if (centralizedAff || forceAffReassignment) {
            assert !exchCtx.mergeExchanges();
            for (CacheGroupContext grp : cctx.cache().cacheGroups()) {
                if (grp.isLocal())
                    continue;
                boolean needRefresh = false;
                try {
                    needRefresh = grp.topology().initPartitionsWhenAffinityReady(res, this);
                } catch (IgniteInterruptedCheckedException e) {
                    U.error(log, "Failed to initialize partitions.", e);
                }
                if (needRefresh)
                    cctx.exchange().refreshPartitions();
            }
        }
        for (GridCacheContext cacheCtx : cctx.cacheContexts()) {
            GridCacheContext drCacheCtx = cacheCtx.isNear() ? cacheCtx.near().dht().context() : cacheCtx;
            if (drCacheCtx.isDrEnabled()) {
                try {
                    drCacheCtx.dr().onExchange(res, exchId.isLeft());
                } catch (IgniteCheckedException e) {
                    U.error(log, "Failed to notify DR: " + e, e);
                }
            }
        }
        if (serverNodeDiscoveryEvent())
            detectLostPartitions(res);
        Map<Integer, CacheValidation> m = U.newHashMap(cctx.cache().cacheGroups().size());
        for (CacheGroupContext grp : cctx.cache().cacheGroups()) m.put(grp.groupId(), validateCacheGroup(grp, events().lastEvent().topologyNodes()));
        grpValidRes = m;
    }
    if (!cctx.localNode().isClient())
        tryToPerformLocalSnapshotOperation();
    cctx.cache().onExchangeDone(initialVersion(), exchActions, err);
    cctx.exchange().onExchangeDone(res, initialVersion(), err);
    cctx.kernalContext().authentication().onActivate();
    if (exchActions != null && err == null)
        exchActions.completeRequestFutures(cctx);
    if (stateChangeExchange() && err == null)
        cctx.kernalContext().state().onStateChangeExchangeDone(exchActions.stateChangeRequest());
    Map<T2<Integer, Integer>, Long> localReserved = partHistSuppliers.getReservations(cctx.localNodeId());
    if (localReserved != null) {
        for (Map.Entry<T2<Integer, Integer>, Long> e : localReserved.entrySet()) {
            boolean success = cctx.database().reserveHistoryForPreloading(e.getKey().get1(), e.getKey().get2(), e.getValue());
            if (!success) {
                // TODO: how to handle?
                err = new IgniteCheckedException("Could not reserve history");
            }
        }
    }
    cctx.database().releaseHistoryForExchange();
    cctx.database().rebuildIndexesIfNeeded(this);
    if (err == null) {
        for (CacheGroupContext grp : cctx.cache().cacheGroups()) {
            if (!grp.isLocal())
                grp.topology().onExchangeDone(this, grp.affinity().readyAffinity(res), false);
        }
    }
    if (super.onDone(res, err)) {
        if (log.isDebugEnabled())
            log.debug("Completed partition exchange [localNode=" + cctx.localNodeId() + ", exchange= " + this + ", durationFromInit=" + (U.currentTimeMillis() - initTs) + ']');
        initFut.onDone(err == null);
        if (exchCtx != null && exchCtx.events().hasServerLeft()) {
            ExchangeDiscoveryEvents evts = exchCtx.events();
            for (DiscoveryEvent evt : exchCtx.events().events()) {
                if (serverLeftEvent(evt)) {
                    for (CacheGroupContext grp : cctx.cache().cacheGroups()) grp.affinityFunction().removeNode(evt.eventNode().id());
                }
            }
        }
        exchActions = null;
        if (firstDiscoEvt instanceof DiscoveryCustomEvent)
            ((DiscoveryCustomEvent) firstDiscoEvt).customMessage(null);
        if (err == null) {
            cctx.exchange().lastFinishedFuture(this);
            if (exchCtx != null && (exchCtx.events().hasServerLeft() || exchCtx.events().hasServerJoin())) {
                ExchangeDiscoveryEvents evts = exchCtx.events();
                for (DiscoveryEvent evt : exchCtx.events().events()) {
                    if (serverLeftEvent(evt) || serverJoinEvent(evt))
                        logExchange(evt);
                }
            }
        }
        return true;
    }
    return false;
}
Also used : GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) ExchangeDiscoveryEvents(org.apache.ignite.internal.processors.cache.ExchangeDiscoveryEvents) DiscoveryCustomEvent(org.apache.ignite.internal.events.DiscoveryCustomEvent) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteSystemProperties.getLong(org.apache.ignite.IgniteSystemProperties.getLong) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) T2(org.apache.ignite.internal.util.typedef.T2)

Example 13 with CacheGroupContext

use of org.apache.ignite.internal.processors.cache.CacheGroupContext in project ignite by apache.

the class GridDhtPartitionsExchangeFuture method clientOnlyExchange.

/**
 * @throws IgniteCheckedException If failed.
 */
private void clientOnlyExchange() throws IgniteCheckedException {
    if (crd != null) {
        assert !crd.isLocal() : crd;
        if (!centralizedAff)
            sendLocalPartitions(crd);
        initDone();
        return;
    } else {
        if (centralizedAff) {
            // Last server node failed.
            for (CacheGroupContext grp : cctx.cache().cacheGroups()) {
                GridAffinityAssignmentCache aff = grp.affinity();
                aff.initialize(initialVersion(), aff.idealAssignment());
            }
        } else
            onAllServersLeft();
    }
    onDone(initialVersion());
}
Also used : GridAffinityAssignmentCache(org.apache.ignite.internal.processors.affinity.GridAffinityAssignmentCache) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext)

Example 14 with CacheGroupContext

use of org.apache.ignite.internal.processors.cache.CacheGroupContext in project ignite by apache.

the class GridDhtPartitionsExchangeFuture method assignPartitionsStates.

/**
 */
private void assignPartitionsStates() {
    for (Map.Entry<Integer, CacheGroupDescriptor> e : cctx.affinity().cacheGroups().entrySet()) {
        CacheGroupDescriptor grpDesc = e.getValue();
        if (grpDesc.config().getCacheMode() == CacheMode.LOCAL)
            continue;
        if (!CU.isPersistentCache(grpDesc.config(), cctx.gridConfig().getDataStorageConfiguration()))
            continue;
        CacheGroupContext grpCtx = cctx.cache().cacheGroup(e.getKey());
        GridDhtPartitionTopology top = grpCtx != null ? grpCtx.topology() : cctx.exchange().clientTopology(e.getKey(), events().discoveryCache());
        assignPartitionStates(top);
    }
}
Also used : GridDhtPartitionTopology(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtPartitionTopology) CacheGroupDescriptor(org.apache.ignite.internal.processors.cache.CacheGroupDescriptor) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 15 with CacheGroupContext

use of org.apache.ignite.internal.processors.cache.CacheGroupContext in project ignite by apache.

the class GridDhtPartitionsExchangeFuture method finishExchangeOnCoordinator.

/**
 * @param sndResNodes Additional nodes to send finish message to.
 */
private void finishExchangeOnCoordinator(@Nullable Collection<ClusterNode> sndResNodes) {
    try {
        AffinityTopologyVersion resTopVer = exchCtx.events().topologyVersion();
        if (log.isInfoEnabled()) {
            log.info("finishExchangeOnCoordinator [topVer=" + initialVersion() + ", resVer=" + resTopVer + ']');
        }
        Map<Integer, CacheGroupAffinityMessage> idealAffDiff = null;
        if (exchCtx.mergeExchanges()) {
            synchronized (mux) {
                if (mergedJoinExchMsgs != null) {
                    for (Map.Entry<UUID, GridDhtPartitionsSingleMessage> e : mergedJoinExchMsgs.entrySet()) {
                        msgs.put(e.getKey(), e.getValue());
                        updatePartitionSingleMap(e.getKey(), e.getValue());
                    }
                }
            }
            assert exchCtx.events().hasServerJoin() || exchCtx.events().hasServerLeft();
            exchCtx.events().processEvents(this);
            if (exchCtx.events().hasServerLeft())
                idealAffDiff = cctx.affinity().onServerLeftWithExchangeMergeProtocol(this);
            else
                cctx.affinity().onServerJoinWithExchangeMergeProtocol(this, true);
            for (CacheGroupDescriptor desc : cctx.affinity().cacheGroups().values()) {
                if (desc.config().getCacheMode() == CacheMode.LOCAL)
                    continue;
                CacheGroupContext grp = cctx.cache().cacheGroup(desc.groupId());
                GridDhtPartitionTopology top = grp != null ? grp.topology() : cctx.exchange().clientTopology(desc.groupId(), events().discoveryCache());
                top.beforeExchange(this, true, true);
            }
        }
        Map<Integer, CacheGroupAffinityMessage> joinedNodeAff = null;
        for (Map.Entry<UUID, GridDhtPartitionsSingleMessage> e : msgs.entrySet()) {
            GridDhtPartitionsSingleMessage msg = e.getValue();
            // Apply update counters after all single messages are received.
            for (Map.Entry<Integer, GridDhtPartitionMap> entry : msg.partitions().entrySet()) {
                Integer grpId = entry.getKey();
                CacheGroupContext grp = cctx.cache().cacheGroup(grpId);
                GridDhtPartitionTopology top = grp != null ? grp.topology() : cctx.exchange().clientTopology(grpId, events().discoveryCache());
                CachePartitionPartialCountersMap cntrs = msg.partitionUpdateCounters(grpId, top.partitions());
                if (cntrs != null)
                    top.collectUpdateCounters(cntrs);
            }
            Collection<Integer> affReq = msg.cacheGroupsAffinityRequest();
            if (affReq != null) {
                joinedNodeAff = CacheGroupAffinityMessage.createAffinityMessages(cctx, resTopVer, affReq, joinedNodeAff);
            }
        }
        if (firstDiscoEvt.type() == EVT_DISCOVERY_CUSTOM_EVT) {
            assert firstDiscoEvt instanceof DiscoveryCustomEvent;
            if (activateCluster() || changedBaseline())
                assignPartitionsStates();
            DiscoveryCustomMessage discoveryCustomMessage = ((DiscoveryCustomEvent) firstDiscoEvt).customMessage();
            if (discoveryCustomMessage instanceof DynamicCacheChangeBatch) {
                if (exchActions != null) {
                    assignPartitionsStates();
                    Set<String> caches = exchActions.cachesToResetLostPartitions();
                    if (!F.isEmpty(caches))
                        resetLostPartitions(caches);
                }
            } else if (discoveryCustomMessage instanceof SnapshotDiscoveryMessage && ((SnapshotDiscoveryMessage) discoveryCustomMessage).needAssignPartitions())
                assignPartitionsStates();
        } else {
            if (exchCtx.events().hasServerJoin())
                assignPartitionsStates();
            if (exchCtx.events().hasServerLeft())
                detectLostPartitions(resTopVer);
        }
        if (!exchCtx.mergeExchanges() && forceAffReassignment)
            idealAffDiff = cctx.affinity().onCustomEventWithEnforcedAffinityReassignment(this);
        for (CacheGroupContext grpCtx : cctx.cache().cacheGroups()) {
            if (!grpCtx.isLocal())
                grpCtx.topology().applyUpdateCounters();
        }
        updateLastVersion(cctx.versions().last());
        cctx.versions().onExchange(lastVer.get().order());
        IgniteProductVersion minVer = exchCtx.events().discoveryCache().minimumNodeVersion();
        GridDhtPartitionsFullMessage msg = createPartitionsMessage(true, minVer.compareToIgnoreTimestamp(PARTIAL_COUNTERS_MAP_SINCE) >= 0);
        if (exchCtx.mergeExchanges()) {
            assert !centralizedAff;
            msg.resultTopologyVersion(resTopVer);
            if (exchCtx.events().hasServerLeft())
                msg.idealAffinityDiff(idealAffDiff);
        } else if (forceAffReassignment)
            msg.idealAffinityDiff(idealAffDiff);
        msg.prepareMarshal(cctx);
        synchronized (mux) {
            finishState = new FinishState(crd.id(), resTopVer, msg);
            state = ExchangeLocalState.DONE;
        }
        if (centralizedAff) {
            assert !exchCtx.mergeExchanges();
            IgniteInternalFuture<Map<Integer, Map<Integer, List<UUID>>>> fut = cctx.affinity().initAffinityOnNodeLeft(this);
            if (!fut.isDone()) {
                fut.listen(new IgniteInClosure<IgniteInternalFuture<Map<Integer, Map<Integer, List<UUID>>>>>() {

                    @Override
                    public void apply(IgniteInternalFuture<Map<Integer, Map<Integer, List<UUID>>>> fut) {
                        onAffinityInitialized(fut);
                    }
                });
            } else
                onAffinityInitialized(fut);
        } else {
            Set<ClusterNode> nodes;
            Map<UUID, GridDhtPartitionsSingleMessage> mergedJoinExchMsgs0;
            synchronized (mux) {
                srvNodes.remove(cctx.localNode());
                nodes = U.newHashSet(srvNodes.size());
                nodes.addAll(srvNodes);
                mergedJoinExchMsgs0 = mergedJoinExchMsgs;
                if (mergedJoinExchMsgs != null) {
                    for (Map.Entry<UUID, GridDhtPartitionsSingleMessage> e : mergedJoinExchMsgs.entrySet()) {
                        if (e.getValue() != null) {
                            ClusterNode node = cctx.discovery().node(e.getKey());
                            if (node != null)
                                nodes.add(node);
                        }
                    }
                }
                if (!F.isEmpty(sndResNodes))
                    nodes.addAll(sndResNodes);
            }
            if (!nodes.isEmpty())
                sendAllPartitions(msg, nodes, mergedJoinExchMsgs0, joinedNodeAff);
            if (!stateChangeExchange())
                onDone(exchCtx.events().topologyVersion(), null);
            for (Map.Entry<UUID, GridDhtPartitionsSingleMessage> e : pendingSingleMsgs.entrySet()) {
                if (log.isInfoEnabled()) {
                    log.info("Process pending message on coordinator [node=" + e.getKey() + ", ver=" + initialVersion() + ", resVer=" + resTopVer + ']');
                }
                processSingleMessage(e.getKey(), e.getValue());
            }
        }
        if (stateChangeExchange()) {
            IgniteCheckedException err = null;
            StateChangeRequest req = exchActions.stateChangeRequest();
            assert req != null : exchActions;
            boolean stateChangeErr = false;
            if (!F.isEmpty(changeGlobalStateExceptions)) {
                stateChangeErr = true;
                err = new IgniteCheckedException("Cluster state change failed.");
                cctx.kernalContext().state().onStateChangeError(changeGlobalStateExceptions, req);
            } else {
                boolean hasMoving = !partsToReload.isEmpty();
                Set<Integer> waitGrps = cctx.affinity().waitGroups();
                if (!hasMoving) {
                    for (CacheGroupContext grpCtx : cctx.cache().cacheGroups()) {
                        if (waitGrps.contains(grpCtx.groupId()) && grpCtx.topology().hasMovingPartitions()) {
                            hasMoving = true;
                            break;
                        }
                    }
                }
                cctx.kernalContext().state().onExchangeFinishedOnCoordinator(this, hasMoving);
            }
            boolean active = !stateChangeErr && req.activate();
            ChangeGlobalStateFinishMessage stateFinishMsg = new ChangeGlobalStateFinishMessage(req.requestId(), active, !stateChangeErr);
            cctx.discovery().sendCustomEvent(stateFinishMsg);
            if (!centralizedAff)
                onDone(exchCtx.events().topologyVersion(), err);
        }
    } catch (IgniteCheckedException e) {
        if (reconnectOnError(e))
            onDone(new IgniteNeedReconnectException(cctx.localNode(), e));
        else
            onDone(e);
    }
}
Also used : GridDhtPartitionTopology(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtPartitionTopology) StateChangeRequest(org.apache.ignite.internal.processors.cache.StateChangeRequest) IgniteProductVersion(org.apache.ignite.lang.IgniteProductVersion) CacheGroupDescriptor(org.apache.ignite.internal.processors.cache.CacheGroupDescriptor) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) DiscoveryCustomMessage(org.apache.ignite.internal.managers.discovery.DiscoveryCustomMessage) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) DynamicCacheChangeBatch(org.apache.ignite.internal.processors.cache.DynamicCacheChangeBatch) ArrayList(java.util.ArrayList) List(java.util.List) UUID(java.util.UUID) IgniteNeedReconnectException(org.apache.ignite.internal.IgniteNeedReconnectException) ClusterNode(org.apache.ignite.cluster.ClusterNode) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) DiscoveryCustomEvent(org.apache.ignite.internal.events.DiscoveryCustomEvent) SnapshotDiscoveryMessage(org.apache.ignite.internal.processors.cache.persistence.snapshot.SnapshotDiscoveryMessage) ChangeGlobalStateFinishMessage(org.apache.ignite.internal.processors.cluster.ChangeGlobalStateFinishMessage) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Aggregations

CacheGroupContext (org.apache.ignite.internal.processors.cache.CacheGroupContext)21 HashMap (java.util.HashMap)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)7 Map (java.util.Map)6 ConcurrentMap (java.util.concurrent.ConcurrentMap)6 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 LinkedHashMap (java.util.LinkedHashMap)5 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)5 GridDhtPartitionTopology (org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtPartitionTopology)5 ArrayList (java.util.ArrayList)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 List (java.util.List)3 UUID (java.util.UUID)3 ClusterNode (org.apache.ignite.cluster.ClusterNode)3 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)3 GridDhtLocalPartition (org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtLocalPartition)3 HashSet (java.util.HashSet)2 Set (java.util.Set)2 IgniteNeedReconnectException (org.apache.ignite.internal.IgniteNeedReconnectException)2 DiscoveryCustomEvent (org.apache.ignite.internal.events.DiscoveryCustomEvent)2