Search in sources :

Example 1 with IgniteInClosureX

use of org.apache.ignite.internal.util.lang.IgniteInClosureX 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)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 UUID (java.util.UUID)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)1 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)1 GridAffinityAssignmentCache (org.apache.ignite.internal.processors.affinity.GridAffinityAssignmentCache)1 GridDhtAffinityAssignmentResponse (org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtAffinityAssignmentResponse)1 GridDhtAssignmentFetchFuture (org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtAssignmentFetchFuture)1 GridDhtPartitionsExchangeFuture (org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsExchangeFuture)1 GridCompoundFuture (org.apache.ignite.internal.util.future.GridCompoundFuture)1 GridFutureAdapter (org.apache.ignite.internal.util.future.GridFutureAdapter)1 IgniteInClosureX (org.apache.ignite.internal.util.lang.IgniteInClosureX)1 IgniteBiInClosure (org.apache.ignite.lang.IgniteBiInClosure)1