Search in sources :

Example 1 with AffinityTopologyVersion

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

the class GridCacheQueryManager method scanIterator.

/**
     * @param qry Query.
     * @param locNode Local node.
     * @return Full-scan row iterator.
     * @throws IgniteCheckedException If failed to get iterator.
     */
@SuppressWarnings({ "unchecked" })
private GridCloseableIterator<IgniteBiTuple<K, V>> scanIterator(final GridCacheQueryAdapter<?> qry, boolean locNode) throws IgniteCheckedException {
    final IgniteBiPredicate<K, V> keyValFilter = qry.scanFilter();
    try {
        injectResources(keyValFilter);
        Integer part = qry.partition();
        if (cctx.isLocal())
            part = null;
        if (part != null && (part < 0 || part >= cctx.affinity().partitions()))
            return new GridEmptyCloseableIterator<>();
        final ExpiryPolicy plc = cctx.expiry();
        AffinityTopologyVersion topVer = GridQueryProcessor.getRequestAffinityTopologyVersion();
        if (topVer == null)
            topVer = cctx.affinity().affinityTopologyVersion();
        final boolean backups = qry.includeBackups() || cctx.isReplicated();
        final GridDhtLocalPartition locPart;
        final GridIterator<CacheDataRow> it;
        if (part != null) {
            final GridDhtCacheAdapter dht = cctx.isNear() ? cctx.near().dht() : cctx.dht();
            GridDhtLocalPartition locPart0 = dht.topology().localPartition(part, topVer, false);
            if (locPart0 == null || locPart0.state() != OWNING || !locPart0.reserve())
                throw new GridDhtUnreservedPartitionException(part, cctx.affinity().affinityTopologyVersion(), "Partition can not be reserved");
            if (locPart0.state() != OWNING) {
                locPart0.release();
                throw new GridDhtUnreservedPartitionException(part, cctx.affinity().affinityTopologyVersion(), "Partition can not be reserved");
            }
            locPart = locPart0;
            it = cctx.offheap().iterator(part);
        } else {
            locPart = null;
            it = cctx.offheap().iterator(true, backups, topVer);
        }
        return new PeekValueExpiryAwareIterator(it, plc, topVer, keyValFilter, qry.keepBinary(), locNode) {

            @Override
            protected void onClose() {
                super.onClose();
                if (locPart != null)
                    locPart.release();
                closeScanFilter(keyValFilter);
            }
        };
    } catch (IgniteCheckedException | RuntimeException e) {
        closeScanFilter(keyValFilter);
        throw e;
    }
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.database.CacheDataRow) GridDhtCacheAdapter(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtCacheAdapter) GridDhtUnreservedPartitionException(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtUnreservedPartitionException) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteCacheExpiryPolicy(org.apache.ignite.internal.processors.cache.IgniteCacheExpiryPolicy) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) GridDhtLocalPartition(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtLocalPartition)

Example 2 with AffinityTopologyVersion

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

the class GridClusterStateProcessor method start.

/** {@inheritDoc} */
@Override
public void start(boolean activeOnStart) throws IgniteCheckedException {
    super.start(activeOnStart);
    globalState = activeOnStart ? ACTIVE : INACTIVE;
    cacheProc = ctx.cache();
    sharedCtx = cacheProc.context();
    sharedCtx.io().addHandler(0, GridChangeGlobalStateMessageResponse.class, new CI2<UUID, GridChangeGlobalStateMessageResponse>() {

        @Override
        public void apply(UUID nodeId, GridChangeGlobalStateMessageResponse msg) {
            processChangeGlobalStateResponse(nodeId, msg);
        }
    });
    ctx.discovery().setCustomEventListener(ChangeGlobalStateMessage.class, new CustomEventListener<ChangeGlobalStateMessage>() {

        @Override
        public void onCustomEvent(AffinityTopologyVersion topVer, ClusterNode snd, ChangeGlobalStateMessage msg) {
            assert topVer != null;
            assert snd != null;
            assert msg != null;
            boolean activate = msg.activate();
            ChangeGlobalStateContext actx = lastCgsCtx;
            if (actx != null && globalState == TRANSITION) {
                GridChangeGlobalStateFuture f = cgsLocFut.get();
                if (log.isDebugEnabled())
                    log.debug("Concurrent " + prettyStr(activate) + " [id=" + ctx.localNodeId() + " topVer=" + topVer + " actx=" + actx + ", msg=" + msg + "]");
                if (f != null && f.requestId.equals(msg.requestId()))
                    f.onDone(new IgniteCheckedException("Concurrent change state, now in progress=" + (activate) + ", initiatingNodeId=" + actx.initiatingNodeId + ", you try=" + (prettyStr(activate)) + ", locNodeId=" + ctx.localNodeId()));
                msg.concurrentChangeState();
            } else {
                if (log.isInfoEnabled())
                    log.info("Create " + prettyStr(activate) + " context [id=" + ctx.localNodeId() + " topVer=" + topVer + ", reqId=" + msg.requestId() + ", initiatingNodeId=" + msg.initiatorNodeId() + "]");
                lastCgsCtx = new ChangeGlobalStateContext(msg.requestId(), msg.initiatorNodeId(), msg.getDynamicCacheChangeBatch(), msg.activate());
                globalState = TRANSITION;
            }
        }
    });
    ctx.event().addLocalEventListener(lsr, EVT_NODE_LEFT, EVT_NODE_FAILED);
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) ChangeGlobalStateMessage(org.apache.ignite.internal.processors.cache.ChangeGlobalStateMessage) UUID(java.util.UUID) GridChangeGlobalStateMessageResponse(org.apache.ignite.internal.processors.cache.GridChangeGlobalStateMessageResponse)

