Search in sources :

Example 56 with GridDhtPartitionTopology

use of org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionTopology in project ignite by apache.

the class GridCacheRebalancingSyncSelfTest method checkPartitionMapExchangeFinished.

/**
 */
public static void checkPartitionMapExchangeFinished() {
    for (Ignite g : G.allGrids()) {
        IgniteKernal g0 = (IgniteKernal) g;
        for (IgniteCacheProxy<?, ?> c : g0.context().cache().jcaches()) {
            CacheConfiguration cfg = c.context().config();
            if (cfg.getCacheMode() != LOCAL && cfg.getRebalanceMode() != NONE) {
                GridDhtCacheAdapter<?, ?> dht = dht(c);
                GridDhtPartitionTopology top = dht.topology();
                List<GridDhtLocalPartition> locs = top.localPartitions();
                for (GridDhtLocalPartition loc : locs) {
                    GridDhtPartitionState actl = loc.state();
                    boolean res = GridDhtPartitionState.OWNING.equals(actl);
                    if (!res)
                        printPartitionState(c);
                    assertTrue("Wrong local partition state part=" + loc.id() + ", should be OWNING [state=" + actl + "], node=" + g0.name() + " cache=" + c.getName(), res);
                    Collection<ClusterNode> affNodes = g0.affinity(cfg.getName()).mapPartitionToPrimaryAndBackups(loc.id());
                    assertTrue(affNodes.contains(g0.localNode()));
                }
                for (Ignite remote : G.allGrids()) {
                    IgniteKernal remote0 = (IgniteKernal) remote;
                    IgniteCacheProxy<?, ?> remoteC = remote0.context().cache().jcache(cfg.getName());
                    GridDhtCacheAdapter<?, ?> remoteDht = dht(remoteC);
                    GridDhtPartitionTopology remoteTop = remoteDht.topology();
                    GridDhtPartitionMap pMap = remoteTop.partitionMap(true).get(((IgniteKernal) g).localNodeId());
                    assertEquals(pMap.size(), locs.size());
                    for (Map.Entry entry : pMap.entrySet()) {
                        assertTrue("Wrong remote partition state part=" + entry.getKey() + ", should be OWNING [state=" + entry.getValue() + "], node=" + remote.name() + " cache=" + c.getName(), entry.getValue() == GridDhtPartitionState.OWNING);
                    }
                    for (GridDhtLocalPartition loc : locs) assertTrue(pMap.containsKey(loc.id()));
                }
            }
        }
    }
    log.info("checkPartitionMapExchangeFinished finished");
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteKernal(org.apache.ignite.internal.IgniteKernal) GridDhtPartitionTopology(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionTopology) GridDhtPartitionMap(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionMap) GridDhtPartitionState(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionState) Ignite(org.apache.ignite.Ignite) GridDhtLocalPartition(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition) Map(java.util.Map) GridDhtPartitionMap(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionMap) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 57 with GridDhtPartitionTopology

use of org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionTopology in project ignite by apache.

the class IgniteClientCacheStartFailoverTest method testRebalanceState.

/**
 * @throws Exception If failed.
 */
@Test
public void testRebalanceState() throws Exception {
    final int SRVS = 3;
    startGrids(SRVS);
    List<String> cacheNames = startCaches(ignite(0), 100);
    Ignite c = startClientGrid(SRVS);
    assertTrue(c.configuration().isClientMode());
    awaitPartitionMapExchange();
    TestRecordingCommunicationSpi.spi(ignite(0)).blockMessages(new IgniteBiPredicate<ClusterNode, Message>() {

        @Override
        public boolean apply(ClusterNode clusterNode, Message msg) {
            return msg instanceof GridDhtPartitionsFullMessage && ((GridDhtPartitionsFullMessage) msg).exchangeId() == null;
        }
    });
    startGrid(SRVS + 1);
    for (String cacheName : cacheNames) c.cache(cacheName);
    // Will switch to ideal topology but some partitions are not evicted yet.
    for (int i = 0; i < SRVS + 1; i++) {
        AffinityTopologyVersion topVer = new AffinityTopologyVersion(SRVS + 2, 1);
        IgniteKernal node = (IgniteKernal) ignite(i);
        for (String cacheName : cacheNames) {
            GridDhtPartitionTopology top = node.cachex(cacheName).context().topology();
            waitForReadyTopology(top, topVer);
            assertEquals(topVer, top.readyTopologyVersion());
        }
    }
    TestRecordingCommunicationSpi.spi(ignite(0)).stopBlock();
    // Trigger eviction.
    awaitPartitionMapExchange();
    for (int i = 0; i < SRVS + 1; i++) {
        final AffinityTopologyVersion topVer = new AffinityTopologyVersion(SRVS + 2, 1);
        final IgniteKernal node = (IgniteKernal) ignite(i);
        for (String cacheName : cacheNames) {
            final GridDhtPartitionTopology top = node.cachex(cacheName).context().topology();
            GridTestUtils.waitForCondition(new GridAbsPredicate() {

                @Override
                public boolean apply() {
                    return top.rebalanceFinished(topVer);
                }
            }, 5000);
            assertTrue(top.rebalanceFinished(topVer));
        }
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteKernal(org.apache.ignite.internal.IgniteKernal) GridDhtPartitionsFullMessage(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsFullMessage) Message(org.apache.ignite.plugin.extensions.communication.Message) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridDhtPartitionTopology(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionTopology) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) GridDhtPartitionsFullMessage(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsFullMessage) Ignite(org.apache.ignite.Ignite) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 58 with GridDhtPartitionTopology

use of org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionTopology in project ignite by apache.

the class CacheRentingStateRepairTest method testRentingStateRepairAfterRestart.

/**
 * Tests partition is properly evicted when node is restarted in the middle of the eviction.
 */
@Test
public void testRentingStateRepairAfterRestart() throws Exception {
    try {
        IgniteEx g0 = startGrid(0);
        g0.cluster().baselineAutoAdjustEnabled(false);
        startGrid(1);
        g0.cluster().active(true);
        awaitPartitionMapExchange();
        List<Integer> parts = evictingPartitionsAfterJoin(g0, g0.cache(DEFAULT_CACHE_NAME), 20);
        int delayEvictPart = parts.get(0);
        int k = 0;
        while (g0.affinity(DEFAULT_CACHE_NAME).partition(k) != delayEvictPart) k++;
        g0.cache(DEFAULT_CACHE_NAME).put(k, k);
        GridDhtPartitionTopology top = dht(g0.cache(DEFAULT_CACHE_NAME)).topology();
        GridDhtLocalPartition part = top.localPartition(delayEvictPart);
        assertNotNull(part);
        // Prevent eviction.
        part.reserve();
        startGrid(2);
        g0.cluster().setBaselineTopology(3);
        // Wait until all is evicted except first partition.
        assertTrue("Failed to wait for partition eviction: reservedPart=" + part.id() + ", otherParts=" + top.localPartitions().stream().map(p -> "[id=" + p.id() + ", state=" + p.state() + ']').collect(Collectors.toList()), waitForCondition(() -> {
            for (int i = 0; i < parts.size(); i++) {
                if (delayEvictPart == i)
                    // Skip reserved partition.
                    continue;
                Integer p = parts.get(i);
                @Nullable GridDhtLocalPartition locPart = top.localPartition(p);
                assertNotNull(locPart);
                if (locPart.state() != GridDhtPartitionState.EVICTED)
                    return false;
            }
            return true;
        }, 5000));
        /**
         * Force renting state before node stop.
         * This also could be achieved by stopping node just after RENTING state is set.
         */
        part.setState(GridDhtPartitionState.RENTING);
        assertEquals(GridDhtPartitionState.RENTING, part.state());
        stopGrid(0);
        g0 = startGrid(0);
        awaitPartitionMapExchange();
        part = dht(g0.cache(DEFAULT_CACHE_NAME)).topology().localPartition(delayEvictPart);
        assertNotNull(part);
        final GridDhtLocalPartition finalPart = part;
        CountDownLatch evictLatch = new CountDownLatch(1);
        part.rent().listen(new IgniteInClosure<IgniteInternalFuture<?>>() {

            @Override
            public void apply(IgniteInternalFuture<?> fut) {
                assertEquals(GridDhtPartitionState.EVICTED, finalPart.state());
                evictLatch.countDown();
            }
        });
        assertTrue("Failed to wait for partition eviction after restart", evictLatch.await(5_000, TimeUnit.MILLISECONDS));
        awaitPartitionMapExchange(true, true, null);
    } finally {
        stopAllGrids();
    }
}
Also used : IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) WALMode(org.apache.ignite.configuration.WALMode) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) GridTestUtils.waitForCondition(org.apache.ignite.testframework.GridTestUtils.waitForCondition) U(org.apache.ignite.internal.util.typedef.internal.U) IgniteEx(org.apache.ignite.internal.IgniteEx) Test(org.junit.Test) Collectors(java.util.stream.Collectors) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) GridDhtLocalPartition(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) GridDhtPartitionTopology(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionTopology) X(org.apache.ignite.internal.util.typedef.X) DataStorageConfiguration(org.apache.ignite.configuration.DataStorageConfiguration) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) GridDhtPartitionState(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionState) GridDhtPartitionTopologyImpl(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionTopologyImpl) DataRegionConfiguration(org.apache.ignite.configuration.DataRegionConfiguration) GridDhtPartitionTopology(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionTopology) IgniteEx(org.apache.ignite.internal.IgniteEx) GridDhtLocalPartition(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 59 with GridDhtPartitionTopology

use of org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionTopology in project ignite by apache.

the class CachePartitionLossWithRestartsTest method waitForDetection.

/**
 * @param node Node.
 * @param topVer Topology version.
 */
private GridDhtPartitionTopology waitForDetection(IgniteEx node, AffinityTopologyVersion topVer) throws Exception {
    GridCacheSharedContext<Object, Object> cctx = node.context().cache().context();
    CacheGroupDescriptor desc = cctx.affinity().cacheGroups().get(CU.cacheId(DEFAULT_CACHE_NAME));
    CacheGroupContext grp = cctx.cache().cacheGroup(desc.groupId());
    GridDhtPartitionTopology top = grp != null ? grp.topology() : cctx.exchange().clientTopology(desc.groupId(), null);
    cctx.exchange().affinityReadyFuture(topVer).get();
    cctx.exchange().lastTopologyFuture().get();
    return top;
}
Also used : GridDhtPartitionTopology(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionTopology) CacheGroupDescriptor(org.apache.ignite.internal.processors.cache.CacheGroupDescriptor) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext)

