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);
}
}
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;
}
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());
}
}
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;
}
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() : "";
}
Aggregations