Search in sources :

Example 1 with GridCacheSharedContext

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

the class IgniteH2Indexing method checkCacheIndexSegmentation.

/**
     * @throws IllegalStateException if segmented indices used with non-segmented indices.
     */
private void checkCacheIndexSegmentation(List<Integer> cacheIds) {
    if (cacheIds.isEmpty())
        // Nothing to check
        return;
    GridCacheSharedContext sharedCtx = ctx.cache().context();
    int expectedParallelism = 0;
    for (Integer cacheId : cacheIds) {
        GridCacheContext cctx = sharedCtx.cacheContext(cacheId);
        assert cctx != null;
        if (!cctx.isPartitioned())
            continue;
        if (expectedParallelism == 0)
            expectedParallelism = cctx.config().getQueryParallelism();
        else if (cctx.config().getQueryParallelism() != expectedParallelism) {
            throw new IllegalStateException("Using indexes with different parallelism levels in same query is " + "forbidden.");
        }
    }
}
Also used : IgniteSystemProperties.getInteger(org.apache.ignite.IgniteSystemProperties.getInteger) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) GridCacheSharedContext(org.apache.ignite.internal.processors.cache.GridCacheSharedContext)

Example 2 with GridCacheSharedContext

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

the class GridCommonAbstractTest method dumpCacheDebugInfo.

/**
     * @param ignite Node.
     */
public void dumpCacheDebugInfo(Ignite ignite) {
    GridKernalContext ctx = ((IgniteKernal) ignite).context();
    log.error("Cache information update [node=" + ignite.name() + ", client=" + ignite.configuration().isClientMode() + ']');
    GridCacheSharedContext cctx = ctx.cache().context();
    log.error("Pending transactions:");
    for (IgniteInternalTx tx : cctx.tm().activeTransactions()) log.error(">>> " + tx);
    log.error("Pending explicit locks:");
    for (GridCacheExplicitLockSpan lockSpan : cctx.mvcc().activeExplicitLocks()) log.error(">>> " + lockSpan);
    log.error("Pending cache futures:");
    for (GridCacheFuture<?> fut : cctx.mvcc().activeFutures()) log.error(">>> " + fut);
    log.error("Pending atomic cache futures:");
    for (GridCacheFuture<?> fut : cctx.mvcc().atomicFutures()) log.error(">>> " + fut);
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) IgniteInternalTx(org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx) GridKernalContext(org.apache.ignite.internal.GridKernalContext) GridCacheExplicitLockSpan(org.apache.ignite.internal.processors.cache.GridCacheExplicitLockSpan) GridCacheSharedContext(org.apache.ignite.internal.processors.cache.GridCacheSharedContext)

Example 3 with GridCacheSharedContext

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

the class IgniteTxStateImpl method addActiveCache.