Example 60 with GridDhtPartitionTopology

use of org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionTopology in project ignite by apache.

the class CacheBaselineTopologyTest method testTopologyChangesWithFixedBaseline.

/**
 * @throws Exception If failed.
 */
@Test
public void testTopologyChangesWithFixedBaseline() throws Exception {
    startGrids(NODE_COUNT);
    IgniteEx ignite = grid(0);
    ignite.cluster().baselineAutoAdjustEnabled(false);
    ignite.cluster().active(true);
    awaitPartitionMapExchange();
    Map<ClusterNode, Ignite> nodes = new HashMap<>();
    for (int i = 0; i < NODE_COUNT; i++) {
        Ignite ig = grid(i);
        nodes.put(ig.cluster().localNode(), ig);
    }
    IgniteCache<Integer, Integer> cache = ignite.createCache(new CacheConfiguration<Integer, Integer>().setName(CACHE_NAME).setCacheMode(PARTITIONED).setBackups(1).setPartitionLossPolicy(READ_ONLY_SAFE));
    int key = -1;
    for (int k = 0; k < 100_000; k++) {
        if (!ignite.affinity(CACHE_NAME).mapKeyToPrimaryAndBackups(k).contains(ignite.localNode())) {
            key = k;
            break;
        }
    }
    assert key >= 0;
    int part = ignite.affinity(CACHE_NAME).partition(key);
    Collection<ClusterNode> initialMapping = ignite.affinity(CACHE_NAME).mapKeyToPrimaryAndBackups(key);
    assert initialMapping.size() == 2 : initialMapping;
    ignite.cluster().setBaselineTopology(baselineNodes(nodes.keySet()));
    awaitPartitionMapExchange();
    cache.put(key, 1);
    Collection<ClusterNode> mapping = ignite.affinity(CACHE_NAME).mapKeyToPrimaryAndBackups(key);
    assert initialMapping.size() == mapping.size() : mapping;
    assert initialMapping.containsAll(mapping) : mapping;
    IgniteEx newIgnite = startGrid(4);
    awaitPartitionMapExchange();
    mapping = ignite.affinity(CACHE_NAME).mapKeyToPrimaryAndBackups(key);
    assert initialMapping.size() == mapping.size() : mapping;
    assert initialMapping.containsAll(mapping) : mapping;
    mapping = newIgnite.affinity(CACHE_NAME).mapKeyToPrimaryAndBackups(key);
    assert initialMapping.size() == mapping.size() : mapping;
    assert initialMapping.containsAll(mapping) : mapping;
    Set<String> stoppedNodeNames = new HashSet<>();
    ClusterNode node = mapping.iterator().next();
    stoppedNodeNames.add(nodes.get(node).name());
    nodes.get(node).close();
    nodes.remove(node);
    awaitPartitionMapExchange(true, true, null);
    mapping = ignite.affinity(CACHE_NAME).mapKeyToPrimaryAndBackups(key);
    assert mapping.size() == 1 : mapping;
    assert initialMapping.containsAll(mapping);
    node = mapping.iterator().next();
    stoppedNodeNames.add(nodes.get(node).name());
    nodes.get(node).close();
    nodes.remove(node);
    awaitPartitionMapExchange();
    mapping = ignite.affinity(CACHE_NAME).mapKeyToPrimaryAndBackups(key);
    assert mapping.isEmpty() : mapping;
    GridDhtPartitionTopology topology = ignite.cachex(CACHE_NAME).context().topology();
    assert topology.lostPartitions().contains(part);
    for (String nodeName : stoppedNodeNames) {
        startGrid(nodeName);
    }
    assert ignite.cluster().nodes().size() == NODE_COUNT + 1;
    mapping = ignite.affinity(CACHE_NAME).mapKeyToPrimaryAndBackups(key);
    assert initialMapping.size() == mapping.size() : mapping;
    for (ClusterNode n1 : initialMapping) {
        boolean found = false;
        for (ClusterNode n2 : mapping) {
            if (n2.consistentId().equals(n1.consistentId())) {
                found = true;
                break;
            }
        }
        assert found;
    }
    ignite.resetLostPartitions(Collections.singleton(CACHE_NAME));
    cache.put(key, 2);
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) DetachedClusterNode(org.apache.ignite.internal.cluster.DetachedClusterNode) HashMap(java.util.HashMap) GridDhtPartitionTopology(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionTopology) IgniteEx(org.apache.ignite.internal.IgniteEx) Ignite(org.apache.ignite.Ignite) HashSet(java.util.HashSet) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

GridDhtPartitionTopology (org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionTopology)64 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)24 GridDhtLocalPartition (org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition)21 ClusterNode (org.apache.ignite.cluster.ClusterNode)20 Map (java.util.Map)18 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)18 CacheGroupContext (org.apache.ignite.internal.processors.cache.CacheGroupContext)17 HashMap (java.util.HashMap)15 ArrayList (java.util.ArrayList)14 Ignite (org.apache.ignite.Ignite)14 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)12 Test (org.junit.Test)12 IgniteEx (org.apache.ignite.internal.IgniteEx)11 UUID (java.util.UUID)10 IgniteKernal (org.apache.ignite.internal.IgniteKernal)10 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)9 IgniteException (org.apache.ignite.IgniteException)9 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)9 GridDhtPartitionMap (org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionMap)9 HashSet (java.util.HashSet)8