Search in sources :

Example 61 with CacheGroupContext

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

the class WalRecoveryTxLogicalRecordsTest method testHistoricalRebalanceIterator.

/**
 * @throws Exception if failed.
 */
@Test
public void testHistoricalRebalanceIterator() throws Exception {
    System.setProperty(IgniteSystemProperties.IGNITE_PDS_WAL_REBALANCE_THRESHOLD, "0");
    extraCcfg = new CacheConfiguration(CACHE_NAME + "2");
    extraCcfg.setAffinity(new RendezvousAffinityFunction(false, PARTS));
    Ignite ignite = startGrid();
    try {
        ignite.cluster().active(true);
        GridCacheDatabaseSharedManager dbMgr = (GridCacheDatabaseSharedManager) ((IgniteEx) ignite).context().cache().context().database();
        dbMgr.waitForCheckpoint("test");
        // This number depends on wal history size.
        int entries = 25;
        IgniteCache<Integer, Integer> cache = ignite.cache(CACHE_NAME);
        IgniteCache<Integer, Integer> cache2 = ignite.cache(CACHE_NAME + "2");
        for (int i = 0; i < entries; i++) {
            // Put to partition 0.
            cache.put(i * PARTS, i * PARTS);
            // Put to partition 1.
            cache.put(i * PARTS + 1, i * PARTS + 1);
            // Put to another cache.
            cache2.put(i, i);
            dbMgr.waitForCheckpoint("test");
        }
        for (int i = 0; i < entries; i++) {
            assertEquals((Integer) (i * PARTS), cache.get(i * PARTS));
            assertEquals((Integer) (i * PARTS + 1), cache.get(i * PARTS + 1));
            assertEquals((Integer) (i), cache2.get(i));
        }
        CacheGroupContext grp = ((IgniteEx) ignite).context().cache().cacheGroup(CU.cacheId(CACHE_NAME));
        IgniteCacheOffheapManager offh = grp.offheap();
        AffinityTopologyVersion topVer = grp.affinity().lastVersion();
        IgniteDhtDemandedPartitionsMap map;
        for (int i = 0; i < entries; i++) {
            map = new IgniteDhtDemandedPartitionsMap();
            map.addHistorical(0, i, entries, PARTS);
            WALPointer ptr = reserveWalPointerForIterator(grp.shared());
            try (IgniteRebalanceIterator it = offh.rebalanceIterator(map, topVer)) {
                assertNotNull(it);
                assertTrue("Not historical for iteration: " + i, it.historical(0));
                for (int j = i; j < entries; j++) {
                    assertTrue("i=" + i + ", j=" + j, it.hasNextX());
                    CacheDataRow row = it.next();
                    assertEquals(j * PARTS, (int) row.key().value(grp.cacheObjectContext(), false));
                    assertEquals(j * PARTS, (int) row.value().value(grp.cacheObjectContext(), false));
                }
                assertFalse(it.hasNext());
            } finally {
                releaseWalPointerForIterator(grp.shared(), ptr);
            }
            map = new IgniteDhtDemandedPartitionsMap();
            map.addHistorical(1, i, entries, PARTS);
            ptr = reserveWalPointerForIterator(grp.shared());
            try (IgniteRebalanceIterator it = offh.rebalanceIterator(map, topVer)) {
                assertNotNull(it);
                assertTrue("Not historical for iteration: " + i, it.historical(1));
                for (int j = i; j < entries; j++) {
                    assertTrue(it.hasNextX());
                    CacheDataRow row = it.next();
                    assertEquals(j * PARTS + 1, (int) row.key().value(grp.cacheObjectContext(), false));
                    assertEquals(j * PARTS + 1, (int) row.value().value(grp.cacheObjectContext(), false));
                }
                assertFalse(it.hasNext());
            } finally {
                releaseWalPointerForIterator(grp.shared(), ptr);
            }
        }
        stopAllGrids();
        // Check that iterator is valid after restart.
        ignite = startGrid();
        ignite.cluster().active(true);
        grp = ((IgniteEx) ignite).context().cache().cacheGroup(CU.cacheId(CACHE_NAME));
        offh = grp.offheap();
        topVer = grp.affinity().lastVersion();
        for (int i = 0; i < entries; i++) {
            long start = System.currentTimeMillis();
            map = new IgniteDhtDemandedPartitionsMap();
            map.addHistorical(0, i, entries, PARTS);
            WALPointer ptr = reserveWalPointerForIterator(grp.shared());
            try (IgniteRebalanceIterator it = offh.rebalanceIterator(map, topVer)) {
                long end = System.currentTimeMillis();
                info("Time to get iterator: " + (end - start));
                assertTrue("Not historical for iteration: " + i, it.historical(0));
                assertNotNull(it);
                start = System.currentTimeMillis();
                for (int j = i; j < entries; j++) {
                    assertTrue("i=" + i + ", j=" + j, it.hasNextX());
                    CacheDataRow row = it.next();
                    assertEquals(j * PARTS, (int) row.key().value(grp.cacheObjectContext(), false));
                    assertEquals(j * PARTS, (int) row.value().value(grp.cacheObjectContext(), false));
                }
                end = System.currentTimeMillis();
                info("Time to iterate: " + (end - start));
                assertFalse(it.hasNext());
            } finally {
                releaseWalPointerForIterator(grp.shared(), ptr);
            }
            map = new IgniteDhtDemandedPartitionsMap();
            map.addHistorical(1, i, entries, PARTS);
            ptr = reserveWalPointerForIterator(grp.shared());
            try (IgniteRebalanceIterator it = offh.rebalanceIterator(map, topVer)) {
                assertNotNull(it);
                assertTrue("Not historical for iteration: " + i, it.historical(1));
                for (int j = i; j < entries; j++) {
                    assertTrue(it.hasNextX());
                    CacheDataRow row = it.next();
                    assertEquals(j * PARTS + 1, (int) row.key().value(grp.cacheObjectContext(), false));
                    assertEquals(j * PARTS + 1, (int) row.value().value(grp.cacheObjectContext(), false));
                }
                assertFalse(it.hasNext());
            } finally {
                releaseWalPointerForIterator(grp.shared(), ptr);
            }
        }
    } finally {
        stopAllGrids();
        System.clearProperty(IgniteSystemProperties.IGNITE_PDS_WAL_REBALANCE_THRESHOLD);
    }
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) GridCacheDatabaseSharedManager(org.apache.ignite.internal.processors.cache.persistence.GridCacheDatabaseSharedManager) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) IgniteRebalanceIterator(org.apache.ignite.internal.processors.cache.IgniteRebalanceIterator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteDhtDemandedPartitionsMap(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.IgniteDhtDemandedPartitionsMap) IgniteCacheOffheapManager(org.apache.ignite.internal.processors.cache.IgniteCacheOffheapManager) IgniteEx(org.apache.ignite.internal.IgniteEx) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) Ignite(org.apache.ignite.Ignite) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext) WALPointer(org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 62 with CacheGroupContext

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

the class GridCacheDatabaseSharedManager method partsForCheckpointHistorySearch.

/**
 * @return Map of group id -> Set parts.
 */
private Map<Integer, Set<Integer>> partsForCheckpointHistorySearch() {
    Map<Integer, Set<Integer>> part4CheckpointHistSearch = new HashMap<>();
    for (CacheGroupContext grp : cctx.cache().cacheGroups()) {
        if (grp.isLocal())
            continue;
        for (GridDhtLocalPartition part : grp.topology().currentLocalPartitions()) {
            if (part.state() != GridDhtPartitionState.OWNING || part.dataStore().fullSize() <= walRebalanceThreshold)
                continue;
            Set<Integer> parts = part4CheckpointHistSearch.get(grp.groupId());
            if (parts == null)
                part4CheckpointHistSearch.put(grp.groupId(), parts = new HashSet<>());
            parts.add(part.id());
        }
    }
    return part4CheckpointHistSearch;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Set(java.util.Set) HashSet(java.util.HashSet) ConcurrentLinkedHashMap(org.jsr166.ConcurrentLinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) GridDhtLocalPartition(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtLocalPartition) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext)

Example 63 with CacheGroupContext

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

the class GridCacheDatabaseSharedManager method restorePartitionState.

/**
 * @param partStates Partition states.
 * @throws IgniteCheckedException If failed to restore.
 */
private void restorePartitionState(Map<T2<Integer, Integer>, T2<Integer, Long>> partStates, Collection<Integer> ignoreGrps) throws IgniteCheckedException {
    for (CacheGroupContext grp : cctx.cache().cacheGroups()) {
        if (grp.isLocal() || !grp.affinityNode() || ignoreGrps.contains(grp.groupId())) {
            // Local cache has no partitions and its states.
            continue;
        }
        if (!grp.dataRegion().config().isPersistenceEnabled())
            continue;
        int grpId = grp.groupId();
        PageMemoryEx pageMem = (PageMemoryEx) grp.dataRegion().pageMemory();
        for (int i = 0; i < grp.affinity().partitions(); i++) {
            T2<Integer, Long> restore = partStates.get(new T2<>(grpId, i));
            if (storeMgr.exists(grpId, i)) {
                storeMgr.ensure(grpId, i);
                if (storeMgr.pages(grpId, i) <= 1)
                    continue;
                GridDhtLocalPartition part = grp.topology().forceCreatePartition(i);
                assert part != null;
                // TODO: https://issues.apache.org/jira/browse/IGNITE-6097
                grp.offheap().onPartitionInitialCounterUpdated(i, 0);
                checkpointReadLock();
                try {
                    long partMetaId = pageMem.partitionMetaPageId(grpId, i);
                    long partMetaPage = pageMem.acquirePage(grpId, partMetaId);
                    try {
                        long pageAddr = pageMem.writeLock(grpId, partMetaId, partMetaPage);
                        boolean changed = false;
                        try {
                            PagePartitionMetaIO io = PagePartitionMetaIO.VERSIONS.forPage(pageAddr);
                            if (restore != null) {
                                int stateId = restore.get1();
                                io.setPartitionState(pageAddr, (byte) stateId);
                                changed = updateState(part, stateId);
                                if (stateId == GridDhtPartitionState.OWNING.ordinal() || (stateId == GridDhtPartitionState.MOVING.ordinal() && part.initialUpdateCounter() < restore.get2())) {
                                    part.initialUpdateCounter(restore.get2());
                                    changed = true;
                                }
                            } else
                                updateState(part, (int) io.getPartitionState(pageAddr));
                        } finally {
                            pageMem.writeUnlock(grpId, partMetaId, partMetaPage, null, changed);
                        }
                    } finally {
                        pageMem.releasePage(grpId, partMetaId, partMetaPage);
                    }
                } finally {
                    checkpointReadUnlock();
                }
            } else if (restore != null) {
                GridDhtLocalPartition part = grp.topology().forceCreatePartition(i);
                assert part != null;
                // TODO: https://issues.apache.org/jira/browse/IGNITE-6097
                grp.offheap().onPartitionInitialCounterUpdated(i, 0);
                updateState(part, restore.get1());
            }
        }
        // After partition states are restored, it is necessary to update internal data structures in topology.
        grp.topology().afterStateRestored(grp.topology().lastTopologyChangeVersion());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PageMemoryEx(org.apache.ignite.internal.processors.cache.persistence.pagemem.PageMemoryEx) GridDhtLocalPartition(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtLocalPartition) PagePartitionMetaIO(org.apache.ignite.internal.processors.cache.persistence.tree.io.PagePartitionMetaIO) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext)

Example 64 with CacheGroupContext

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

the class IdleVerifyUtility method getUpdateCountersSnapshot.

/**
 * Gather updateCounters info.
 * Holds {@link org.apache.ignite.internal.processors.cache.PartitionUpdateCounter#copy} of update counters.
 *
 * @param ign Ignite instance.
 * @param grpIds Group Id`s.
 * @return Current groups distribution with update counters per partitions.
 */
public static Map<Integer, Map<Integer, PartitionUpdateCounter>> getUpdateCountersSnapshot(IgniteEx ign, Set<Integer> grpIds) {
    Map<Integer, Map<Integer, PartitionUpdateCounter>> partsWithCountersPerGrp = new HashMap<>();
    for (Integer grpId : grpIds) {
        CacheGroupContext grpCtx = ign.context().cache().cacheGroup(grpId);
        if (grpCtx == null)
            throw new GridNotIdleException("Group not found: " + grpId + "." + " Possible reasons: rebalance in progress or concurrent cache destroy.");
        GridDhtPartitionTopology top = grpCtx.topology();
        Map<Integer, PartitionUpdateCounter> partsWithCounters = partsWithCountersPerGrp.computeIfAbsent(grpId, k -> new HashMap<>());
        for (GridDhtLocalPartition part : top.currentLocalPartitions()) {
            if (part.state() != GridDhtPartitionState.OWNING)
                continue;
            @Nullable PartitionUpdateCounter updCntr = part.dataStore().partUpdateCounter();
            partsWithCounters.put(part.id(), updCntr == null ? null : updCntr.copy());
        }
    }
    return partsWithCountersPerGrp;
}
Also used : HashMap(java.util.HashMap) GridDhtPartitionTopology(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionTopology) PartitionUpdateCounter(org.apache.ignite.internal.processors.cache.PartitionUpdateCounter) GridDhtLocalPartition(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext) HashMap(java.util.HashMap) Map(java.util.Map) Nullable(org.jetbrains.annotations.Nullable)

Example 65 with CacheGroupContext

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

the class IdleVerifyUtility method formatUpdateCountersDiff.

/**
 * Prints diff between incoming update counters snapshots.
 *
 * @param ig Ignite instance.
 * @param diff Compared groups diff.
 * @return Formatted diff representation.
 */
public static String formatUpdateCountersDiff(IgniteEx ig, List<Integer> diff) {
    SB sb = null;
    if (!diff.isEmpty()) {
        sb = new SB();
        for (int grpId0 : diff) {
            if (sb.length() != 0)
                sb.a(", ");
            else
                sb.a("\"");
            DynamicCacheDescriptor desc = ig.context().cache().cacheDescriptor(grpId0);
            CacheGroupContext grpCtx = ig.context().cache().cacheGroup(desc == null ? grpId0 : desc.groupId());
            sb.a(grpCtx.cacheOrGroupName());
        }
        sb.a("\"");
    }
    return sb != null ? sb.toString() : "";
}
Also used : DynamicCacheDescriptor(org.apache.ignite.internal.processors.cache.DynamicCacheDescriptor) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext) SB(org.apache.ignite.internal.util.typedef.internal.SB)

Aggregations

CacheGroupContext (org.apache.ignite.internal.processors.cache.CacheGroupContext)103 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)31 IgniteEx (org.apache.ignite.internal.IgniteEx)29 Map (java.util.Map)27 HashMap (java.util.HashMap)24 IgniteException (org.apache.ignite.IgniteException)22 ArrayList (java.util.ArrayList)21 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)20 Test (org.junit.Test)20 GridDhtLocalPartition (org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition)19 List (java.util.List)17 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)17 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)16 GridDhtPartitionTopology (org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionTopology)16 HashSet (java.util.HashSet)13 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)12 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)12 GridCacheSharedContext (org.apache.ignite.internal.processors.cache.GridCacheSharedContext)12 Set (java.util.Set)11 Collection (java.util.Collection)10