/** {@inheritDoc} */
@Override
public void addActiveCache(GridCacheContext cacheCtx, boolean recovery, IgniteTxLocalAdapter tx) throws IgniteCheckedException {
    GridCacheSharedContext cctx = cacheCtx.shared();
    int cacheId = cacheCtx.cacheId();
    if (this.recovery != null && this.recovery != recovery)
        throw new IgniteCheckedException("Failed to enlist an entry to existing transaction " + "(cannot transact between recovery and non-recovery caches).");
    this.recovery = recovery;
    // Check if we can enlist new cache to transaction.
    if (!activeCacheIds.contains(cacheId)) {
        String err = cctx.verifyTxCompatibility(tx, activeCacheIds, cacheCtx);
        if (err != null) {
            StringBuilder cacheNames = new StringBuilder();
            int idx = 0;
            for (int i = 0; i < activeCacheIds.size(); i++) {
                int activeCacheId = activeCacheIds.get(i);
                cacheNames.append(cctx.cacheContext(activeCacheId).name());
                if (idx++ < activeCacheIds.size() - 1)
                    cacheNames.append(", ");
            }
            throw new IgniteCheckedException("Failed to enlist new cache to existing transaction (" + err + ") [activeCaches=[" + cacheNames + "]" + ", cacheName=" + cacheCtx.name() + ", cacheSystem=" + cacheCtx.systemTx() + ", txSystem=" + tx.system() + ']');
        } else
            activeCacheIds.add(cacheId);
        if (activeCacheIds.size() == 1)
            tx.activeCachesDeploymentEnabled(cacheCtx.deploymentEnabled());
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCacheSharedContext(org.apache.ignite.internal.processors.cache.GridCacheSharedContext)

Example 4 with GridCacheSharedContext

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

the class CacheLateAffinityAssignmentTest method calculateAffinity.

/**
     * @param topVer Topology version.
     * @param filterByRcvd If {@code true} filters caches by 'receivedFrom' property.
     * @param cur Optional current affinity.
     * @throws Exception If failed.
     * @return {@code True} if some primary node changed comparing to given affinity.
     */
private boolean calculateAffinity(long topVer, boolean filterByRcvd, @Nullable Map<String, List<List<ClusterNode>>> cur) throws Exception {
    List<Ignite> all = G.allGrids();
    IgniteKernal ignite = (IgniteKernal) Collections.min(all, new Comparator<Ignite>() {

        @Override
        public int compare(Ignite n1, Ignite n2) {
            return Long.compare(n1.cluster().localNode().order(), n2.cluster().localNode().order());
        }
    });
    assert all.size() > 0;
    Map<Integer, List<List<ClusterNode>>> assignments = idealAff.get(topVer);
    if (assignments == null)
        idealAff.put(topVer, assignments = new HashMap<>());
    GridKernalContext ctx = ignite.context();
    GridCacheSharedContext cctx = ctx.cache().context();
    AffinityTopologyVersion topVer0 = new AffinityTopologyVersion(topVer);
    cctx.discovery().topologyFuture(topVer).get();
    List<GridDhtPartitionsExchangeFuture> futs = cctx.exchange().exchangeFutures();
    DiscoveryEvent evt = null;
    long stopTime = System.currentTimeMillis() + 10_000;
    boolean primaryChanged = false;
    do {
        for (int i = futs.size() - 1; i >= 0; i--) {
            GridDhtPartitionsExchangeFuture fut = futs.get(i);
            if (fut.topologyVersion().equals(topVer0)) {
                evt = fut.discoveryEvent();
                break;
            }
        }
        if (evt == null) {
            U.sleep(500);
            futs = cctx.exchange().exchangeFutures();
        } else
            break;
    } while (System.currentTimeMillis() < stopTime);
    assertNotNull("Failed to find exchange future:", evt);
    Collection<ClusterNode> allNodes = ctx.discovery().cacheNodes(topVer0);
    for (DynamicCacheDescriptor cacheDesc : ctx.cache().cacheDescriptors()) {
        if (assignments.get(cacheDesc.cacheId()) != null)
            continue;
        if (filterByRcvd && cacheDesc.receivedFrom() != null && ctx.discovery().node(topVer0, cacheDesc.receivedFrom()) == null)
            continue;
        AffinityFunction func = cacheDesc.cacheConfiguration().getAffinity();
        func = cctx.cache().clone(func);
        cctx.kernalContext().resource().injectGeneric(func);
        List<ClusterNode> affNodes = new ArrayList<>();
        IgnitePredicate<ClusterNode> filter = cacheDesc.cacheConfiguration().getNodeFilter();
        for (ClusterNode n : allNodes) {
            if (!CU.clientNode(n) && (filter == null || filter.apply(n)))
                affNodes.add(n);
        }
        Collections.sort(affNodes, GridNodeOrderComparator.INSTANCE);
        AffinityFunctionContext affCtx = new GridAffinityFunctionContextImpl(affNodes, previousAssignment(topVer, cacheDesc.cacheId()), evt, topVer0, cacheDesc.cacheConfiguration().getBackups());
        List<List<ClusterNode>> assignment = func.assignPartitions(affCtx);
        if (cur != null) {
            List<List<ClusterNode>> prev = cur.get(cacheDesc.cacheConfiguration().getName());
            assertEquals(prev.size(), assignment.size());
            if (!primaryChanged) {
                for (int p = 0; p < prev.size(); p++) {
                    List<ClusterNode> nodes0 = prev.get(p);
                    List<ClusterNode> nodes1 = assignment.get(p);
                    if (nodes0.size() > 0 && nodes1.size() > 0) {
                        ClusterNode p0 = nodes0.get(0);
                        ClusterNode p1 = nodes1.get(0);
                        if (allNodes.contains(p0) && !p0.equals(p1)) {
                            primaryChanged = true;
                            log.info("Primary changed [cache=" + cacheDesc.cacheConfiguration().getName() + ", part=" + p + ", prev=" + F.nodeIds(nodes0) + ", new=" + F.nodeIds(nodes1) + ']');
                            break;
                        }
                    }
                }
            }
        }
        assignments.put(cacheDesc.cacheId(), assignment);
    }
    return primaryChanged;
}
Also used : GridKernalContext(org.apache.ignite.internal.GridKernalContext) ArrayList(java.util.ArrayList) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) GridCacheSharedContext(org.apache.ignite.internal.processors.cache.GridCacheSharedContext) GridNodeOrderComparator(org.apache.ignite.internal.GridNodeOrderComparator) Comparator(java.util.Comparator) Ignite(org.apache.ignite.Ignite) List(java.util.List) ArrayList(java.util.ArrayList) ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteKernal(org.apache.ignite.internal.IgniteKernal) GridDhtPartitionsExchangeFuture(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsExchangeFuture) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) DynamicCacheDescriptor(org.apache.ignite.internal.processors.cache.DynamicCacheDescriptor) AffinityFunctionContext(org.apache.ignite.cache.affinity.AffinityFunctionContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridAffinityFunctionContextImpl(org.apache.ignite.internal.processors.affinity.GridAffinityFunctionContextImpl) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) AffinityFunction(org.apache.ignite.cache.affinity.AffinityFunction)

