Search in sources :

Example 1 with GridAffinityAssignmentCache

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

the class CacheAffinitySharedManager method onChangeAffinityMessage.

/**
     * Called on exchange initiated by {@link CacheAffinityChangeMessage} which sent after rebalance finished.
     *
     * @param exchFut Exchange future.
     * @param crd Coordinator flag.
     * @param msg Message.
     * @throws IgniteCheckedException If failed.
     */
public void onChangeAffinityMessage(final GridDhtPartitionsExchangeFuture exchFut, boolean crd, final CacheAffinityChangeMessage msg) throws IgniteCheckedException {
    assert affCalcVer != null || cctx.kernalContext().clientNode();
    assert msg.topologyVersion() != null && msg.exchangeId() == null : msg;
    assert affCalcVer == null || affCalcVer.equals(msg.topologyVersion());
    final AffinityTopologyVersion topVer = exchFut.topologyVersion();
    if (log.isDebugEnabled()) {
        log.debug("Process affinity change message [exchVer=" + exchFut.topologyVersion() + ", affCalcVer=" + affCalcVer + ", msgVer=" + msg.topologyVersion() + ']');
    }
    final Map<Integer, Map<Integer, List<UUID>>> affChange = msg.assignmentChange();
    assert !F.isEmpty(affChange) : msg;
    final Map<Integer, IgniteUuid> deploymentIds = msg.cacheDeploymentIds();
    final Map<Object, List<List<ClusterNode>>> affCache = new HashMap<>();
    forAllCaches(crd, new IgniteInClosureX<GridAffinityAssignmentCache>() {

        @Override
        public void applyx(GridAffinityAssignmentCache aff) throws IgniteCheckedException {
            AffinityTopologyVersion affTopVer = aff.lastVersion();
            assert affTopVer.topologyVersion() > 0 : affTopVer;
            DynamicCacheDescriptor desc = registeredCaches.get(aff.cacheId());
            assert desc != null : aff.cacheName();
            IgniteUuid deploymentId = desc.deploymentId();
            if (!deploymentId.equals(deploymentIds.get(aff.cacheId()))) {
                aff.clientEventTopologyChange(exchFut.discoveryEvent(), topVer);
                return;
            }
            Map<Integer, List<UUID>> change = affChange.get(aff.cacheId());
            if (change != null) {
                assert !change.isEmpty() : msg;
                List<List<ClusterNode>> curAff = aff.assignments(affTopVer);
                List<List<ClusterNode>> assignment = new ArrayList<>(curAff);
                for (Map.Entry<Integer, List<UUID>> e : change.entrySet()) {
                    Integer part = e.getKey();
                    List<ClusterNode> nodes = toNodes(topVer, e.getValue());
                    assert !nodes.equals(assignment.get(part)) : "Assignment did not change " + "[cache=" + aff.cacheName() + ", part=" + part + ", cur=" + F.nodeIds(assignment.get(part)) + ", new=" + F.nodeIds(nodes) + ", exchVer=" + exchFut.topologyVersion() + ", msgVer=" + msg.topologyVersion() + ']';
                    assignment.set(part, nodes);
                }
                aff.initialize(topVer, cachedAssignment(aff, assignment, affCache));
            } else
                aff.clientEventTopologyChange(exchFut.discoveryEvent(), topVer);
        }
    });
    synchronized (mux) {
        if (affCalcVer == null)
            affCalcVer = msg.topologyVersion();
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteUuid(org.apache.ignite.lang.IgniteUuid) GridAffinityAssignmentCache(org.apache.ignite.internal.processors.affinity.GridAffinityAssignmentCache) ArrayList(java.util.ArrayList) List(java.util.List) UUID(java.util.UUID) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 2 with GridAffinityAssignmentCache

use of org.apache.ignite.internal.processors.affinity.GridAffinityAssignmentCache 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 GridAffinityAssignmentCache

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

the class CacheAffinitySharedManager method onExchangeChangeAffinityMessage.

/**
     * Called when received {@link CacheAffinityChangeMessage} which should complete exchange.
     *
     * @param exchFut Exchange future.
     * @param crd Coordinator flag.
     * @param msg Affinity change message.
     */
public void onExchangeChangeAffinityMessage(GridDhtPartitionsExchangeFuture exchFut, boolean crd, CacheAffinityChangeMessage msg) {
    if (log.isDebugEnabled()) {
        log.debug("Process exchange affinity change message [exchVer=" + exchFut.topologyVersion() + ", msg=" + msg + ']');
    }
    assert exchFut.exchangeId().equals(msg.exchangeId()) : msg;
    final AffinityTopologyVersion topVer = exchFut.topologyVersion();
    final Map<Integer, Map<Integer, List<UUID>>> assignment = msg.assignmentChange();
    assert assignment != null;
    final Map<Object, List<List<ClusterNode>>> affCache = new HashMap<>();
    forAllCaches(crd, new IgniteInClosureX<GridAffinityAssignmentCache>() {

        @Override
        public void applyx(GridAffinityAssignmentCache aff) throws IgniteCheckedException {
            List<List<ClusterNode>> idealAssignment = aff.idealAssignment();
            assert idealAssignment != null;
            Map<Integer, List<UUID>> cacheAssignment = assignment.get(aff.cacheId());
            List<List<ClusterNode>> newAssignment;
            if (cacheAssignment != null) {
                newAssignment = new ArrayList<>(idealAssignment);
                for (Map.Entry<Integer, List<UUID>> e : cacheAssignment.entrySet()) newAssignment.set(e.getKey(), toNodes(topVer, e.getValue()));
            } else
                newAssignment = idealAssignment;
            aff.initialize(topVer, cachedAssignment(aff, newAssignment, affCache));
        }
    });
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ArrayList(java.util.ArrayList) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridAffinityAssignmentCache(org.apache.ignite.internal.processors.affinity.GridAffinityAssignmentCache) ArrayList(java.util.ArrayList) List(java.util.List) UUID(java.util.UUID) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 4 with GridAffinityAssignmentCache

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

the class GridCacheAffinityManager method start0.

/** {@inheritDoc} */
@Override
public void start0() throws IgniteCheckedException {
    affFunction = cctx.config().getAffinity();
    affMapper = cctx.config().getAffinityMapper();
    aff = new GridAffinityAssignmentCache(cctx.kernalContext(), cctx.name(), affFunction, cctx.config().getNodeFilter(), cctx.config().getBackups(), cctx.isLocal());
}
Also used : GridAffinityAssignmentCache(org.apache.ignite.internal.processors.affinity.GridAffinityAssignmentCache)

Example 5 with GridAffinityAssignmentCache

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

the class GridDhtPartitionsExchangeFuture method clientOnlyExchange.

/**
     * @throws IgniteCheckedException If failed.
     */
private void clientOnlyExchange() throws IgniteCheckedException {
    clientOnlyExchange = true;
    if (crd != null) {
        if (crd.isLocal()) {
            for (GridCacheContext cacheCtx : cctx.cacheContexts()) {
                boolean updateTop = !cacheCtx.isLocal() && exchId.topologyVersion().equals(cacheCtx.startTopologyVersion());
                if (updateTop) {
                    for (GridClientPartitionTopology top : cctx.exchange().clientTopologies()) {
                        if (top.cacheId() == cacheCtx.cacheId()) {
                            cacheCtx.topology().update(exchId, top.partitionMap(true), top.updateCounters(false));
                            break;
                        }
                    }
                }
            }
        } else {
            if (!centralizedAff)
                sendLocalPartitions(crd);
            initDone();
            return;
        }
    } else {
        if (centralizedAff) {
            // Last server node failed.
            for (GridCacheContext cacheCtx : cctx.cacheContexts()) {
                GridAffinityAssignmentCache aff = cacheCtx.affinity().affinityCache();
                aff.initialize(topologyVersion(), aff.idealAssignment());
            }
        }
    }
    onDone(topologyVersion());
}
Also used : GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) GridAffinityAssignmentCache(org.apache.ignite.internal.processors.affinity.GridAffinityAssignmentCache) GridClientPartitionTopology(org.apache.ignite.internal.processors.cache.distributed.dht.GridClientPartitionTopology)

Aggregations

GridAffinityAssignmentCache (org.apache.ignite.internal.processors.affinity.GridAffinityAssignmentCache)8 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)6 ArrayList (java.util.ArrayList)4 List (java.util.List)4 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 HashMap (java.util.HashMap)3 UUID (java.util.UUID)3 ClusterNode (org.apache.ignite.cluster.ClusterNode)3 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)2 GridClientPartitionTopology (org.apache.ignite.internal.processors.cache.distributed.dht.GridClientPartitionTopology)2 HashSet (java.util.HashSet)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 NearCacheConfiguration (org.apache.ignite.configuration.NearCacheConfiguration)1 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)1 GridCacheProcessor (org.apache.ignite.internal.processors.cache.GridCacheProcessor)1 GridDhtAffinityAssignmentResponse (org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtAffinityAssignmentResponse)1 GridDhtAssignmentFetchFuture (org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtAssignmentFetchFuture)1