Example 3 with AffinityTopologyVersion

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

the class CacheAffinitySharedManager method initAffinityOnNodeJoin.

/**
     * @param fut Exchange future.
     * @param aff Affinity.
     * @param rebalanceInfo Rebalance information.
     * @param latePrimary If {@code true} delays primary assignment if it is not owner.
     * @param affCache Already calculated assignments (to reduce data stored in history).
     * @throws IgniteCheckedException If failed.
     */
private void initAffinityOnNodeJoin(GridDhtPartitionsExchangeFuture fut, GridAffinityAssignmentCache aff, WaitRebalanceInfo rebalanceInfo, boolean latePrimary, Map<Object, List<List<ClusterNode>>> affCache) throws IgniteCheckedException {
    assert lateAffAssign;
    AffinityTopologyVersion topVer = fut.topologyVersion();
    AffinityTopologyVersion affTopVer = aff.lastVersion();
    assert affTopVer.topologyVersion() > 0 : "Affinity is not initialized [cache=" + aff.cacheName() + ", topVer=" + affTopVer + ", node=" + cctx.localNodeId() + ']';
    List<List<ClusterNode>> curAff = aff.assignments(affTopVer);
    assert aff.idealAssignment() != null : "Previous assignment is not available.";
    List<List<ClusterNode>> idealAssignment = aff.calculate(topVer, fut.discoveryEvent(), fut.discoCache());
    List<List<ClusterNode>> newAssignment = null;
    if (latePrimary) {
        for (int p = 0; p < idealAssignment.size(); p++) {
            List<ClusterNode> newNodes = idealAssignment.get(p);
            List<ClusterNode> curNodes = curAff.get(p);
            ClusterNode curPrimary = curNodes.size() > 0 ? curNodes.get(0) : null;
            ClusterNode newPrimary = newNodes.size() > 0 ? newNodes.get(0) : null;
            if (curPrimary != null && newPrimary != null && !curPrimary.equals(newPrimary)) {
                assert cctx.discovery().node(topVer, curPrimary.id()) != null : curPrimary;
                List<ClusterNode> nodes0 = latePrimaryAssignment(aff, p, curPrimary, newNodes, rebalanceInfo);
                if (newAssignment == null)
                    newAssignment = new ArrayList<>(idealAssignment);
                newAssignment.set(p, nodes0);
            }
        }
    }
    if (newAssignment == null)
        newAssignment = idealAssignment;
    aff.initialize(fut.topologyVersion(), cachedAssignment(aff, newAssignment, affCache));
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with AffinityTopologyVersion

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

the class CacheAffinitySharedManager method fetchAffinity.

/**
     * @param fut Exchange future.
     * @param affCache Affinity.
     * @param fetchFut Affinity fetch future.
     * @throws IgniteCheckedException If failed.
     */
private void fetchAffinity(GridDhtPartitionsExchangeFuture fut, GridAffinityAssignmentCache affCache, GridDhtAssignmentFetchFuture fetchFut) throws IgniteCheckedException {
    assert affCache != null;
    AffinityTopologyVersion topVer = fut.topologyVersion();
    GridDhtAffinityAssignmentResponse res = fetchFut.get();
    if (res == null) {
        List<List<ClusterNode>> aff = affCache.calculate(topVer, fut.discoveryEvent(), fut.discoCache());
        affCache.initialize(topVer, aff);
    } else {
        List<List<ClusterNode>> idealAff = res.idealAffinityAssignment(cctx.discovery());
        if (idealAff != null)
            affCache.idealAssignment(idealAff);
        else {
            assert !affCache.centralizedAffinityFunction() || !lateAffAssign;
            affCache.calculate(topVer, fut.discoveryEvent(), fut.discoCache());
        }
        List<List<ClusterNode>> aff = res.affinityAssignment(cctx.discovery());
        assert aff != null : res;
        affCache.initialize(topVer, aff);
    }
}
Also used : AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridDhtAffinityAssignmentResponse(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtAffinityAssignmentResponse) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with AffinityTopologyVersion

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

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