Example 5 with GridCacheSharedContext

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

the class IgniteTxOriginatingNodeFailureAbstractSelfTest method testTxOriginatingNodeFails.

/**
     * @param keys Keys to update.
     * @param partial Flag indicating whether to simulate partial prepared state.
     * @throws Exception If failed.
     */
protected void testTxOriginatingNodeFails(Collection<Integer> keys, final boolean partial) throws Exception {
    assertFalse(keys.isEmpty());
    final Collection<IgniteKernal> grids = new ArrayList<>();
    ClusterNode txNode = grid(originatingNode()).localNode();
    for (int i = 1; i < gridCount(); i++) grids.add((IgniteKernal) grid(i));
    final Map<Integer, String> map = new HashMap<>();
    final String initVal = "initialValue";
    for (Integer key : keys) {
        grid(originatingNode()).cache(DEFAULT_CACHE_NAME).put(key, initVal);
        map.put(key, String.valueOf(key));
    }
    Map<Integer, Collection<ClusterNode>> nodeMap = new HashMap<>();
    info("Node being checked: " + grid(1).localNode().id());
    for (Integer key : keys) {
        Collection<ClusterNode> nodes = new ArrayList<>();
        nodes.addAll(grid(1).affinity(DEFAULT_CACHE_NAME).mapKeyToPrimaryAndBackups(key));
        nodes.remove(txNode);
        nodeMap.put(key, nodes);
    }
    info("Starting optimistic tx " + "[values=" + map + ", topVer=" + (grid(1)).context().discovery().topologyVersion() + ']');
    if (partial)
        ignoreMessages(grid(1).localNode().id(), ignoreMessageClass());
    final Ignite txIgniteNode = G.ignite(txNode.id());
    GridTestUtils.runAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            IgniteCache<Integer, String> cache = txIgniteNode.cache(DEFAULT_CACHE_NAME);
            assertNotNull(cache);
            TransactionProxyImpl tx = (TransactionProxyImpl) txIgniteNode.transactions().txStart();
            GridNearTxLocal txEx = tx.tx();
            assertTrue(txEx.optimistic());
            cache.putAll(map);
            try {
                txEx.prepareNearTxLocal().get(3, TimeUnit.SECONDS);
            } catch (IgniteFutureTimeoutCheckedException ignored) {
                info("Failed to wait for prepare future completion: " + partial);
            }
            return null;
        }
    }).get();
    info("Stopping originating node " + txNode);
    G.stop(G.ignite(txNode.id()).name(), true);
    info("Stopped grid, waiting for transactions to complete.");
    boolean txFinished = GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            for (IgniteKernal g : grids) {
                GridCacheSharedContext<Object, Object> ctx = g.context().cache().context();
                int txNum = ctx.tm().idMapSize();
                if (txNum != 0)
                    return false;
            }
            return true;
        }
    }, 10000);
    assertTrue(txFinished);
    info("Transactions finished.");
    for (Map.Entry<Integer, Collection<ClusterNode>> e : nodeMap.entrySet()) {
        final Integer key = e.getKey();
        final String val = map.get(key);
        assertFalse(e.getValue().isEmpty());
        for (ClusterNode node : e.getValue()) {
            compute(G.ignite(node.id()).cluster().forNode(node)).call(new IgniteCallable<Void>() {

                /** */
                @IgniteInstanceResource
                private Ignite ignite;

                @Override
                public Void call() throws Exception {
                    IgniteCache<Integer, String> cache = ignite.cache(DEFAULT_CACHE_NAME);
                    assertNotNull(cache);
                    assertEquals(partial ? initVal : val, cache.localPeek(key));
                    return null;
                }
            });
        }
    }
    for (Map.Entry<Integer, String> e : map.entrySet()) {
        for (Ignite g : G.allGrids()) {
            UUID locNodeId = g.cluster().localNode().id();
            assertEquals("Check failed for node: " + locNodeId, partial ? initVal : e.getValue(), g.cache(DEFAULT_CACHE_NAME).get(e.getKey()));
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GridCacheSharedContext(org.apache.ignite.internal.processors.cache.GridCacheSharedContext) Callable(java.util.concurrent.Callable) IgniteCallable(org.apache.ignite.lang.IgniteCallable) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID) ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteKernal(org.apache.ignite.internal.IgniteKernal) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) IgniteCache(org.apache.ignite.IgniteCache) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) IgniteException(org.apache.ignite.IgniteException) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map) TransactionProxyImpl(org.apache.ignite.internal.processors.cache.transactions.TransactionProxyImpl)

Aggregations

GridCacheSharedContext (org.apache.ignite.internal.processors.cache.GridCacheSharedContext)5 IgniteKernal (org.apache.ignite.internal.IgniteKernal)3 ArrayList (java.util.ArrayList)2 Ignite (org.apache.ignite.Ignite)2 ClusterNode (org.apache.ignite.cluster.ClusterNode)2 GridKernalContext (org.apache.ignite.internal.GridKernalContext)2 Collection (java.util.Collection)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 UUID (java.util.UUID)1 Callable (java.util.concurrent.Callable)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 IgniteCache (org.apache.ignite.IgniteCache)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 IgniteException (org.apache.ignite.IgniteException)1 IgniteSystemProperties.getInteger (org.apache.ignite.IgniteSystemProperties.getInteger)1 AffinityFunction (org.apache.ignite.cache.affinity.AffinityFunction)1 AffinityFunctionContext (org.apache.ignite.cache.affinity.AffinityFunctionContext)1