Search in sources :

Example 6 with AffinityTopologyVersion

use of org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion in project ignite by apache.

the class GridDhtPartitionTopologyImpl method update.

/** {@inheritDoc} */
@SuppressWarnings({ "MismatchedQueryAndUpdateOfCollection" })
@Override
public GridDhtPartitionMap update(@Nullable GridDhtPartitionExchangeId exchId, GridDhtPartitionFullMap partMap, @Nullable Map<Integer, T2<Long, Long>> cntrMap) {
    if (log.isDebugEnabled())
        log.debug("Updating full partition map [exchId=" + exchId + ", parts=" + fullMapString() + ']');
    assert partMap != null;
    lock.writeLock().lock();
    try {
        if (stopping)
            return null;
        if (cntrMap != null) {
            // update local map partition counters
            for (Map.Entry<Integer, T2<Long, Long>> e : cntrMap.entrySet()) {
                T2<Long, Long> cntr = this.cntrMap.get(e.getKey());
                if (cntr == null || cntr.get2() < e.getValue().get2())
                    this.cntrMap.put(e.getKey(), e.getValue());
            }
            // update local counters in partitions
            for (int i = 0; i < locParts.length(); i++) {
                GridDhtLocalPartition part = locParts.get(i);
                if (part == null)
                    continue;
                T2<Long, Long> cntr = cntrMap.get(part.id());
                if (cntr != null)
                    part.updateCounter(cntr.get2());
            }
        }
        //if need skip
        if (exchId != null && lastExchangeId != null && lastExchangeId.compareTo(exchId) >= 0) {
            if (log.isDebugEnabled())
                log.debug("Stale exchange id for full partition map update (will ignore) [lastExchId=" + lastExchangeId + ", exchId=" + exchId + ']');
            return null;
        }
        if (node2part != null && node2part.compareTo(partMap) >= 0) {
            if (log.isDebugEnabled())
                log.debug("Stale partition map for full partition map update (will ignore) [lastExchId=" + lastExchangeId + ", exchId=" + exchId + ", curMap=" + node2part + ", newMap=" + partMap + ']');
            return null;
        }
        long updateSeq = this.updateSeq.incrementAndGet();
        if (exchId != null)
            lastExchangeId = exchId;
        if (node2part != null) {
            for (GridDhtPartitionMap part : node2part.values()) {
                GridDhtPartitionMap newPart = partMap.get(part.nodeId());
                // then we keep the newer value.
                if (newPart != null && (newPart.updateSequence() < part.updateSequence() || (cctx.startTopologyVersion().compareTo(newPart.topologyVersion()) > 0))) {
                    if (log.isDebugEnabled())
                        log.debug("Overriding partition map in full update map [exchId=" + exchId + ", curPart=" + mapString(part) + ", newPart=" + mapString(newPart) + ']');
                    partMap.put(part.nodeId(), part);
                }
            }
            // Remove entry if node left.
            for (Iterator<UUID> it = partMap.keySet().iterator(); it.hasNext(); ) {
                UUID nodeId = it.next();
                if (!cctx.discovery().alive(nodeId)) {
                    if (log.isDebugEnabled())
                        log.debug("Removing left node from full map update [nodeId=" + nodeId + ", partMap=" + partMap + ']');
                    it.remove();
                }
            }
        }
        node2part = partMap;
        Map<Integer, Set<UUID>> p2n = new HashMap<>(cctx.affinity().partitions(), 1.0f);
        for (Map.Entry<UUID, GridDhtPartitionMap> e : partMap.entrySet()) {
            for (Integer p : e.getValue().keySet()) {
                Set<UUID> ids = p2n.get(p);
                if (ids == null)
                    // Initialize HashSet to size 3 in anticipation that there won't be
                    // more than 3 nodes per partitions.
                    p2n.put(p, ids = U.newHashSet(3));
                ids.add(e.getKey());
            }
        }
        part2node = p2n;
        boolean changed = false;
        AffinityTopologyVersion affVer = cctx.affinity().affinityTopologyVersion();
        GridDhtPartitionMap nodeMap = partMap.get(cctx.localNodeId());
        if (nodeMap != null && cctx.shared().database().persistenceEnabled()) {
            for (Map.Entry<Integer, GridDhtPartitionState> e : nodeMap.entrySet()) {
                int p = e.getKey();
                GridDhtPartitionState state = e.getValue();
                if (state == MOVING) {
                    GridDhtLocalPartition locPart = locParts.get(p);
                    assert locPart != null;
                    if (locPart.state() == OWNING) {
                        locPart.moving();
                        changed = true;
                    }
                    if (cntrMap != null) {
                        T2<Long, Long> cntr = cntrMap.get(p);
                        if (cntr != null && cntr.get2() > locPart.updateCounter())
                            locPart.updateCounter(cntr.get2());
                    }
                }
            }
        }
        if (!affVer.equals(AffinityTopologyVersion.NONE) && affVer.compareTo(topVer) >= 0) {
            List<List<ClusterNode>> aff = cctx.affinity().assignments(topVer);
            changed |= checkEvictions(updateSeq, aff);
            updateRebalanceVersion(aff);
        }
        consistencyCheck();
        if (log.isDebugEnabled())
            log.debug("Partition map after full update: " + fullMapString());
        if (changed)
            cctx.shared().exchange().scheduleResendPartitions();
        return changed ? localPartitionMap() : null;
    } finally {
        lock.writeLock().unlock();
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridDhtPartitionMap(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionMap) GridAtomicLong(org.apache.ignite.internal.util.GridAtomicLong) List(java.util.List) ArrayList(java.util.ArrayList) UUID(java.util.UUID) Map(java.util.Map) HashMap(java.util.HashMap) GridDhtPartitionFullMap(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionFullMap) GridDhtPartitionMap(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionMap) T2(org.apache.ignite.internal.util.typedef.T2)

Example 7 with AffinityTopologyVersion

use of org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion in project ignite by apache.

the class GridPartitionedGetFuture method init.

/**
     * Initializes future.
     */
public void init() {
    AffinityTopologyVersion lockedTopVer = cctx.shared().lockedTopologyVersion(null);
    if (lockedTopVer != null) {
        canRemap = false;
        map(keys, Collections.<ClusterNode, LinkedHashMap<KeyCacheObject, Boolean>>emptyMap(), lockedTopVer);
    } else {
        AffinityTopologyVersion topVer = this.topVer.topologyVersion() > 0 ? this.topVer : canRemap ? cctx.affinity().affinityTopologyVersion() : cctx.shared().exchange().readyAffinityVersion();
        map(keys, Collections.<ClusterNode, LinkedHashMap<KeyCacheObject, Boolean>>emptyMap(), topVer);
    }
    markInitialized();
}
Also used : AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 8 with AffinityTopologyVersion

use of org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion in project ignite by apache.

the class GridDhtPreloader method assign.

/** {@inheritDoc} */
@Override
public GridDhtPreloaderAssignments assign(GridDhtPartitionsExchangeFuture exchFut) {
    // No assignments for disabled preloader.
    GridDhtPartitionTopology top = cctx.dht().topology();
    if (!cctx.rebalanceEnabled() || !cctx.shared().kernalContext().state().active())
        return new GridDhtPreloaderAssignments(exchFut, top.topologyVersion());
    int partCnt = cctx.affinity().partitions();
    assert exchFut.forcePreload() || exchFut.dummyReassign() || exchFut.exchangeId().topologyVersion().equals(top.topologyVersion()) : "Topology version mismatch [exchId=" + exchFut.exchangeId() + ", cache=" + cctx.name() + ", topVer=" + top.topologyVersion() + ']';
    GridDhtPreloaderAssignments assigns = new GridDhtPreloaderAssignments(exchFut, top.topologyVersion());
    AffinityTopologyVersion topVer = assigns.topologyVersion();
    for (int p = 0; p < partCnt; p++) {
        if (cctx.shared().exchange().hasPendingExchange()) {
            if (log.isDebugEnabled())
                log.debug("Skipping assignments creation, exchange worker has pending assignments: " + exchFut.exchangeId());
            assigns.cancelled(true);
            return assigns;
        }
        // If partition belongs to local node.
        if (cctx.affinity().partitionLocalNode(p, topVer)) {
            GridDhtLocalPartition part = top.localPartition(p, topVer, true);
            assert part != null;
            assert part.id() == p;
            if (part.state() != MOVING) {
                if (log.isDebugEnabled())
                    log.debug("Skipping partition assignment (state is not MOVING): " + part);
                // For.
                continue;
            }
            Collection<ClusterNode> picked = pickedOwners(p, topVer);
            if (picked.isEmpty()) {
                top.own(part);
                if (cctx.events().isRecordable(EVT_CACHE_REBALANCE_PART_DATA_LOST)) {
                    DiscoveryEvent discoEvt = exchFut.discoveryEvent();
                    cctx.events().addPreloadEvent(p, EVT_CACHE_REBALANCE_PART_DATA_LOST, discoEvt.eventNode(), discoEvt.type(), discoEvt.timestamp());
                }
                if (log.isDebugEnabled())
                    log.debug("Owning partition as there are no other owners: " + part);
            } else {
                ClusterNode n = F.rand(picked);
                GridDhtPartitionDemandMessage msg = assigns.get(n);
                if (msg == null) {
                    assigns.put(n, msg = new GridDhtPartitionDemandMessage(top.updateSequence(), exchFut.exchangeId().topologyVersion(), cctx.cacheId()));
                }
                msg.addPartition(p);
            }
        }
    }
    return assigns;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridDhtPartitionTopology(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtPartitionTopology) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) GridDhtLocalPartition(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtLocalPartition)

Example 9 with AffinityTopologyVersion

use of org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion in project ignite by apache.

the class GridDhtPreloader method processAffinityAssignmentRequest.

/**
     * @param node Node.
     * @param req Request.
     */
private void processAffinityAssignmentRequest(final ClusterNode node, final GridDhtAffinityAssignmentRequest req) {
    final AffinityTopologyVersion topVer = req.topologyVersion();
    if (log.isDebugEnabled())
        log.debug("Processing affinity assignment request [node=" + node + ", req=" + req + ']');
    cctx.affinity().affinityReadyFuture(req.topologyVersion()).listen(new CI1<IgniteInternalFuture<AffinityTopologyVersion>>() {

        @Override
        public void apply(IgniteInternalFuture<AffinityTopologyVersion> fut) {
            if (log.isDebugEnabled())
                log.debug("Affinity is ready for topology version, will send response [topVer=" + topVer + ", node=" + node + ']');
            AffinityAssignment assignment = cctx.affinity().assignment(topVer);
            GridDhtAffinityAssignmentResponse res = new GridDhtAffinityAssignmentResponse(req.futureId(), cctx.cacheId(), topVer, assignment.assignment());
            if (cctx.affinity().affinityCache().centralizedAffinityFunction()) {
                assert assignment.idealAssignment() != null;
                res.idealAffinityAssignment(assignment.idealAssignment());
            }
            try {
                cctx.io().send(node, res, AFFINITY_POOL);
            } catch (IgniteCheckedException e) {
                U.error(log, "Failed to send affinity assignment response to remote node [node=" + node + ']', e);
            }
        }
    });
}
Also used : AffinityAssignment(org.apache.ignite.internal.processors.affinity.AffinityAssignment) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridDhtAffinityAssignmentResponse(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtAffinityAssignmentResponse) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture)

Example 10 with AffinityTopologyVersion

use of org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion in project ignite by apache.

the class GridDhtPartitionsExchangeFuture method init.

/**
     * Starts activity.
     *
     * @throws IgniteInterruptedCheckedException If interrupted.
     */
public void init() throws IgniteInterruptedCheckedException {
    if (isDone())
        return;
    initTs = U.currentTimeMillis();
    U.await(evtLatch);
    assert discoEvt != null : this;
    assert exchId.nodeId().equals(discoEvt.eventNode().id()) : this;
    assert !dummy && !forcePreload : this;
    try {
        discoCache.updateAlives(cctx.discovery());
        AffinityTopologyVersion topVer = topologyVersion();
        srvNodes = new ArrayList<>(discoCache.serverNodes());
        remaining.addAll(F.nodeIds(F.view(srvNodes, F.remoteNodes(cctx.localNodeId()))));
        crd = srvNodes.isEmpty() ? null : srvNodes.get(0);
        boolean crdNode = crd != null && crd.isLocal();
        skipPreload = cctx.kernalContext().clientNode();
        ExchangeType exchange;
        if (discoEvt.type() == EVT_DISCOVERY_CUSTOM_EVT) {
            DiscoveryCustomMessage msg = ((DiscoveryCustomEvent) discoEvt).customMessage();
            if (msg instanceof DynamicCacheChangeBatch) {
                assert exchActions != null && !exchActions.empty();
                exchange = onCacheChangeRequest(crdNode);
            } else if (msg instanceof StartFullSnapshotAckDiscoveryMessage)
                exchange = CU.clientNode(discoEvt.eventNode()) ? onClientNodeEvent(crdNode) : onServerNodeEvent(crdNode);
            else {
                assert affChangeMsg != null : this;
                exchange = onAffinityChangeRequest(crdNode);
            }
        } else {
            if (discoEvt.type() == EVT_NODE_JOINED) {
                if (!discoEvt.eventNode().isLocal()) {
                    Collection<DynamicCacheDescriptor> receivedCaches = cctx.cache().startReceivedCaches(discoEvt.eventNode().id(), topVer);
                    cctx.affinity().initStartedCaches(crdNode, this, receivedCaches);
                } else
                    cctx.cache().startCachesOnLocalJoin(topVer);
            }
            exchange = CU.clientNode(discoEvt.eventNode()) ? onClientNodeEvent(crdNode) : onServerNodeEvent(crdNode);
        }
        updateTopologies(crdNode);
        if (exchActions != null && exchActions.hasStop())
            cctx.cache().context().database().beforeCachesStop();
        switch(exchange) {
            case ALL:
                {
                    distributedExchange();
                    break;
                }
            case CLIENT:
                {
                    initTopologies();
                    clientOnlyExchange();
                    break;
                }
            case NONE:
                {
                    initTopologies();
                    onDone(topVer);
                    break;
                }
            default:
                assert false;
        }
    } catch (IgniteInterruptedCheckedException e) {
        onDone(e);
        throw e;
    } catch (IgniteNeedReconnectException e) {
        onDone(e);
    } catch (Throwable e) {
        if (reconnectOnError(e))
            onDone(new IgniteNeedReconnectException(cctx.localNode(), e));
        else {
            U.error(log, "Failed to reinitialize local partitions (preloading will be stopped): " + exchId, e);
            onDone(e);
        }
        if (e instanceof Error)
            throw (Error) e;
    }
}
Also used : AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) DynamicCacheDescriptor(org.apache.ignite.internal.processors.cache.DynamicCacheDescriptor) DiscoveryCustomMessage(org.apache.ignite.internal.managers.discovery.DiscoveryCustomMessage) DiscoveryCustomEvent(org.apache.ignite.internal.events.DiscoveryCustomEvent) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) StartFullSnapshotAckDiscoveryMessage(org.apache.ignite.internal.pagemem.snapshot.StartFullSnapshotAckDiscoveryMessage) DynamicCacheChangeBatch(org.apache.ignite.internal.processors.cache.DynamicCacheChangeBatch) IgniteNeedReconnectException(org.apache.ignite.internal.IgniteNeedReconnectException)

Aggregations

AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)386 ClusterNode (org.apache.ignite.cluster.ClusterNode)135 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)116 ArrayList (java.util.ArrayList)85 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)74 List (java.util.List)63 UUID (java.util.UUID)63 Map (java.util.Map)62 Test (org.junit.Test)59 Ignite (org.apache.ignite.Ignite)51 HashMap (java.util.HashMap)47 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)47 IgniteEx (org.apache.ignite.internal.IgniteEx)46 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)40 IgniteException (org.apache.ignite.IgniteException)38 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)38 GridCacheEntryRemovedException (org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)35 Nullable (org.jetbrains.annotations.Nullable)33 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)32 GridCacheVersion (org.apache.ignite.internal.processors.cache.version.GridCacheVersion)32