alex-plekhanov commented on code in PR #13554:
URL: https://github.com/apache/ignite/pull/13554#discussion_r4523062467
##########
modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/freelist/AbstractFreeList.java:
##########
@@ -1237,46 +1247,68 @@ public int emptyDataPages() {
long backoffNanos = EVICTION_BACKOFF_START_NANOS;
- long lastProgressMillis = U.currentTimeMillis();
+ // Set by the first iteration.
+ long lastProgressMillis = 0;
- // Deficit before the last eviction attempt, 0 until the first one.
- long prevDeficit = 0;
+ // Only a new minimum is a progress: the deficit also jitters with the
reservations of other inserts.
+ long minDeficit = Long.MAX_VALUE;
// Eviction counter before the last eviction attempt.
- long prevEvicted = 0;
+ long prevEvicted = evictedCnt.get();
+
+ // The part of the claim already added to the reserved pages (see
accumulating).
+ long accumulated = 0;
+
+ boolean accumulator = false;
- while (true) {
- long reserved = reservedPages.get();
- long deficit = deficit(claimPages, limitPages, reserved);
+ try {
+ while (true) {
+ long reserved = reservedPages.get();
+ long deficit = deficit(claimPages - accumulated, limitPages,
reserved);
- if (deficit <= 0) {
- if (reservedPages.compareAndSet(reserved, reserved +
claimPages)) {
- clearStalled();
+ if (deficit <= 0) {
+ if (reservedPages.compareAndSet(reserved, reserved +
claimPages - accumulated)) {
+ clearStalled(prevEvicted);
- return claimPages;
+ // The whole claim now belongs to the caller.
Review Comment:
Let's also add comment why we need to clear local variable (to skip
releasing in finally block)
##########
modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/paged/PageEvictionRowSizeBinaryObjectTest.java:
##########
@@ -0,0 +1,100 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.cache.eviction.paged;
+
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.IgniteDataStreamer;
+import org.apache.ignite.cache.CacheAtomicityMode;
+import org.apache.ignite.cache.CacheMode;
+import org.apache.ignite.cache.CacheWriteSynchronizationMode;
+import org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.DataPageEvictionMode;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.junit.Test;
+
+/**
+ * {@code GridCacheMapEntry#rowSize()} must not throw NPE when the value is an
unmarshalled
+ * {@code BinaryObjectImpl} or a POJO whose key has not yet been marshalled on
the reservation path.
+ */
+public class PageEvictionRowSizeBinaryObjectTest extends
PageEvictionAbstractTest {
+ /** {@inheritDoc} */
+ @Override protected IgniteConfiguration getConfiguration(String gridName)
throws Exception {
+ return setEvictionMode(DataPageEvictionMode.RANDOM_2_LRU,
super.getConfiguration(gridName));
+ }
+
+ /** {@inheritDoc} */
+ @Override protected void afterTest() throws Exception {
+ super.afterTest();
+
+ stopAllGrids();
+ }
+
+ /** */
+ @Test
+ public void testPojoPutTxDoesNotNpeInRowSize() throws Exception {
+ IgniteEx ignite = startGrid(0);
+
+ IgniteCache<Integer, TestObject> cache = ignite.createCache(
+ new CacheConfiguration<Integer, TestObject>(DEFAULT_CACHE_NAME)
+ .setAffinity(new RendezvousAffinityFunction(false, 32))
+ .setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL)
+ .setCacheMode(CacheMode.REPLICATED)
+
.setWriteSynchronizationMode(CacheWriteSynchronizationMode.PRIMARY_SYNC));
+
+ int entries = 15_000;
Review Comment:
Maybe calculate entries as formula of SIZE or totalPages? It's not clear
from code that 15_000 is enough to start eviction.
##########
modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/paged/PageEvictionWithExpiryPolicyAbstractTest.java:
##########
@@ -0,0 +1,139 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.cache.eviction.paged;
+
+import java.util.Arrays;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.IgniteInternalFuture;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.junit.Test;
+
+import static org.apache.ignite.testframework.GridTestUtils.waitForCondition;
+
+/** Tests the synergy between ExpiryPolicy (TTL cleanup) and size-aware page
eviction on an in-memory data region. */
+public abstract class PageEvictionWithExpiryPolicyAbstractTest extends
PageEvictionAbstractTest {
+ /** Short TTL applied to some entries. */
+ private static final long TTL = 2000;
+
+ /** {@inheritDoc} */
+ @Override protected void afterTest() throws Exception {
+ super.afterTest();
+
+ stopAllGrids();
+
+ assertNoOomFailures();
+ }
+
+ /** */
+ @Test
+ public void testLargePutWithExpiryNoDeadlock() throws Exception {
+ IgniteEx ignite = startGrid(1);
+
+ IgniteCache<Integer, Object> plainCache = createCache(ignite,
"plain-cache");
+ IgniteCache<Integer, Object> ttlCache = createCache(ignite,
"ttl-cache", TTL, false);
+
+ int smallEntries = 1_000;
+
+ for (int i = 0; i < smallEntries; i++)
+ plainCache.put(i, new byte[16 * 1024]);
+
+ assertFalse("Eviction must not have started during pre-fill",
isEvictionsStarted(ignite));
+
+ int ttlEntries = 100;
+ byte[] ttlVal = new byte[pageSize(ignite) - 200];
+
+ for (int i = 0; i < ttlEntries; i++)
+ ttlCache.put(i, ttlVal);
+
+ int largeRowsPerThread = 40;
+ byte[] largeVal = new byte[1024 * 1024];
+
+ CountDownLatch startLatch = new CountDownLatch(1);
+ AtomicInteger threadIdx = new AtomicInteger();
+
+ IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(()
-> {
+ U.awaitQuiet(startLatch);
+
+ int idx = threadIdx.getAndIncrement();
+
+ for (int k = 0; k < largeRowsPerThread; k++)
+ plainCache.put(smallEntries + idx * largeRowsPerThread + k,
largeVal);
+ }, 10, "paged-writer");
+
+ startLatch.countDown();
+
+ assertTrue("TTL entries must expire after TTL duration",
+ waitForCondition(() -> {
+ for (int i = 0; i < ttlEntries; i++) {
+ if (ttlCache.get(i) != null)
+ return false;
+ }
+
+ return true;
+ }, TTL + 10_000));
+
+ fut.get(TimeUnit.MINUTES.toMillis(3));
+
+ assertTrue("Eviction must have started during concurrent large
writes", isEvictionsStarted(ignite));
+ }
+
+ /** */
+ @Test
+ public void testTtlFreedSpaceAccountedForByEviction() throws Exception {
+ IgniteEx ignite = startGrid(1);
+
+ IgniteCache<Integer, Object> plainCache = createCache(ignite,
"plain-cache");
+ IgniteCache<Integer, Object> ttlCache = createCache(ignite,
"ttl-cache", TTL, false);
+
+ // Pre-fill + two 5% TTL rows + the empty pages pool must stay below
the 0.9 eviction threshold, which applies
Review Comment:
Current eviction threshold is 0.95, so 0.75 + 0.05 * 2 + 0.05*2 =0.95 - can
be treated as value below eviction threshold. Let's increase some of
coefficients
##########
modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/paged/PageEvictionDisabledEvictionSelfTest.java:
##########
@@ -0,0 +1,174 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.cache.eviction.paged;
+
+import java.util.HashSet;
+import java.util.Set;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.cache.CacheAtomicityMode;
+import org.apache.ignite.cache.CacheWriteSynchronizationMode;
+import org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.DataPageEvictionMode;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.mem.IgniteOutOfMemoryException;
+import org.apache.ignite.internal.processors.cache.persistence.DataRegion;
+import
org.apache.ignite.internal.processors.cache.persistence.freelist.FreeList;
+import org.apache.ignite.internal.util.typedef.X;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.junit.Test;
+
+/**
+ * Checks that the size-aware page reservation does not change the behavior of
an in-memory data region with disabled
+ * page eviction: inserts (including read-through loads) are still rejected by
the out-of-memory check at the threshold,
+ * while removals still succeed and do not trigger the failure handler.
+ */
+public class PageEvictionDisabledEvictionSelfTest extends
PageEvictionAbstractTest {
+ /** {@inheritDoc} */
+ @Override protected IgniteConfiguration getConfiguration(String gridName)
throws Exception {
+ return setEvictionMode(DataPageEvictionMode.DISABLED,
super.getConfiguration(gridName));
+ }
+
+ /** {@inheritDoc} */
+ @Override protected void afterTest() throws Exception {
+ super.afterTest();
+
+ stopAllGrids();
+ }
+
+ /** */
+ @Test
+ public void testRemoveAtOomThreshold() throws Exception {
+ IgniteEx ignite = startGrids(2);
+
+ IgniteCache<Integer, byte[]> cache = ignite.createCache(new
CacheConfiguration<Integer, byte[]>(DEFAULT_CACHE_NAME)
+ .setAffinity(new RendezvousAffinityFunction(false, 32))
+ .setAtomicityMode(CacheAtomicityMode.ATOMIC)
+ .setBackups(1)
+
.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC));
+
+ awaitPartitionMapExchange();
+
+ int filled = fillUntilOom(cache, pageSize(ignite));
+
+ OOM_FAILURES.set(0);
+
+ int half = filled / 2;
+
+ // Primary single-key removes (and backup deletes on the other node).
+ for (int i = 0; i < half; i++)
+ cache.remove(i);
+
+ // Primary batch removes. The key that failed with OOM is included: it
may be stored on one of the nodes.
+ Set<Integer> keys = new HashSet<>();
+
+ for (int i = half; i <= filled; i++)
+ keys.add(i);
+
+ cache.removeAll(keys);
+
+ assertEquals(0, cache.size());
+ assertEquals("Removals must not report out-of-memory to the failure
handler", 0, OOM_FAILURES.get());
+ }
+
+ /**
+ * The size-aware reservation is not engaged for a region with disabled
page eviction: it neither reserves nor
+ * performs an early out-of-memory check, even for a row larger than the
region. The out-of-memory check of an
+ * actual insert ({@code ensureFreeSpaceForInsert}) is kept as is.
+ */
+ @Test
+ public void testReservationNotEngaged() throws Exception {
+ IgniteEx ignite = startGrid(0);
+
+ createCache(ignite, DEFAULT_CACHE_NAME).put(0, 0);
+
+ DataRegion region = defaultRegion(ignite);
+
+ FreeList<?> freeList =
ignite.context().cache().context().database().freeList(region.config().getName());
+
+ int hugeRow = (int)Math.min(Integer.MAX_VALUE, regionMaxSize(ignite) *
2);
+
+ assertEquals(0,
ignite.context().cache().context().database().reserveInRegion(region, hugeRow,
false));
+ assertEquals(0, freeList.reserve(hugeRow, false));
+ assertEquals(0, OOM_FAILURES.get());
+
+ GridTestUtils.assertThrows(log, () -> {
+
ignite.context().cache().context().database().ensureFreeSpaceForInsert(region,
hugeRow);
+
+ return null;
+ }, IgniteOutOfMemoryException.class, null);
+ }
+
+ /** Read-through load into a region at the out-of-memory threshold is
rejected by the insert check. */
+ @Test
+ public void testReadThroughGetOomAtThreshold() throws Exception {
+ IgniteEx ignite = startGrid(0);
+
+ IgniteCache<Integer, byte[]> cache =
ignite.getOrCreateCache(readThroughCacheConfig());
+
+ int filled = fillUntilOom(cache, pageSize(ignite));
+
+ GridTestUtils.assertThrowsWithCause(() -> cache.get(filled + 1),
IgniteOutOfMemoryException.class);
+ }
+
+ /** Read-through load below the threshold succeeds. */
+ @Test
+ public void testReadThroughGetBelowThresholdSucceeds() throws Exception {
+ byte[] val =
startGrid(0).getOrCreateCache(readThroughCacheConfig()).get(1);
+
+ assertNotNull("Read-through load must succeed below threshold", val);
+ assertEquals(PAGE_SIZE - 200, val.length);
+ }
+
+ /** @return Atomic read-through cache configuration. */
+ private static CacheConfiguration<Integer, byte[]>
readThroughCacheConfig() {
+ return new CacheConfiguration<Integer, byte[]>(DEFAULT_CACHE_NAME)
+ .setAffinity(new RendezvousAffinityFunction(false, 32))
+ .setAtomicityMode(CacheAtomicityMode.ATOMIC)
+ .setReadThrough(true)
+ .setCacheStoreFactory(new TestStoreFactory());
+ }
+
+ /**
+ * Puts page-sized entries until the insert is rejected with an
out-of-memory.
+ *
+ * @param cache Cache.
+ * @param pageSize Page size.
+ * @return Number of entries written before the out-of-memory.
+ */
+ private static int fillUntilOom(IgniteCache<Integer, byte[]> cache, int
pageSize) {
+ byte[] val = new byte[pageSize - 200];
+
+ for (int i = 0; i < 100_000; i++) {
Review Comment:
Maybe not hardcoded 100_000 value, but some formula?
##########
modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/paged/PageEvictionReservedPagesSelfTest.java:
##########
@@ -0,0 +1,328 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.cache.eviction.paged;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicLong;
+import javax.cache.processor.EntryProcessor;
+import javax.cache.processor.MutableEntry;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.Ignition;
+import org.apache.ignite.cache.CacheWriteSynchronizationMode;
+import org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.DataPageEvictionMode;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.metric.IoStatisticsHolderNoOp;
+import org.apache.ignite.internal.processors.cache.CacheObject;
+import org.apache.ignite.internal.processors.cache.GridCacheContext;
+import
org.apache.ignite.internal.processors.cache.IgniteCacheOffheapManager.CacheDataStore;
+import org.apache.ignite.internal.processors.cache.KeyCacheObject;
+import org.apache.ignite.internal.processors.cache.persistence.CacheDataRow;
+import
org.apache.ignite.internal.processors.cache.persistence.freelist.FreeList;
+import org.apache.ignite.internal.processors.cache.tree.DataRow;
+import org.apache.ignite.internal.processors.cache.version.GridCacheVersion;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.junit.Test;
+
+import static org.apache.ignite.testframework.GridTestUtils.getFieldValue;
+
+/**
+ * Tests the page reservation accounting of an in-memory free list with
enabled page eviction: the reservation is fully
+ * released after batch inserts (whole-page fragments, fill-up-handler rows,
whole-page-aligned rows, concurrent batch
+ * inserts), concurrent puts and putAll on a backup node, and read-through
loads; the empty pages pool is shared by
+ * all concurrent reservations instead of being added to each of them.
+ */
+public class PageEvictionReservedPagesSelfTest extends
PageEvictionAbstractTest {
+ /** Empty pages pool size: close to the allowed maximum of 10% of the
region. */
+ private static final int LARGE_EMPTY_PAGES_POOL_SIZE = 900;
+
+ /** {@inheritDoc} */
+ @Override protected IgniteConfiguration getConfiguration(String gridName)
throws Exception {
+ IgniteConfiguration cfg =
setEvictionMode(DataPageEvictionMode.RANDOM_LRU,
super.getConfiguration(gridName));
+
+
cfg.getDataStorageConfiguration().getDefaultDataRegionConfiguration().setEmptyPagesPoolSize(LARGE_EMPTY_PAGES_POOL_SIZE);
+
+ return cfg;
+ }
+
+ /** {@inheritDoc} */
+ @Override protected void afterTest() throws Exception {
+ super.afterTest();
+
+ stopAllGrids();
+
+ assertNoOomFailures();
+ }
+
+ /** */
+ @Test
+ public void testReservedPagesReleasedAfterBatchInserts() throws Exception {
+ IgniteEx ignite = startGrid(0);
+
+ createCache(ignite, DEFAULT_CACHE_NAME).put(0, 0);
+
+ FreeList<CacheDataRow> freeList = freeList(ignite);
+
+ // Phase 1: small rows that fit into a single page (fill-up path, no
whole-page fragments).
+ insertBatchesAndCheckReservedPages(ignite, freeList, 10);
+
+ // Phase 2: large rows that span multiple pages (whole-page fragments
+ fill-up remainder).
+ insertBatchesAndCheckReservedPages(ignite, freeList, 10_000);
+
+ // Phase 3: rows sized to an exact multiple of the whole-page fragment
size (whole-page fragments only).
+ int minPageSize = getFieldValue(freeList, "MIN_SIZE_FOR_DATA_PAGE");
Review Comment:
Reflection is hard to maintain. Consider using another mechanism. For
example make field public or make `batchSize` method public and use it. It's
more correct to use package private access for such a fields/methods, but test
class and required fields/methods in different packages now. Maybe it's worth
to fill ticket to refactor and try to move all reservation logic to Eviction
tracker (reserved pages for eviction looks closer to eviction tracker than free
lists), move tests and evictions trackers to correct packages and make
fields/methods package private (for some reason eviction trackers are inside
`org.apache.ignite.internal.processors.cache.**persistence**.evict` package,
but not related to `persistence` somehow)
##########
modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/paged/PageEvictionReservedPagesSelfTest.java:
##########
@@ -0,0 +1,328 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.cache.eviction.paged;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicLong;
+import javax.cache.processor.EntryProcessor;
+import javax.cache.processor.MutableEntry;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.Ignition;
+import org.apache.ignite.cache.CacheWriteSynchronizationMode;
+import org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.DataPageEvictionMode;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.metric.IoStatisticsHolderNoOp;
+import org.apache.ignite.internal.processors.cache.CacheObject;
+import org.apache.ignite.internal.processors.cache.GridCacheContext;
+import
org.apache.ignite.internal.processors.cache.IgniteCacheOffheapManager.CacheDataStore;
+import org.apache.ignite.internal.processors.cache.KeyCacheObject;
+import org.apache.ignite.internal.processors.cache.persistence.CacheDataRow;
+import
org.apache.ignite.internal.processors.cache.persistence.freelist.FreeList;
+import org.apache.ignite.internal.processors.cache.tree.DataRow;
+import org.apache.ignite.internal.processors.cache.version.GridCacheVersion;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.junit.Test;
+
+import static org.apache.ignite.testframework.GridTestUtils.getFieldValue;
+
+/**
+ * Tests the page reservation accounting of an in-memory free list with
enabled page eviction: the reservation is fully
+ * released after batch inserts (whole-page fragments, fill-up-handler rows,
whole-page-aligned rows, concurrent batch
+ * inserts), concurrent puts and putAll on a backup node, and read-through
loads; the empty pages pool is shared by
+ * all concurrent reservations instead of being added to each of them.
+ */
+public class PageEvictionReservedPagesSelfTest extends
PageEvictionAbstractTest {
+ /** Empty pages pool size: close to the allowed maximum of 10% of the
region. */
+ private static final int LARGE_EMPTY_PAGES_POOL_SIZE = 900;
+
+ /** {@inheritDoc} */
+ @Override protected IgniteConfiguration getConfiguration(String gridName)
throws Exception {
+ IgniteConfiguration cfg =
setEvictionMode(DataPageEvictionMode.RANDOM_LRU,
super.getConfiguration(gridName));
+
+
cfg.getDataStorageConfiguration().getDefaultDataRegionConfiguration().setEmptyPagesPoolSize(LARGE_EMPTY_PAGES_POOL_SIZE);
+
+ return cfg;
+ }
+
+ /** {@inheritDoc} */
+ @Override protected void afterTest() throws Exception {
+ super.afterTest();
+
+ stopAllGrids();
+
+ assertNoOomFailures();
+ }
+
+ /** */
+ @Test
+ public void testReservedPagesReleasedAfterBatchInserts() throws Exception {
+ IgniteEx ignite = startGrid(0);
+
+ createCache(ignite, DEFAULT_CACHE_NAME).put(0, 0);
+
+ FreeList<CacheDataRow> freeList = freeList(ignite);
+
+ // Phase 1: small rows that fit into a single page (fill-up path, no
whole-page fragments).
+ insertBatchesAndCheckReservedPages(ignite, freeList, 10);
+
+ // Phase 2: large rows that span multiple pages (whole-page fragments
+ fill-up remainder).
+ insertBatchesAndCheckReservedPages(ignite, freeList, 10_000);
+
+ // Phase 3: rows sized to an exact multiple of the whole-page fragment
size (whole-page fragments only).
+ int minPageSize = getFieldValue(freeList, "MIN_SIZE_FOR_DATA_PAGE");
+
+ int fixedSize = row(cacheContext(ignite), 0, 0).size();
+
+ insertBatchesAndCheckReservedPages(ignite, freeList, 2 * minPageSize -
fixedSize);
+ }
+
+ /** */
+ @Test
+ public void testReservedPagesReleasedAfterConcurrentBatchInserts() throws
Exception {
+ IgniteEx ignite = startGrid(0);
+
+ createCache(ignite, DEFAULT_CACHE_NAME).put(0, 0);
+
+ FreeList<CacheDataRow> freeList = freeList(ignite);
+
+ GridCacheContext<?, ?> cctx = cacheContext(ignite);
+
+ int rowsPerThread = 200;
+
+ AtomicInteger keyGen = new AtomicInteger();
+
+ GridTestUtils.runMultiThreaded(() -> {
+ try {
+ Collection<CacheDataRow> rows = new ArrayList<>(rowsPerThread);
+
+ for (int i = 0; i < rowsPerThread; i++)
+ rows.add(row(cctx, keyGen.incrementAndGet(), 100));
+
+ freeList.insertDataRows(rows, IoStatisticsHolderNoOp.INSTANCE);
+ }
+ catch (IgniteCheckedException e) {
+ throw new RuntimeException(e);
+ }
+ }, 8, "concurrent-batch-insert");
+
+ assertAllReleased(10_000);
+ }
+
+ /** */
+ @Test
+ public void testReservedPagesReleasedAfterConcurrentPuts() throws
Exception {
+ IgniteCache<Integer, byte[]> cache = startGridsWithBackupCache();
+
+ AtomicInteger keyGen = new AtomicInteger();
+
+ GridTestUtils.runMultiThreaded(() -> {
+ for (int i = 0; i < 300; i++)
+ cache.put(keyGen.incrementAndGet(), new byte[500]);
+ }, 8, "concurrent-put");
+
+ assertAllReleased(10_000);
+ }
+
+ /** */
+ @Test
+ public void testReservedPagesReleasedAfterConcurrentPutAll() throws
Exception {
+ IgniteCache<Integer, byte[]> cache = startGridsWithBackupCache();
+
+ AtomicInteger keyGen = new AtomicInteger();
+
+ GridTestUtils.runMultiThreaded(() -> {
+ for (int b = 0; b < 10; b++) {
+ Map<Integer, byte[]> batch = new HashMap<>();
+
+ for (int i = 0; i < 100; i++)
+ batch.put(keyGen.incrementAndGet(), new byte[300]);
+
+ cache.putAll(batch);
+ }
+ }, 4, "concurrent-putAll");
+
+ assertAllReleased(10_000);
+ }
+
+ /** Read-through loads reserve pages before the entry lock and must
release them, also on the backup. */
+ @Test
+ public void testReservedPagesReleasedAfterReadThrough() throws Exception {
+ startGrids(2);
+
+ IgniteCache<Integer, byte[]> cache = grid(0).getOrCreateCache(new
CacheConfiguration<Integer, byte[]>(DEFAULT_CACHE_NAME)
+ .setAffinity(new RendezvousAffinityFunction(false, 32))
+
.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC)
+ .setBackups(1)
+ .setReadThrough(true)
+ .setCacheStoreFactory(new TestStoreFactory()));
+
+ awaitPartitionMapExchange();
+
+ for (int i = 1; i <= 15_000; i++)
+ cache.get(i);
+
+ assertTrue("Eviction must have started during read-through loads",
isEvictionsStarted(grid(0)));
+
+ assertAllReleased(10_000);
+ }
+
+ /** The reservation is held while the row is written: the entry processor
runs inside this window. */
+ @Test
+ public void testReservationHeldWhileRowIsWritten() throws Exception {
+ IgniteCache<Integer, Object> cache = createCache(startGrid(0),
DEFAULT_CACHE_NAME);
+
+ cache.put(0, 0);
+
+ long inFlight = cache.invoke(1, new ReservedPagesProbe());
+
+ assertTrue("Reservation must be held while the row is written: " +
inFlight, inFlight > 0);
+
+ assertAllReleased(10_000);
+ }
+
+ /**
+ * Holds reservations of single-page rows of many writers at once in an
empty region: the sum of per-writer pools
+ * would exceed the region (and there is nothing to evict), but a single
shared pool leaves room for all of them.
+ */
+ @Test
+ public void testSimultaneousReservationsShareThePool() throws Exception {
+ IgniteEx ignite = startGrid(0);
+
+ createCache(ignite, DEFAULT_CACHE_NAME).put(0, 0);
+
+ int writers = 16;
+
+ assertTrue(writers * LARGE_EMPTY_PAGES_POOL_SIZE > totalPages(ignite));
+
+ FreeList<CacheDataRow> freeList = freeList(ignite);
+
+ long[] reserved = new long[writers];
+
+ try {
+ for (int i = 0; i < writers; i++) {
+ reserved[i] = freeList.reserve(pageSize(ignite) / 2, false);
+
+ assertEquals(1, reserved[i]);
+ }
+ }
+ finally {
+ for (long pages : reserved)
+ freeList.releaseReserved(pages);
+ }
+
+ assertEquals(0, reservedPages(freeList));
+ }
+
+ /** @return Cache with a backup on two started nodes. */
+ private IgniteCache<Integer, byte[]> startGridsWithBackupCache() throws
Exception {
+ startGrids(2);
+
+ IgniteCache<Integer, byte[]> cache = grid(0).createCache(new
CacheConfiguration<Integer, byte[]>(DEFAULT_CACHE_NAME)
+ .setAffinity(new RendezvousAffinityFunction(false, 32))
+ .setBackups(1)
+
.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC));
+
+ awaitPartitionMapExchange();
+
+ return cache;
+ }
+
+ /**
+ * Asserts that {@code reservedPages} is {@code 0} on every data store of
every started node, waiting up to
+ * {@code timeoutMs} for the reservation to drain.
+ *
+ * @param timeoutMs Wait timeout in milliseconds.
+ * @throws Exception If the reservation does not drain in time.
+ */
+ private static void assertAllReleased(long timeoutMs) throws Exception {
Review Comment:
Always called with 10_000 timeout, let's move this value inside method.
##########
modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/paged/PageEvictionSizeAwareAbstractTest.java:
##########
@@ -0,0 +1,630 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.cache.eviction.paged;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.Lock;
+import javax.cache.CacheException;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.IgniteDataStreamer;
+import org.apache.ignite.cache.CacheWriteSynchronizationMode;
+import org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.DataRegionConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.IgniteInternalFuture;
+import org.apache.ignite.internal.mem.IgniteOutOfMemoryException;
+import org.apache.ignite.internal.processors.cache.persistence.DataRegion;
+import
org.apache.ignite.internal.processors.cache.persistence.IgniteCacheDatabaseSharedManager;
+import
org.apache.ignite.internal.processors.cache.persistence.freelist.AbstractFreeList;
+import
org.apache.ignite.internal.processors.cache.persistence.freelist.FreeList;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.lang.IgniteFuture;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.apache.ignite.testframework.junits.WithSystemProperty;
+import org.junit.Test;
+
+import static
org.apache.ignite.IgniteSystemProperties.IGNITE_PAGE_EVICTION_NO_PROGRESS_TIMEOUT_MILLIS;
+
+/** Tests size-aware page eviction on in-memory (non-persistent) data regions.
*/
+public abstract class PageEvictionSizeAwareAbstractTest extends
PageEvictionAbstractTest {
+ /** Name of the rebalance supplier node, which has a twice larger data
region. */
+ private static final String SUPPLIER = "supplier";
+
+ /** Eviction no-progress timeout (milliseconds) of the guard test. */
+ private static final long GUARD_TIMEOUT = 3_000;
+
+ /** {@inheritDoc} */
+ @Override protected IgniteConfiguration getConfiguration(String gridName)
throws Exception {
+ IgniteConfiguration cfg = super.getConfiguration(gridName);
+
+ if (SUPPLIER.equals(gridName)) {
+ DataRegionConfiguration regCfg =
cfg.getDataStorageConfiguration().getDefaultDataRegionConfiguration();
+
+ regCfg.setMaxSize(regCfg.getMaxSize() * 2);
+ }
+
+ return cfg;
+ }
+
+ /** {@inheritDoc} */
+ @Override protected void afterTest() throws Exception {
+ super.afterTest();
+
+ stopAllGrids();
+
+ assertNoOomFailures();
+ }
+
+ /**
+ * Rebalancing inserts the received rows in batches reserved as a whole:
the demander, which receives more data than
+ * fits below its threshold, evicts at the threshold and releases the
reservations.
+ */
+ @Test
+ public void testRebalanceHonorsThreshold() throws Exception {
+ IgniteEx supplier = startGrid(SUPPLIER);
+
+ IgniteCache<Integer, Object> cache = supplier.createCache(new
CacheConfiguration<Integer, Object>(DEFAULT_CACHE_NAME)
+ .setAffinity(new RendezvousAffinityFunction(false, 32))
+ .setBackups(1));
+
+ byte[] val = new byte[pageSize(supplier) - 200];
+
+ // One page per entry: 1.5 times the demander region.
+ int keys = (int)(totalPages(supplier) * 0.75);
+
+ for (int base = 0; base < keys; base += 100) {
+ Map<Integer, Object> batch = new HashMap<>();
+
+ for (int i = base; i < Math.min(base + 100, keys); i++)
+ batch.put(i, val);
+
+ cache.putAll(batch);
+ }
+
+ IgniteEx demander = startGrid(1);
+
+ awaitPartitionMapExchange();
+
+ assertHonorsThreshold(demander);
+
+ assertEquals(0,
((AtomicLong)GridTestUtils.getFieldValue(defaultFreeList(demander),
"reservedPages")).get());
+ }
+
+ /** */
+ @Test
+ public void testRecordLargerThanRegionOom() throws Exception {
+ IgniteEx ignite = startGrid(1);
+
+ IgniteCache<Integer, Object> cache = createCache(ignite,
DEFAULT_CACHE_NAME);
+
+ long regionSize = regionMaxSize(ignite);
+
+ GridTestUtils.assertThrowsWithCause(
+ () -> cache.put(1, new byte[(int)(regionSize * 2)]),
+ IgniteOutOfMemoryException.class
+ );
+
+ GridTestUtils.assertThrowsWithCause(
+ () -> {
+ Map<Integer, Object> batch = new HashMap<>();
+
+ Object val = new byte[(int)(regionSize / 4)];
+
+ for (int i = 0; i < 4; i++)
+ batch.put(i, val);
+
+ cache.putAll(batch);
+ },
+ IgniteOutOfMemoryException.class
+ );
+ }
+
+ /** */
+ @Test
+ public void testPutAllLargeRows() throws Exception {
+ IgniteEx ignite = startGrid(1);
+
+ IgniteCache<Integer, Object> cache = createCache(ignite,
DEFAULT_CACHE_NAME);
+
+ int smallEntries = (int)(totalPages(ignite) * 0.85);
+
+ byte[] small = new byte[pageSize(ignite) - 200];
+
+ for (int i = 0; i < smallEntries; i++)
+ cache.put(smallEntries + i, small);
+
+ assertTrue("Pre-fill must load >80% of pages", loadedPages(ignite) >
totalPages(ignite) * 0.80);
+ assertFalse("Eviction must not start during pre-fill",
isEvictionsStarted(ignite));
+
+ int putAllLargeRows = 3;
+ Map<Integer, Object> large = new HashMap<>();
+
+ Object val = new byte[(int)(regionMaxSize(ignite) / 4)];
+
+ for (int i = 0; i < putAllLargeRows; i++)
+ large.put(i, val);
+
+ cache.putAll(large);
+
+ assertTrue("Eviction must start after putAll of large rows near
capacity", isEvictionsStarted(ignite));
+
+ for (int i = 0; i < putAllLargeRows; i++)
+ assertNotNull("Large row " + i + " must be readable after putAll",
cache.get(i));
+ }
+
+ /** */
+ @Test
+ public void testUpdateRowGrows() throws Exception {
+ IgniteEx ignite = startGrid(1);
+
+ IgniteCache<Integer, Object> cache = createCache(ignite,
DEFAULT_CACHE_NAME);
+
+ int smallEntries = (int)(totalPages(ignite) * 0.85);
+ byte[] small = new byte[pageSize(ignite) - 200];
+
+ for (int i = 0; i < smallEntries; i++)
+ cache.put(smallEntries + i, small);
+
+ cache.put(1, new byte[1024]);
+
+ byte[] big = new byte[(int)(regionMaxSize(ignite) / 4)];
+
+ Arrays.fill(big, (byte)7);
+
+ cache.put(1, big);
+
+ assertTrue("Eviction must have started after growing a row near
capacity", isEvictionsStarted(ignite));
+
+ byte[] read = (byte[])cache.get(1);
+
+ assertNotNull("Updated large value must be readable", read);
+ assertTrue("Updated value must equal the stored value",
Arrays.equals(big, read));
+ }
+
+ /** */
+ @Test
+ public void testLargeRowBelowThresholdUsesHeadroomNoEviction() throws
Exception {
+ IgniteEx ignite = startGrid(1);
+
+ IgniteCache<Integer, Object> cache = createCache(ignite,
DEFAULT_CACHE_NAME);
+
+ int preFill = fillRegion(ignite, cache, 0.4);
+
+ long loadedPages = loadedPages(ignite);
+ long totalPages = totalPages(ignite);
+
+ assertTrue("Pre-fill must load ~40% of pages", loadedPages >
totalPages * 0.30 && loadedPages < totalPages * 0.50);
+ assertFalse("Eviction must not have started during pre-fill below
threshold", isEvictionsStarted(ignite));
+
+ byte[] val = new byte[(int)(regionMaxSize(ignite) * 0.3)];
+
+ Arrays.fill(val, (byte)1);
+
+ cache.put(preFill, val);
+
+ assertFalse("Eviction must not start when the region stays below the
threshold", isEvictionsStarted(ignite));
+
+ for (int i = 0; i < preFill; i++)
+ assertNotNull("Pre-filled entry " + i + " must not be evicted
below threshold", cache.get(i));
+
+ byte[] read = (byte[])cache.get(preFill);
+
+ assertNotNull("Large row must be readable", read);
+ assertTrue("Value read back must equal the stored value",
Arrays.equals(val, read));
+ }
+
+ /** */
+ @Test
+ public void testLargeRowAboveThresholdEvictsAndSucceeds() throws Exception
{
+ IgniteEx ignite = startGrid(1);
+
+ IgniteCache<Integer, Object> cache = createCache(ignite,
DEFAULT_CACHE_NAME);
+
+ int smallEntries = fillRegion(ignite, cache, 0.85);
+
+ assertTrue("Pre-fill must load >80% of pages", loadedPages(ignite) >
totalPages(ignite) * 0.80);
+
+ byte[] val = new byte[(int)(regionMaxSize(ignite) / 4)];
+
+ Arrays.fill(val, (byte)1);
+
+ int largeKey = smallEntries + 1;
+
+ cache.put(largeKey, val);
+
+ assertTrue("Eviction must have started after a large put near
capacity", isEvictionsStarted(ignite));
+
+ byte[] read = (byte[])cache.get(largeKey);
+
+ assertNotNull("Large row must be readable after eviction", read);
+ assertTrue("Value read back must equal the stored value",
Arrays.equals(val, read));
+ }
+
+ /** Atomic cache with a backup: primary batch and backup updates must
evict at the same threshold. */
+ @Test
+ public void testAtomicBackupHonorsThreshold() throws Exception {
+ IgniteEx ignite = startGrids(2);
+
+ IgniteCache<Integer, Object> cache = ignite.createCache(new
CacheConfiguration<Integer, Object>(DEFAULT_CACHE_NAME)
+ .setAffinity(new RendezvousAffinityFunction(false, 32))
+ .setBackups(1)
+
.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC));
+
+ awaitPartitionMapExchange();
+
+ byte[] val = new byte[pageSize(ignite) - 200];
+
+ int keys = (int)(totalPages(ignite) * 3);
+
+ for (int base = 0; base < keys; base += 100) {
+ Map<Integer, Object> batch = new HashMap<>();
+
+ for (int i = base; i < base + 100; i++)
+ batch.put(i, val);
+
+ cache.putAll(batch);
+ }
+
+ assertHonorsThreshold(grid(0));
+ assertHonorsThreshold(grid(1));
+ }
+
+ /** Transactional cache: commits must evict at the threshold. */
+ @Test
+ public void testTransactionalHonorsThreshold() throws Exception {
+ IgniteEx ignite = startGrid(0);
+
+ IgniteCache<Integer, Object> cache = createCache(ignite,
DEFAULT_CACHE_NAME, 0, true);
+
+ byte[] val = new byte[pageSize(ignite) - 200];
+
+ int keys = (int)(totalPages(ignite) * 3);
+
+ for (int i = 0; i < keys; i++)
+ cache.put(i, val);
+
+ assertHonorsThreshold(ignite);
+ }
+
+ /** A row that does not fit below the threshold together with the pool may
use the space above it. */
+ @Test
+ public void testRowLargerThanThresholdFits() throws Exception {
+ IgniteEx ignite = startGrid(0);
+
+ IgniteCache<Integer, Object> cache = createCache(ignite,
DEFAULT_CACHE_NAME);
+
+ fillRegion(ignite, cache, 0.5);
+
+ int largeValSize = (int)(regionMaxSize(ignite) * EVICTION_THRESHOLD);
+
+ cache.put(-1, new byte[largeValSize]);
+
+ assertEquals(largeValSize, ((byte[])cache.get(-1)).length);
+ }
+
+ /**
+ * A commit can not be refused, so the reservation does not stop a row
larger than the region: it fails in the page
+ * memory, which reports a critical error to the failure handler (the
default handler stops the node).
+ */
+ @Test
+ public void testTransactionalRecordLargerThanRegionFailsInPageMemory()
throws Exception {
+ IgniteEx ignite = startGrid(0);
+
+ IgniteCache<Integer, Object> cache = createCache(ignite,
DEFAULT_CACHE_NAME, 0, true);
+
+ long regionSize = regionMaxSize(ignite);
+
+ GridTestUtils.assertThrowsWithCause(
+ () -> cache.put(1, new byte[(int)(regionSize * 2)]),
+ IgniteOutOfMemoryException.class
+ );
+
+ assertTrue("A page memory out-of-memory must reach the failure
handler", OOM_FAILURES.get() > 0);
+
+ // Expected in this test only.
+ OOM_FAILURES.set(0);
+ }
+
+ /** The default streamer mode inserts through {@code initialValue}, which
reserves pages for every row. */
+ @Test
+ public void testDefaultStreamerHonorsThreshold() throws Exception {
+ IgniteEx ignite = startGrid(0);
+
+ createCache(ignite, DEFAULT_CACHE_NAME);
+
+ byte[] val = new byte[pageSize(ignite) - 200];
+
+ int keys = (int)(totalPages(ignite) * 3);
+
+ try (IgniteDataStreamer<Integer, Object> streamer =
ignite.dataStreamer(DEFAULT_CACHE_NAME)) {
+ for (int i = 0; i < keys; i++)
+ streamer.addData(i, val);
+ }
+
+ assertHonorsThreshold(ignite);
+ }
+
+ /**
+ * The default streamer mode can refuse a row: the refusal reaches the
caller through the future of the operation and
+ * does not reach the failure handler (see {@link #afterTest()}). Closing
the streamer reports the failed operation
+ * too, but not necessarily with its cause.
+ */
+ @Test
+ public void testDefaultStreamerRecordLargerThanRegionRefused() throws
Exception {
+ IgniteEx ignite = startGrid(0);
+
+ createCache(ignite, DEFAULT_CACHE_NAME);
+
+ IgniteDataStreamer<Integer, Object> streamer =
ignite.dataStreamer(DEFAULT_CACHE_NAME);
+
+ IgniteFuture<?> fut = streamer.addData(1, new
byte[(int)(regionMaxSize(ignite) * 2)]);
+
+ GridTestUtils.assertThrows(log, () -> {
+ streamer.close();
+
+ return null;
+ }, CacheException.class, null);
+
+ GridTestUtils.assertThrowsWithCause(() -> fut.get(),
IgniteOutOfMemoryException.class);
+
+ assertFalse("A refused row must not start the eviction",
isEvictionsStarted(ignite));
+ }
+
+ /**
+ * A claim that fits the raw region size, but not the pages the region can
actually load, is refused at once: it must
+ * not evict the data before the refusal.
+ */
+ @Test
+ public void testClaimAboveLoadablePagesRefusedWithoutEviction() throws
Exception {
+ IgniteEx ignite = startGrid(0);
+
+ IgniteCache<Integer, Object> cache = createCache(ignite,
DEFAULT_CACHE_NAME);
+
+ int entries = fillRegion(ignite, cache, 0.5);
+
+ FreeList<?> freeList = defaultFreeList(ignite);
+
+ long claimPages = totalPages(ignite) -
defaultRegion(ignite).config().getEmptyPagesPoolSize();
+
+ int pagePayload = GridTestUtils.getFieldValue(freeList,
"MIN_SIZE_FOR_DATA_PAGE");
+
+ GridTestUtils.assertThrows(log, () -> freeList.reserve(claimPages *
pagePayload, false),
+ IgniteOutOfMemoryException.class, null);
+
+ assertFalse("A refused claim must not start the eviction",
isEvictionsStarted(ignite));
+ assertEquals(entries, cache.size());
+ }
+
+ /**
+ * When no page can be evicted (all resident entries are locked), the
eviction progress guard refuses an insert that
+ * can be refused (a primary atomic update) with an out-of-memory, while a
best-effort reservation (backup update,
+ * transaction commit, rebalancing) gives up without an error. Once the
stall is detected, the already waiting and
+ * the following best-effort reservations give up without waiting for the
timeout, until the eviction makes progress
+ * again.
+ */
+ @Test
+ @WithSystemProperty(key = IGNITE_PAGE_EVICTION_NO_PROGRESS_TIMEOUT_MILLIS,
value = "" + GUARD_TIMEOUT)
+ public void testGuardWhenAllEntriesLocked() throws Exception {
+ IgniteEx ignite = startGrid(1);
+
+ IgniteCache<Integer, Object> atomicCache = createCache(ignite,
DEFAULT_CACHE_NAME);
+
+ CountDownLatch release = new CountDownLatch(1);
+
+ IgniteInternalFuture<?> lockerFut = fillRegionAndLockEntries(ignite,
release);
+
+ try {
+ int largeValSize = 4 * 1024 * 1024;
+
+ GridTestUtils.assertThrowsWithCause(
+ () -> atomicCache.put(0, new byte[largeValSize]),
+ IgniteOutOfMemoryException.class
+ );
+
+ IgniteCacheDatabaseSharedManager db =
ignite.context().cache().context().database();
+
+ DataRegion region = defaultRegion(ignite);
+
+ FreeList<?> freeList = db.freeList(region.config().getName());
+
+ // A reservation that waits for the timeout takes at least the
timeout (with the 10 ms clock error), a
+ // reservation that gives up does not: the bound separates them
far from the expected time of both.
+ long bound = GUARD_TIMEOUT * 9 / 10;
+
+ assertFalse(GridTestUtils.<Boolean>getFieldValue(freeList,
"stalled"));
+
+ IgniteInternalFuture<Long> firstFut = GridTestUtils.runAsync(() ->
db.reserveInRegion(region, largeValSize, true),
+ "size-aware-guard-first");
+
+ U.sleep(GUARD_TIMEOUT * 9 / 10);
+
+ // Starts waiting before the stall is detected and must be
released once the first reservation detects it.
+ long start = System.currentTimeMillis();
+
+ assertEquals(0, db.reserveInRegion(region, largeValSize, true));
+
+ assertTrue("Waiting best-effort reservation must give up once the
stall is detected",
+ System.currentTimeMillis() - start < bound);
+
+ assertEquals(0, (long)firstFut.get(60, TimeUnit.SECONDS));
+
+ assertTrue(GridTestUtils.<Boolean>getFieldValue(freeList,
"stalled"));
+
+ // A reservation that needs no eviction says nothing about it, so
the stall stays.
+ long smallReserved = db.reserveInRegion(region, 1, false);
+
+ try {
+ assertEquals(1, smallReserved);
+
+ assertTrue(GridTestUtils.<Boolean>getFieldValue(freeList,
"stalled"));
+ }
+ finally {
+ db.releaseInRegion(region, smallReserved);
+ }
+
+ // Reservations that gave up return the pages they accumulated.
+ assertEquals(0, GridTestUtils.<AtomicLong>getFieldValue(freeList,
"reservedPages").get());
+
+ // The stall is not a time window: once the timeout passes,
best-effort reservations still do not wait.
+ U.sleep(GUARD_TIMEOUT + 500);
+
+ start = System.currentTimeMillis();
+
+ assertEquals(0, db.reserveInRegion(region, largeValSize, true));
+
+ assertTrue("Best-effort reservation must not wait for the timeout
while the eviction is stalled",
+ System.currentTimeMillis() - start < bound);
+
+ release.countDown();
+
+ lockerFut.get(10, TimeUnit.SECONDS);
+
+ // The unlocked entries can be evicted again: the reservation
succeeds and clears the stall. While stalled, a
+ // best-effort reservation gives up after a single eviction
attempt, which may free no page, so it is retried.
+ long reserved = 0;
+
+ long deadline = System.currentTimeMillis() +
TimeUnit.SECONDS.toMillis(60);
+
+ while (reserved == 0 && System.currentTimeMillis() < deadline)
+ reserved = db.reserveInRegion(region, largeValSize, true);
+
+ try {
+ assertTrue("Reservation must succeed once the entries are
unlocked", reserved > 0);
+
+ assertFalse(GridTestUtils.<Boolean>getFieldValue(freeList,
"stalled"));
+ }
+ finally {
+ db.releaseInRegion(region, reserved);
+ }
+ }
+ finally {
+ release.countDown();
+
+ lockerFut.get(10, TimeUnit.SECONDS);
+ }
+ }
+
+ /** With a zero timeout the guard gives up after the first eviction
attempt without progress. */
+ @Test
+ @WithSystemProperty(key = IGNITE_PAGE_EVICTION_NO_PROGRESS_TIMEOUT_MILLIS,
value = "0")
+ public void testZeroNoProgressTimeoutGivesUpWithoutWaiting() throws
Exception {
+ IgniteEx ignite = startGrid(1);
+
+ IgniteCache<Integer, Object> atomicCache = createCache(ignite,
DEFAULT_CACHE_NAME);
+
+ CountDownLatch release = new CountDownLatch(1);
+
+ IgniteInternalFuture<?> lockerFut = fillRegionAndLockEntries(ignite,
release);
+
+ try {
+ long start = System.currentTimeMillis();
+
+ GridTestUtils.assertThrowsWithCause(
+ () -> atomicCache.put(0, new byte[4 * 1024 * 1024]),
+ IgniteOutOfMemoryException.class
+ );
+
+ assertTrue("The default timeout must not be waited for",
+ System.currentTimeMillis() - start <
AbstractFreeList.DFLT_EVICTION_NO_PROGRESS_TIMEOUT_MILLIS);
+
+ assertEquals(0,
GridTestUtils.<AtomicLong>getFieldValue(defaultFreeList(ignite),
"reservedPages").get());
Review Comment:
Consider to use getter (see previous comments)
##########
modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/paged/PageEvictionDisabledEvictionSelfTest.java:
##########
@@ -0,0 +1,174 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.cache.eviction.paged;
+
+import java.util.HashSet;
+import java.util.Set;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.cache.CacheAtomicityMode;
+import org.apache.ignite.cache.CacheWriteSynchronizationMode;
+import org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.DataPageEvictionMode;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.mem.IgniteOutOfMemoryException;
+import org.apache.ignite.internal.processors.cache.persistence.DataRegion;
+import
org.apache.ignite.internal.processors.cache.persistence.freelist.FreeList;
+import org.apache.ignite.internal.util.typedef.X;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.junit.Test;
+
+/**
+ * Checks that the size-aware page reservation does not change the behavior of
an in-memory data region with disabled
+ * page eviction: inserts (including read-through loads) are still rejected by
the out-of-memory check at the threshold,
+ * while removals still succeed and do not trigger the failure handler.
+ */
+public class PageEvictionDisabledEvictionSelfTest extends
PageEvictionAbstractTest {
+ /** {@inheritDoc} */
+ @Override protected IgniteConfiguration getConfiguration(String gridName)
throws Exception {
+ return setEvictionMode(DataPageEvictionMode.DISABLED,
super.getConfiguration(gridName));
+ }
+
+ /** {@inheritDoc} */
+ @Override protected void afterTest() throws Exception {
+ super.afterTest();
+
+ stopAllGrids();
+ }
+
+ /** */
+ @Test
+ public void testRemoveAtOomThreshold() throws Exception {
+ IgniteEx ignite = startGrids(2);
+
+ IgniteCache<Integer, byte[]> cache = ignite.createCache(new
CacheConfiguration<Integer, byte[]>(DEFAULT_CACHE_NAME)
+ .setAffinity(new RendezvousAffinityFunction(false, 32))
+ .setAtomicityMode(CacheAtomicityMode.ATOMIC)
+ .setBackups(1)
+
.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC));
+
+ awaitPartitionMapExchange();
+
+ int filled = fillUntilOom(cache, pageSize(ignite));
+
+ OOM_FAILURES.set(0);
+
+ int half = filled / 2;
+
+ // Primary single-key removes (and backup deletes on the other node).
+ for (int i = 0; i < half; i++)
+ cache.remove(i);
+
+ // Primary batch removes. The key that failed with OOM is included: it
may be stored on one of the nodes.
+ Set<Integer> keys = new HashSet<>();
+
+ for (int i = half; i <= filled; i++)
+ keys.add(i);
+
+ cache.removeAll(keys);
+
+ assertEquals(0, cache.size());
+ assertEquals("Removals must not report out-of-memory to the failure
handler", 0, OOM_FAILURES.get());
+ }
+
+ /**
+ * The size-aware reservation is not engaged for a region with disabled
page eviction: it neither reserves nor
+ * performs an early out-of-memory check, even for a row larger than the
region. The out-of-memory check of an
+ * actual insert ({@code ensureFreeSpaceForInsert}) is kept as is.
+ */
+ @Test
+ public void testReservationNotEngaged() throws Exception {
+ IgniteEx ignite = startGrid(0);
+
+ createCache(ignite, DEFAULT_CACHE_NAME).put(0, 0);
+
+ DataRegion region = defaultRegion(ignite);
+
+ FreeList<?> freeList =
ignite.context().cache().context().database().freeList(region.config().getName());
+
+ int hugeRow = (int)Math.min(Integer.MAX_VALUE, regionMaxSize(ignite) *
2);
+
+ assertEquals(0,
ignite.context().cache().context().database().reserveInRegion(region, hugeRow,
false));
+ assertEquals(0, freeList.reserve(hugeRow, false));
+ assertEquals(0, OOM_FAILURES.get());
+
+ GridTestUtils.assertThrows(log, () -> {
+
ignite.context().cache().context().database().ensureFreeSpaceForInsert(region,
hugeRow);
+
+ return null;
+ }, IgniteOutOfMemoryException.class, null);
+ }
+
+ /** Read-through load into a region at the out-of-memory threshold is
rejected by the insert check. */
+ @Test
+ public void testReadThroughGetOomAtThreshold() throws Exception {
+ IgniteEx ignite = startGrid(0);
+
+ IgniteCache<Integer, byte[]> cache =
ignite.getOrCreateCache(readThroughCacheConfig());
+
+ int filled = fillUntilOom(cache, pageSize(ignite));
+
+ GridTestUtils.assertThrowsWithCause(() -> cache.get(filled + 1),
IgniteOutOfMemoryException.class);
+ }
+
+ /** Read-through load below the threshold succeeds. */
+ @Test
+ public void testReadThroughGetBelowThresholdSucceeds() throws Exception {
+ byte[] val =
startGrid(0).getOrCreateCache(readThroughCacheConfig()).get(1);
+
+ assertNotNull("Read-through load must succeed below threshold", val);
+ assertEquals(PAGE_SIZE - 200, val.length);
Review Comment:
Filled with `pageSize(ignite) - 200`, but calculated with `PAGE_SIZE - 200`,
let's use the same constant/calculated value and maybe introduce new field for
entry size
##########
modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/paged/PageEvictionReservedPagesSelfTest.java:
##########
@@ -0,0 +1,328 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.cache.eviction.paged;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicLong;
+import javax.cache.processor.EntryProcessor;
+import javax.cache.processor.MutableEntry;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.Ignition;
+import org.apache.ignite.cache.CacheWriteSynchronizationMode;
+import org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.DataPageEvictionMode;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.metric.IoStatisticsHolderNoOp;
+import org.apache.ignite.internal.processors.cache.CacheObject;
+import org.apache.ignite.internal.processors.cache.GridCacheContext;
+import
org.apache.ignite.internal.processors.cache.IgniteCacheOffheapManager.CacheDataStore;
+import org.apache.ignite.internal.processors.cache.KeyCacheObject;
+import org.apache.ignite.internal.processors.cache.persistence.CacheDataRow;
+import
org.apache.ignite.internal.processors.cache.persistence.freelist.FreeList;
+import org.apache.ignite.internal.processors.cache.tree.DataRow;
+import org.apache.ignite.internal.processors.cache.version.GridCacheVersion;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.junit.Test;
+
+import static org.apache.ignite.testframework.GridTestUtils.getFieldValue;
+
+/**
+ * Tests the page reservation accounting of an in-memory free list with
enabled page eviction: the reservation is fully
+ * released after batch inserts (whole-page fragments, fill-up-handler rows,
whole-page-aligned rows, concurrent batch
+ * inserts), concurrent puts and putAll on a backup node, and read-through
loads; the empty pages pool is shared by
+ * all concurrent reservations instead of being added to each of them.
+ */
+public class PageEvictionReservedPagesSelfTest extends
PageEvictionAbstractTest {
+ /** Empty pages pool size: close to the allowed maximum of 10% of the
region. */
+ private static final int LARGE_EMPTY_PAGES_POOL_SIZE = 900;
+
+ /** {@inheritDoc} */
+ @Override protected IgniteConfiguration getConfiguration(String gridName)
throws Exception {
+ IgniteConfiguration cfg =
setEvictionMode(DataPageEvictionMode.RANDOM_LRU,
super.getConfiguration(gridName));
+
+
cfg.getDataStorageConfiguration().getDefaultDataRegionConfiguration().setEmptyPagesPoolSize(LARGE_EMPTY_PAGES_POOL_SIZE);
+
+ return cfg;
+ }
+
+ /** {@inheritDoc} */
+ @Override protected void afterTest() throws Exception {
+ super.afterTest();
+
+ stopAllGrids();
+
+ assertNoOomFailures();
+ }
+
+ /** */
+ @Test
+ public void testReservedPagesReleasedAfterBatchInserts() throws Exception {
+ IgniteEx ignite = startGrid(0);
+
+ createCache(ignite, DEFAULT_CACHE_NAME).put(0, 0);
+
+ FreeList<CacheDataRow> freeList = freeList(ignite);
+
+ // Phase 1: small rows that fit into a single page (fill-up path, no
whole-page fragments).
+ insertBatchesAndCheckReservedPages(ignite, freeList, 10);
+
+ // Phase 2: large rows that span multiple pages (whole-page fragments
+ fill-up remainder).
+ insertBatchesAndCheckReservedPages(ignite, freeList, 10_000);
+
+ // Phase 3: rows sized to an exact multiple of the whole-page fragment
size (whole-page fragments only).
+ int minPageSize = getFieldValue(freeList, "MIN_SIZE_FOR_DATA_PAGE");
+
+ int fixedSize = row(cacheContext(ignite), 0, 0).size();
+
+ insertBatchesAndCheckReservedPages(ignite, freeList, 2 * minPageSize -
fixedSize);
+ }
+
+ /** */
+ @Test
+ public void testReservedPagesReleasedAfterConcurrentBatchInserts() throws
Exception {
+ IgniteEx ignite = startGrid(0);
+
+ createCache(ignite, DEFAULT_CACHE_NAME).put(0, 0);
+
+ FreeList<CacheDataRow> freeList = freeList(ignite);
+
+ GridCacheContext<?, ?> cctx = cacheContext(ignite);
+
+ int rowsPerThread = 200;
Review Comment:
200, 300, 10 put/putAll per thread looks not so concurrent. One thread can
complete execution until another is started. Let's increase this values at
least x10.
##########
modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/paged/PageEvictionConcurrentWritesAbstractTest.java:
##########
@@ -0,0 +1,328 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.cache.eviction.paged;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.cache.CacheWriteSynchronizationMode;
+import org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.IgniteInternalFuture;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.junit.Test;
+
+/** Concurrent deadlock and data-integrity tests for size-aware page eviction.
*/
+public abstract class PageEvictionConcurrentWritesAbstractTest extends
PageEvictionAbstractTest {
+ /** {@inheritDoc} */
+ @Override protected void afterTest() throws Exception {
+ super.afterTest();
+
+ stopAllGrids();
+
+ assertNoOomFailures();
+ }
+
+ /** */
+ @Test
+ public void testConcurrentLargeWritesNoDeadlock() throws Exception {
+ int smallEntries = 1_000;
+ int largeRowsPerThread = 20;
+ int threads = 10;
+
+ IgniteEx ignite = startGrid(1);
+
+ IgniteCache<Integer, Object> cache = createCache(ignite,
DEFAULT_CACHE_NAME);
+
+ long regionMax = regionMaxSize(ignite);
+
+ int smallValSize = (int)(regionMax * 0.8 / smallEntries);
+
+ for (int i = 0; i < smallEntries; i++)
+ cache.put(i, valueFor(i, smallValSize));
+
+ assertFalse("Eviction must not have started during pre-fill",
isEvictionsStarted(ignite));
+
+ CountDownLatch startLatch = new CountDownLatch(1);
+ AtomicInteger threadIdx = new AtomicInteger();
+
+ int largeValSize = (int)(regionMax / 20);
+
+ IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(()
-> {
+ U.awaitQuiet(startLatch);
+
+ int idx = threadIdx.getAndIncrement();
+
+ for (int k = 0; k < largeRowsPerThread; k++) {
+ int key = smallEntries + idx * largeRowsPerThread + k;
+
+ cache.put(key, valueFor(key, largeValSize));
+ }
+ }, threads, "paged-writer");
+
+ startLatch.countDown();
+
+ fut.get(TimeUnit.MINUTES.toMillis(3));
Review Comment:
`TimeUnit.MINUTES.toMillis(1)` is large enough for any case in this test,
why different timeouts is used 1-2-3 minutes? Let's unify timeout and introduce
some constant instead of TimeUnit.MINUTES.toMillis(x)
##########
modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/paged/PageEvictionWithExpiryPolicyAbstractTest.java:
##########
@@ -115,85 +64,54 @@ private IgniteCache<Integer, Object> createCache(IgniteEx
ignite, String cacheNa
public void testLargePutWithExpiryNoDeadlock() throws Exception {
Review Comment:
But there still no concurrent expire. Consider the following modification:
- Set a smaller TTL for expiring (for example 100 ms)
- Concurrently put entries to TTL cache for some period of time (for example
2 seconds, time must be greater than TTL to start expire and enough to fill the
region and evict the large amount of entries)
- During this period some entries will be expired and some will be evicted
by concurrent puts
##########
modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/paged/PageEvictionSizeAwareAbstractTest.java:
##########
@@ -0,0 +1,630 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.cache.eviction.paged;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.Lock;
+import javax.cache.CacheException;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.IgniteDataStreamer;
+import org.apache.ignite.cache.CacheWriteSynchronizationMode;
+import org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.DataRegionConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.IgniteInternalFuture;
+import org.apache.ignite.internal.mem.IgniteOutOfMemoryException;
+import org.apache.ignite.internal.processors.cache.persistence.DataRegion;
+import
org.apache.ignite.internal.processors.cache.persistence.IgniteCacheDatabaseSharedManager;
+import
org.apache.ignite.internal.processors.cache.persistence.freelist.AbstractFreeList;
+import
org.apache.ignite.internal.processors.cache.persistence.freelist.FreeList;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.lang.IgniteFuture;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.apache.ignite.testframework.junits.WithSystemProperty;
+import org.junit.Test;
+
+import static
org.apache.ignite.IgniteSystemProperties.IGNITE_PAGE_EVICTION_NO_PROGRESS_TIMEOUT_MILLIS;
+
+/** Tests size-aware page eviction on in-memory (non-persistent) data regions.
*/
+public abstract class PageEvictionSizeAwareAbstractTest extends
PageEvictionAbstractTest {
+ /** Name of the rebalance supplier node, which has a twice larger data
region. */
+ private static final String SUPPLIER = "supplier";
+
+ /** Eviction no-progress timeout (milliseconds) of the guard test. */
+ private static final long GUARD_TIMEOUT = 3_000;
+
+ /** {@inheritDoc} */
+ @Override protected IgniteConfiguration getConfiguration(String gridName)
throws Exception {
+ IgniteConfiguration cfg = super.getConfiguration(gridName);
+
+ if (SUPPLIER.equals(gridName)) {
+ DataRegionConfiguration regCfg =
cfg.getDataStorageConfiguration().getDefaultDataRegionConfiguration();
+
+ regCfg.setMaxSize(regCfg.getMaxSize() * 2);
+ }
+
+ return cfg;
+ }
+
+ /** {@inheritDoc} */
+ @Override protected void afterTest() throws Exception {
+ super.afterTest();
+
+ stopAllGrids();
+
+ assertNoOomFailures();
+ }
+
+ /**
+ * Rebalancing inserts the received rows in batches reserved as a whole:
the demander, which receives more data than
+ * fits below its threshold, evicts at the threshold and releases the
reservations.
+ */
+ @Test
+ public void testRebalanceHonorsThreshold() throws Exception {
+ IgniteEx supplier = startGrid(SUPPLIER);
+
+ IgniteCache<Integer, Object> cache = supplier.createCache(new
CacheConfiguration<Integer, Object>(DEFAULT_CACHE_NAME)
+ .setAffinity(new RendezvousAffinityFunction(false, 32))
+ .setBackups(1));
+
+ byte[] val = new byte[pageSize(supplier) - 200];
+
+ // One page per entry: 1.5 times the demander region.
+ int keys = (int)(totalPages(supplier) * 0.75);
+
+ for (int base = 0; base < keys; base += 100) {
+ Map<Integer, Object> batch = new HashMap<>();
+
+ for (int i = base; i < Math.min(base + 100, keys); i++)
+ batch.put(i, val);
+
+ cache.putAll(batch);
+ }
+
+ IgniteEx demander = startGrid(1);
+
+ awaitPartitionMapExchange();
+
+ assertHonorsThreshold(demander);
+
+ assertEquals(0,
((AtomicLong)GridTestUtils.getFieldValue(defaultFreeList(demander),
"reservedPages")).get());
+ }
+
+ /** */
+ @Test
+ public void testRecordLargerThanRegionOom() throws Exception {
+ IgniteEx ignite = startGrid(1);
+
+ IgniteCache<Integer, Object> cache = createCache(ignite,
DEFAULT_CACHE_NAME);
+
+ long regionSize = regionMaxSize(ignite);
+
+ GridTestUtils.assertThrowsWithCause(
+ () -> cache.put(1, new byte[(int)(regionSize * 2)]),
+ IgniteOutOfMemoryException.class
+ );
+
+ GridTestUtils.assertThrowsWithCause(
+ () -> {
+ Map<Integer, Object> batch = new HashMap<>();
+
+ Object val = new byte[(int)(regionSize / 4)];
+
+ for (int i = 0; i < 4; i++)
+ batch.put(i, val);
+
+ cache.putAll(batch);
+ },
+ IgniteOutOfMemoryException.class
+ );
+ }
+
+ /** */
+ @Test
+ public void testPutAllLargeRows() throws Exception {
+ IgniteEx ignite = startGrid(1);
+
+ IgniteCache<Integer, Object> cache = createCache(ignite,
DEFAULT_CACHE_NAME);
+
+ int smallEntries = (int)(totalPages(ignite) * 0.85);
+
+ byte[] small = new byte[pageSize(ignite) - 200];
+
+ for (int i = 0; i < smallEntries; i++)
+ cache.put(smallEntries + i, small);
+
+ assertTrue("Pre-fill must load >80% of pages", loadedPages(ignite) >
totalPages(ignite) * 0.80);
+ assertFalse("Eviction must not start during pre-fill",
isEvictionsStarted(ignite));
+
+ int putAllLargeRows = 3;
+ Map<Integer, Object> large = new HashMap<>();
+
+ Object val = new byte[(int)(regionMaxSize(ignite) / 4)];
+
+ for (int i = 0; i < putAllLargeRows; i++)
+ large.put(i, val);
+
+ cache.putAll(large);
+
+ assertTrue("Eviction must start after putAll of large rows near
capacity", isEvictionsStarted(ignite));
+
+ for (int i = 0; i < putAllLargeRows; i++)
+ assertNotNull("Large row " + i + " must be readable after putAll",
cache.get(i));
+ }
+
+ /** */
+ @Test
+ public void testUpdateRowGrows() throws Exception {
+ IgniteEx ignite = startGrid(1);
+
+ IgniteCache<Integer, Object> cache = createCache(ignite,
DEFAULT_CACHE_NAME);
+
+ int smallEntries = (int)(totalPages(ignite) * 0.85);
+ byte[] small = new byte[pageSize(ignite) - 200];
+
+ for (int i = 0; i < smallEntries; i++)
+ cache.put(smallEntries + i, small);
+
+ cache.put(1, new byte[1024]);
+
+ byte[] big = new byte[(int)(regionMaxSize(ignite) / 4)];
+
+ Arrays.fill(big, (byte)7);
+
+ cache.put(1, big);
+
+ assertTrue("Eviction must have started after growing a row near
capacity", isEvictionsStarted(ignite));
+
+ byte[] read = (byte[])cache.get(1);
+
+ assertNotNull("Updated large value must be readable", read);
+ assertTrue("Updated value must equal the stored value",
Arrays.equals(big, read));
+ }
+
+ /** */
+ @Test
+ public void testLargeRowBelowThresholdUsesHeadroomNoEviction() throws
Exception {
+ IgniteEx ignite = startGrid(1);
+
+ IgniteCache<Integer, Object> cache = createCache(ignite,
DEFAULT_CACHE_NAME);
+
+ int preFill = fillRegion(ignite, cache, 0.4);
+
+ long loadedPages = loadedPages(ignite);
+ long totalPages = totalPages(ignite);
+
+ assertTrue("Pre-fill must load ~40% of pages", loadedPages >
totalPages * 0.30 && loadedPages < totalPages * 0.50);
+ assertFalse("Eviction must not have started during pre-fill below
threshold", isEvictionsStarted(ignite));
+
+ byte[] val = new byte[(int)(regionMaxSize(ignite) * 0.3)];
+
+ Arrays.fill(val, (byte)1);
+
+ cache.put(preFill, val);
+
+ assertFalse("Eviction must not start when the region stays below the
threshold", isEvictionsStarted(ignite));
+
+ for (int i = 0; i < preFill; i++)
+ assertNotNull("Pre-filled entry " + i + " must not be evicted
below threshold", cache.get(i));
+
+ byte[] read = (byte[])cache.get(preFill);
+
+ assertNotNull("Large row must be readable", read);
+ assertTrue("Value read back must equal the stored value",
Arrays.equals(val, read));
+ }
+
+ /** */
+ @Test
+ public void testLargeRowAboveThresholdEvictsAndSucceeds() throws Exception
{
+ IgniteEx ignite = startGrid(1);
+
+ IgniteCache<Integer, Object> cache = createCache(ignite,
DEFAULT_CACHE_NAME);
+
+ int smallEntries = fillRegion(ignite, cache, 0.85);
+
+ assertTrue("Pre-fill must load >80% of pages", loadedPages(ignite) >
totalPages(ignite) * 0.80);
+
+ byte[] val = new byte[(int)(regionMaxSize(ignite) / 4)];
+
+ Arrays.fill(val, (byte)1);
+
+ int largeKey = smallEntries + 1;
+
+ cache.put(largeKey, val);
+
+ assertTrue("Eviction must have started after a large put near
capacity", isEvictionsStarted(ignite));
+
+ byte[] read = (byte[])cache.get(largeKey);
+
+ assertNotNull("Large row must be readable after eviction", read);
+ assertTrue("Value read back must equal the stored value",
Arrays.equals(val, read));
+ }
+
+ /** Atomic cache with a backup: primary batch and backup updates must
evict at the same threshold. */
+ @Test
+ public void testAtomicBackupHonorsThreshold() throws Exception {
+ IgniteEx ignite = startGrids(2);
+
+ IgniteCache<Integer, Object> cache = ignite.createCache(new
CacheConfiguration<Integer, Object>(DEFAULT_CACHE_NAME)
+ .setAffinity(new RendezvousAffinityFunction(false, 32))
+ .setBackups(1)
+
.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC));
+
+ awaitPartitionMapExchange();
+
+ byte[] val = new byte[pageSize(ignite) - 200];
+
+ int keys = (int)(totalPages(ignite) * 3);
+
+ for (int base = 0; base < keys; base += 100) {
+ Map<Integer, Object> batch = new HashMap<>();
+
+ for (int i = base; i < base + 100; i++)
+ batch.put(i, val);
+
+ cache.putAll(batch);
+ }
+
+ assertHonorsThreshold(grid(0));
+ assertHonorsThreshold(grid(1));
+ }
+
+ /** Transactional cache: commits must evict at the threshold. */
+ @Test
+ public void testTransactionalHonorsThreshold() throws Exception {
+ IgniteEx ignite = startGrid(0);
+
+ IgniteCache<Integer, Object> cache = createCache(ignite,
DEFAULT_CACHE_NAME, 0, true);
+
+ byte[] val = new byte[pageSize(ignite) - 200];
+
+ int keys = (int)(totalPages(ignite) * 3);
+
+ for (int i = 0; i < keys; i++)
+ cache.put(i, val);
+
+ assertHonorsThreshold(ignite);
+ }
+
+ /** A row that does not fit below the threshold together with the pool may
use the space above it. */
+ @Test
+ public void testRowLargerThanThresholdFits() throws Exception {
+ IgniteEx ignite = startGrid(0);
+
+ IgniteCache<Integer, Object> cache = createCache(ignite,
DEFAULT_CACHE_NAME);
+
+ fillRegion(ignite, cache, 0.5);
+
+ int largeValSize = (int)(regionMaxSize(ignite) * EVICTION_THRESHOLD);
+
+ cache.put(-1, new byte[largeValSize]);
+
+ assertEquals(largeValSize, ((byte[])cache.get(-1)).length);
+ }
+
+ /**
+ * A commit can not be refused, so the reservation does not stop a row
larger than the region: it fails in the page
+ * memory, which reports a critical error to the failure handler (the
default handler stops the node).
+ */
+ @Test
+ public void testTransactionalRecordLargerThanRegionFailsInPageMemory()
throws Exception {
+ IgniteEx ignite = startGrid(0);
+
+ IgniteCache<Integer, Object> cache = createCache(ignite,
DEFAULT_CACHE_NAME, 0, true);
+
+ long regionSize = regionMaxSize(ignite);
+
+ GridTestUtils.assertThrowsWithCause(
+ () -> cache.put(1, new byte[(int)(regionSize * 2)]),
+ IgniteOutOfMemoryException.class
+ );
+
+ assertTrue("A page memory out-of-memory must reach the failure
handler", OOM_FAILURES.get() > 0);
+
+ // Expected in this test only.
+ OOM_FAILURES.set(0);
+ }
+
+ /** The default streamer mode inserts through {@code initialValue}, which
reserves pages for every row. */
+ @Test
+ public void testDefaultStreamerHonorsThreshold() throws Exception {
+ IgniteEx ignite = startGrid(0);
+
+ createCache(ignite, DEFAULT_CACHE_NAME);
+
+ byte[] val = new byte[pageSize(ignite) - 200];
+
+ int keys = (int)(totalPages(ignite) * 3);
+
+ try (IgniteDataStreamer<Integer, Object> streamer =
ignite.dataStreamer(DEFAULT_CACHE_NAME)) {
+ for (int i = 0; i < keys; i++)
+ streamer.addData(i, val);
+ }
+
+ assertHonorsThreshold(ignite);
+ }
+
+ /**
+ * The default streamer mode can refuse a row: the refusal reaches the
caller through the future of the operation and
+ * does not reach the failure handler (see {@link #afterTest()}). Closing
the streamer reports the failed operation
+ * too, but not necessarily with its cause.
+ */
+ @Test
+ public void testDefaultStreamerRecordLargerThanRegionRefused() throws
Exception {
+ IgniteEx ignite = startGrid(0);
+
+ createCache(ignite, DEFAULT_CACHE_NAME);
+
+ IgniteDataStreamer<Integer, Object> streamer =
ignite.dataStreamer(DEFAULT_CACHE_NAME);
+
+ IgniteFuture<?> fut = streamer.addData(1, new
byte[(int)(regionMaxSize(ignite) * 2)]);
+
+ GridTestUtils.assertThrows(log, () -> {
+ streamer.close();
+
+ return null;
+ }, CacheException.class, null);
+
+ GridTestUtils.assertThrowsWithCause(() -> fut.get(),
IgniteOutOfMemoryException.class);
+
+ assertFalse("A refused row must not start the eviction",
isEvictionsStarted(ignite));
+ }
+
+ /**
+ * A claim that fits the raw region size, but not the pages the region can
actually load, is refused at once: it must
+ * not evict the data before the refusal.
+ */
+ @Test
+ public void testClaimAboveLoadablePagesRefusedWithoutEviction() throws
Exception {
+ IgniteEx ignite = startGrid(0);
+
+ IgniteCache<Integer, Object> cache = createCache(ignite,
DEFAULT_CACHE_NAME);
+
+ int entries = fillRegion(ignite, cache, 0.5);
+
+ FreeList<?> freeList = defaultFreeList(ignite);
+
+ long claimPages = totalPages(ignite) -
defaultRegion(ignite).config().getEmptyPagesPoolSize();
+
+ int pagePayload = GridTestUtils.getFieldValue(freeList,
"MIN_SIZE_FOR_DATA_PAGE");
+
+ GridTestUtils.assertThrows(log, () -> freeList.reserve(claimPages *
pagePayload, false),
+ IgniteOutOfMemoryException.class, null);
+
+ assertFalse("A refused claim must not start the eviction",
isEvictionsStarted(ignite));
+ assertEquals(entries, cache.size());
+ }
+
+ /**
+ * When no page can be evicted (all resident entries are locked), the
eviction progress guard refuses an insert that
+ * can be refused (a primary atomic update) with an out-of-memory, while a
best-effort reservation (backup update,
+ * transaction commit, rebalancing) gives up without an error. Once the
stall is detected, the already waiting and
+ * the following best-effort reservations give up without waiting for the
timeout, until the eviction makes progress
+ * again.
+ */
+ @Test
+ @WithSystemProperty(key = IGNITE_PAGE_EVICTION_NO_PROGRESS_TIMEOUT_MILLIS,
value = "" + GUARD_TIMEOUT)
+ public void testGuardWhenAllEntriesLocked() throws Exception {
+ IgniteEx ignite = startGrid(1);
+
+ IgniteCache<Integer, Object> atomicCache = createCache(ignite,
DEFAULT_CACHE_NAME);
+
+ CountDownLatch release = new CountDownLatch(1);
+
+ IgniteInternalFuture<?> lockerFut = fillRegionAndLockEntries(ignite,
release);
+
+ try {
+ int largeValSize = 4 * 1024 * 1024;
+
+ GridTestUtils.assertThrowsWithCause(
+ () -> atomicCache.put(0, new byte[largeValSize]),
+ IgniteOutOfMemoryException.class
+ );
+
+ IgniteCacheDatabaseSharedManager db =
ignite.context().cache().context().database();
+
+ DataRegion region = defaultRegion(ignite);
+
+ FreeList<?> freeList = db.freeList(region.config().getName());
+
+ // A reservation that waits for the timeout takes at least the
timeout (with the 10 ms clock error), a
+ // reservation that gives up does not: the bound separates them
far from the expected time of both.
+ long bound = GUARD_TIMEOUT * 9 / 10;
+
+ assertFalse(GridTestUtils.<Boolean>getFieldValue(freeList,
"stalled"));
+
+ IgniteInternalFuture<Long> firstFut = GridTestUtils.runAsync(() ->
db.reserveInRegion(region, largeValSize, true),
+ "size-aware-guard-first");
+
+ U.sleep(GUARD_TIMEOUT * 9 / 10);
+
+ // Starts waiting before the stall is detected and must be
released once the first reservation detects it.
+ long start = System.currentTimeMillis();
+
+ assertEquals(0, db.reserveInRegion(region, largeValSize, true));
+
+ assertTrue("Waiting best-effort reservation must give up once the
stall is detected",
+ System.currentTimeMillis() - start < bound);
+
+ assertEquals(0, (long)firstFut.get(60, TimeUnit.SECONDS));
+
+ assertTrue(GridTestUtils.<Boolean>getFieldValue(freeList,
"stalled"));
+
+ // A reservation that needs no eviction says nothing about it, so
the stall stays.
+ long smallReserved = db.reserveInRegion(region, 1, false);
+
+ try {
+ assertEquals(1, smallReserved);
+
+ assertTrue(GridTestUtils.<Boolean>getFieldValue(freeList,
"stalled"));
+ }
+ finally {
+ db.releaseInRegion(region, smallReserved);
+ }
+
+ // Reservations that gave up return the pages they accumulated.
+ assertEquals(0, GridTestUtils.<AtomicLong>getFieldValue(freeList,
"reservedPages").get());
+
+ // The stall is not a time window: once the timeout passes,
best-effort reservations still do not wait.
+ U.sleep(GUARD_TIMEOUT + 500);
+
+ start = System.currentTimeMillis();
+
+ assertEquals(0, db.reserveInRegion(region, largeValSize, true));
+
+ assertTrue("Best-effort reservation must not wait for the timeout
while the eviction is stalled",
+ System.currentTimeMillis() - start < bound);
+
+ release.countDown();
+
+ lockerFut.get(10, TimeUnit.SECONDS);
+
+ // The unlocked entries can be evicted again: the reservation
succeeds and clears the stall. While stalled, a
+ // best-effort reservation gives up after a single eviction
attempt, which may free no page, so it is retried.
+ long reserved = 0;
+
+ long deadline = System.currentTimeMillis() +
TimeUnit.SECONDS.toMillis(60);
+
+ while (reserved == 0 && System.currentTimeMillis() < deadline)
+ reserved = db.reserveInRegion(region, largeValSize, true);
+
+ try {
+ assertTrue("Reservation must succeed once the entries are
unlocked", reserved > 0);
+
+ assertFalse(GridTestUtils.<Boolean>getFieldValue(freeList,
"stalled"));
Review Comment:
Consider to use getter (see previous comments)
##########
modules/core/src/test/java/org/apache/ignite/internal/processors/cache/eviction/paged/PageEvictionReservedPagesSelfTest.java:
##########
@@ -0,0 +1,328 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.cache.eviction.paged;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicLong;
+import javax.cache.processor.EntryProcessor;
+import javax.cache.processor.MutableEntry;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.Ignition;
+import org.apache.ignite.cache.CacheWriteSynchronizationMode;
+import org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.DataPageEvictionMode;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.metric.IoStatisticsHolderNoOp;
+import org.apache.ignite.internal.processors.cache.CacheObject;
+import org.apache.ignite.internal.processors.cache.GridCacheContext;
+import
org.apache.ignite.internal.processors.cache.IgniteCacheOffheapManager.CacheDataStore;
+import org.apache.ignite.internal.processors.cache.KeyCacheObject;
+import org.apache.ignite.internal.processors.cache.persistence.CacheDataRow;
+import
org.apache.ignite.internal.processors.cache.persistence.freelist.FreeList;
+import org.apache.ignite.internal.processors.cache.tree.DataRow;
+import org.apache.ignite.internal.processors.cache.version.GridCacheVersion;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.junit.Test;
+
+import static org.apache.ignite.testframework.GridTestUtils.getFieldValue;
+
+/**
+ * Tests the page reservation accounting of an in-memory free list with
enabled page eviction: the reservation is fully
+ * released after batch inserts (whole-page fragments, fill-up-handler rows,
whole-page-aligned rows, concurrent batch
+ * inserts), concurrent puts and putAll on a backup node, and read-through
loads; the empty pages pool is shared by
+ * all concurrent reservations instead of being added to each of them.
+ */
+public class PageEvictionReservedPagesSelfTest extends
PageEvictionAbstractTest {
+ /** Empty pages pool size: close to the allowed maximum of 10% of the
region. */
+ private static final int LARGE_EMPTY_PAGES_POOL_SIZE = 900;
+
+ /** {@inheritDoc} */
+ @Override protected IgniteConfiguration getConfiguration(String gridName)
throws Exception {
+ IgniteConfiguration cfg =
setEvictionMode(DataPageEvictionMode.RANDOM_LRU,
super.getConfiguration(gridName));
+
+
cfg.getDataStorageConfiguration().getDefaultDataRegionConfiguration().setEmptyPagesPoolSize(LARGE_EMPTY_PAGES_POOL_SIZE);
+
+ return cfg;
+ }
+
+ /** {@inheritDoc} */
+ @Override protected void afterTest() throws Exception {
+ super.afterTest();
+
+ stopAllGrids();
+
+ assertNoOomFailures();
+ }
+
+ /** */
+ @Test
+ public void testReservedPagesReleasedAfterBatchInserts() throws Exception {
+ IgniteEx ignite = startGrid(0);
+
+ createCache(ignite, DEFAULT_CACHE_NAME).put(0, 0);
+
+ FreeList<CacheDataRow> freeList = freeList(ignite);
+
+ // Phase 1: small rows that fit into a single page (fill-up path, no
whole-page fragments).
+ insertBatchesAndCheckReservedPages(ignite, freeList, 10);
+
+ // Phase 2: large rows that span multiple pages (whole-page fragments
+ fill-up remainder).
+ insertBatchesAndCheckReservedPages(ignite, freeList, 10_000);
+
+ // Phase 3: rows sized to an exact multiple of the whole-page fragment
size (whole-page fragments only).
+ int minPageSize = getFieldValue(freeList, "MIN_SIZE_FOR_DATA_PAGE");
+
+ int fixedSize = row(cacheContext(ignite), 0, 0).size();
+
+ insertBatchesAndCheckReservedPages(ignite, freeList, 2 * minPageSize -
fixedSize);
+ }
+
+ /** */
+ @Test
+ public void testReservedPagesReleasedAfterConcurrentBatchInserts() throws
Exception {
+ IgniteEx ignite = startGrid(0);
+
+ createCache(ignite, DEFAULT_CACHE_NAME).put(0, 0);
+
+ FreeList<CacheDataRow> freeList = freeList(ignite);
+
+ GridCacheContext<?, ?> cctx = cacheContext(ignite);
+
+ int rowsPerThread = 200;
+
+ AtomicInteger keyGen = new AtomicInteger();
+
+ GridTestUtils.runMultiThreaded(() -> {
+ try {
+ Collection<CacheDataRow> rows = new ArrayList<>(rowsPerThread);
+
+ for (int i = 0; i < rowsPerThread; i++)
+ rows.add(row(cctx, keyGen.incrementAndGet(), 100));
+
+ freeList.insertDataRows(rows, IoStatisticsHolderNoOp.INSTANCE);
+ }
+ catch (IgniteCheckedException e) {
+ throw new RuntimeException(e);
+ }
+ }, 8, "concurrent-batch-insert");
+
+ assertAllReleased(10_000);
+ }
+
+ /** */
+ @Test
+ public void testReservedPagesReleasedAfterConcurrentPuts() throws
Exception {
+ IgniteCache<Integer, byte[]> cache = startGridsWithBackupCache();
+
+ AtomicInteger keyGen = new AtomicInteger();
+
+ GridTestUtils.runMultiThreaded(() -> {
+ for (int i = 0; i < 300; i++)
+ cache.put(keyGen.incrementAndGet(), new byte[500]);
+ }, 8, "concurrent-put");
+
+ assertAllReleased(10_000);
+ }
+
+ /** */
+ @Test
+ public void testReservedPagesReleasedAfterConcurrentPutAll() throws
Exception {
+ IgniteCache<Integer, byte[]> cache = startGridsWithBackupCache();
+
+ AtomicInteger keyGen = new AtomicInteger();
+
+ GridTestUtils.runMultiThreaded(() -> {
+ for (int b = 0; b < 10; b++) {
+ Map<Integer, byte[]> batch = new HashMap<>();
+
+ for (int i = 0; i < 100; i++)
+ batch.put(keyGen.incrementAndGet(), new byte[300]);
+
+ cache.putAll(batch);
+ }
+ }, 4, "concurrent-putAll");
+
+ assertAllReleased(10_000);
+ }
+
+ /** Read-through loads reserve pages before the entry lock and must
release them, also on the backup. */
+ @Test
+ public void testReservedPagesReleasedAfterReadThrough() throws Exception {
+ startGrids(2);
+
+ IgniteCache<Integer, byte[]> cache = grid(0).getOrCreateCache(new
CacheConfiguration<Integer, byte[]>(DEFAULT_CACHE_NAME)
+ .setAffinity(new RendezvousAffinityFunction(false, 32))
+
.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC)
+ .setBackups(1)
+ .setReadThrough(true)
+ .setCacheStoreFactory(new TestStoreFactory()));
+
+ awaitPartitionMapExchange();
+
+ for (int i = 1; i <= 15_000; i++)
+ cache.get(i);
+
+ assertTrue("Eviction must have started during read-through loads",
isEvictionsStarted(grid(0)));
+
+ assertAllReleased(10_000);
+ }
+
+ /** The reservation is held while the row is written: the entry processor
runs inside this window. */
+ @Test
+ public void testReservationHeldWhileRowIsWritten() throws Exception {
+ IgniteCache<Integer, Object> cache = createCache(startGrid(0),
DEFAULT_CACHE_NAME);
+
+ cache.put(0, 0);
+
+ long inFlight = cache.invoke(1, new ReservedPagesProbe());
+
+ assertTrue("Reservation must be held while the row is written: " +
inFlight, inFlight > 0);
+
+ assertAllReleased(10_000);
+ }
+
+ /**
+ * Holds reservations of single-page rows of many writers at once in an
empty region: the sum of per-writer pools
+ * would exceed the region (and there is nothing to evict), but a single
shared pool leaves room for all of them.
+ */
+ @Test
+ public void testSimultaneousReservationsShareThePool() throws Exception {
+ IgniteEx ignite = startGrid(0);
+
+ createCache(ignite, DEFAULT_CACHE_NAME).put(0, 0);
+
+ int writers = 16;
+
+ assertTrue(writers * LARGE_EMPTY_PAGES_POOL_SIZE > totalPages(ignite));
+
+ FreeList<CacheDataRow> freeList = freeList(ignite);
+
+ long[] reserved = new long[writers];
+
+ try {
+ for (int i = 0; i < writers; i++) {
+ reserved[i] = freeList.reserve(pageSize(ignite) / 2, false);
+
+ assertEquals(1, reserved[i]);
+ }
+ }
+ finally {
+ for (long pages : reserved)
+ freeList.releaseReserved(pages);
+ }
+
+ assertEquals(0, reservedPages(freeList));
+ }
+
+ /** @return Cache with a backup on two started nodes. */
+ private IgniteCache<Integer, byte[]> startGridsWithBackupCache() throws
Exception {
+ startGrids(2);
+
+ IgniteCache<Integer, byte[]> cache = grid(0).createCache(new
CacheConfiguration<Integer, byte[]>(DEFAULT_CACHE_NAME)
+ .setAffinity(new RendezvousAffinityFunction(false, 32))
+ .setBackups(1)
+
.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC));
+
+ awaitPartitionMapExchange();
+
+ return cache;
+ }
+
+ /**
+ * Asserts that {@code reservedPages} is {@code 0} on every data store of
every started node, waiting up to
+ * {@code timeoutMs} for the reservation to drain.
+ *
+ * @param timeoutMs Wait timeout in milliseconds.
+ * @throws Exception If the reservation does not drain in time.
+ */
+ private static void assertAllReleased(long timeoutMs) throws Exception {
+ for (Ignite ignite : Ignition.allGrids()) {
+ for (CacheDataStore store :
cacheContext((IgniteEx)ignite).offheap().cacheDataStores()) {
+ FreeList<CacheDataRow> freeList =
(FreeList<CacheDataRow>)store.rowStore().freeList();
+
+ assertTrue(GridTestUtils.waitForCondition(() ->
reservedPages(freeList) == 0, timeoutMs));
+ }
+ }
+ }
+
+ /** */
+ private static long reservedPages(FreeList<CacheDataRow> freeList) {
+ return ((AtomicLong)getFieldValue(freeList, "reservedPages")).get();
Review Comment:
Maybe introduce some getter instead?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]