You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by ib...@apache.org on 2025/03/04 09:20:36 UTC
(ignite-3) branch main updated: IGNITE-24627 Add unit tests for target ratio based throttling (#5330)
This is an automated email from the ASF dual-hosted git repository.
ibessonov pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/ignite-3.git
The following commit(s) were added to refs/heads/main by this push:
new 702fff9dcb1 IGNITE-24627 Add unit tests for target ratio based throttling (#5330)
702fff9dcb1 is described below
commit 702fff9dcb1054ea7e3f29db33bcc782b557c074
Author: Ivan Bessonov <be...@gmail.com>
AuthorDate: Tue Mar 4 12:20:31 2025 +0300
IGNITE-24627 Add unit tests for target ratio based throttling (#5330)
---
.../apache/ignite/internal/util/IgniteUtils.java | 34 ++
.../ItBplusTreePersistentPageMemoryTest.java | 1 -
...BplusTreeReuseListPersistentPageMemoryTest.java | 1 -
.../persistence/PersistentPageMemory.java | 29 +-
.../checkpoint/CheckpointTimeoutLock.java | 14 +-
.../persistence/store/AbstractFilePageStoreIo.java | 2 +
.../throttling/PagesWriteThrottlePolicy.java | 4 +-
...tle.java => TargetRatioPagesWriteThrottle.java} | 18 +-
.../checkpoint/PageMemoryThrottlingTest.java | 430 +++++++++++++++++++++
.../replacement/AbstractPageReplacementTest.java | 1 -
.../pagememory/PersistentPageMemoryDataRegion.java | 5 -
.../pagememory/PersistentPageMemoryNoLoadTest.java | 1 -
12 files changed, 499 insertions(+), 41 deletions(-)
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java b/modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java
index fa3066e1b27..3ce0017f435 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java
@@ -57,6 +57,7 @@ import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
@@ -771,6 +772,39 @@ public class IgniteUtils {
}
}
+ /**
+ * Waits if necessary for this future to complete, and then returns its result ignoring interrupts.
+ *
+ * @param future Future to wait on.
+ * @param timeout Timeout in milliseconds.
+ * @return Result value.
+ * @throws CancellationException If this future was cancelled.
+ * @throws ExecutionException If this future completed exceptionally.
+ */
+ public static <T> T getUninterruptibly(Future<T> future, long timeout) throws ExecutionException, TimeoutException {
+ boolean interrupted = false;
+
+ try {
+ long start = System.currentTimeMillis();
+ while (true) {
+ long current = System.currentTimeMillis();
+ long wait = timeout - (current - start);
+ if (wait < 0) {
+ throw new TimeoutException("Timeout waiting for future completion [timeout=" + timeout + "ms]");
+ }
+
+ try {
+ return future.get(wait, TimeUnit.MILLISECONDS);
+ } catch (InterruptedException e) {
+ interrupted = true;
+ }
+ }
+ } finally {
+ if (interrupted) {
+ Thread.currentThread().interrupt();
+ }
+ }
+ }
/**
* Blocks until the future is completed and either returns the value from the normal completion, or throws an exception if the future
diff --git a/modules/page-memory/src/integrationTest/java/org/apache/ignite/internal/pagememory/tree/persistence/ItBplusTreePersistentPageMemoryTest.java b/modules/page-memory/src/integrationTest/java/org/apache/ignite/internal/pagememory/tree/persistence/ItBplusTreePersistentPageMemoryTest.java
index 3e6d1792a21..b7bb27fb590 100644
--- a/modules/page-memory/src/integrationTest/java/org/apache/ignite/internal/pagememory/tree/persistence/ItBplusTreePersistentPageMemoryTest.java
+++ b/modules/page-memory/src/integrationTest/java/org/apache/ignite/internal/pagememory/tree/persistence/ItBplusTreePersistentPageMemoryTest.java
@@ -92,7 +92,6 @@ public class ItBplusTreePersistentPageMemoryTest extends AbstractBplusTreePageMe
(fullPageId, buf, tag) -> {
},
mockCheckpointTimeoutLock(true),
- () -> null,
PAGE_SIZE,
wrapLock(offheapReadWriteLock)
);
diff --git a/modules/page-memory/src/integrationTest/java/org/apache/ignite/internal/pagememory/tree/persistence/ItBplusTreeReuseListPersistentPageMemoryTest.java b/modules/page-memory/src/integrationTest/java/org/apache/ignite/internal/pagememory/tree/persistence/ItBplusTreeReuseListPersistentPageMemoryTest.java
index 467b716dcc7..414bd46869b 100644
--- a/modules/page-memory/src/integrationTest/java/org/apache/ignite/internal/pagememory/tree/persistence/ItBplusTreeReuseListPersistentPageMemoryTest.java
+++ b/modules/page-memory/src/integrationTest/java/org/apache/ignite/internal/pagememory/tree/persistence/ItBplusTreeReuseListPersistentPageMemoryTest.java
@@ -75,7 +75,6 @@ public class ItBplusTreeReuseListPersistentPageMemoryTest extends AbstractBplusT
(fullPageId, buf, tag) -> {
},
mockCheckpointTimeoutLock(true),
- () -> null,
PAGE_SIZE,
wrapLock(new OffheapReadWriteLock(OffheapReadWriteLock.DEFAULT_CONCURRENCY_LEVEL))
);
diff --git a/modules/page-memory/src/main/java/org/apache/ignite/internal/pagememory/persistence/PersistentPageMemory.java b/modules/page-memory/src/main/java/org/apache/ignite/internal/pagememory/persistence/PersistentPageMemory.java
index ee2d254e7e7..68ac8c7a548 100644
--- a/modules/page-memory/src/main/java/org/apache/ignite/internal/pagememory/persistence/PersistentPageMemory.java
+++ b/modules/page-memory/src/main/java/org/apache/ignite/internal/pagememory/persistence/PersistentPageMemory.java
@@ -76,7 +76,6 @@ import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantReadWriteLock;
-import java.util.function.Supplier;
import org.apache.ignite.internal.lang.IgniteInternalCheckedException;
import org.apache.ignite.internal.lang.IgniteInternalException;
import org.apache.ignite.internal.logger.IgniteLogger;
@@ -162,9 +161,6 @@ public class PersistentPageMemory implements PageMemory {
/** Page IO registry. */
private final PageIoRegistry ioRegistry;
- /** The supplier of checkpoint progress. */
- private final Supplier<@Nullable CheckpointProgress> checkpointProgressSupplier;
-
/** Page manager. */
private final PageReadWriteManager pageStoreManager;
@@ -207,7 +203,7 @@ public class PersistentPageMemory implements PageMemory {
private volatile @Nullable PagePool checkpointPool;
/** Pages write throttle. */
- private final @Nullable PagesWriteThrottlePolicy writeThrottle;
+ private volatile @Nullable PagesWriteThrottlePolicy writeThrottle;
/**
* Delayed page replacement (rotation with disk) tracker. Because other thread may require exactly the same page to be loaded from
@@ -228,7 +224,6 @@ public class PersistentPageMemory implements PageMemory {
* @param pageStoreManager Page store manager.
* @param flushDirtyPageForReplacement Write callback invoked when a dirty page is removed for replacement.
* @param checkpointTimeoutLock Checkpoint timeout lock.
- * @param checkpointProgressSupplier The supplier of checkpoint progress.
* @param pageSize Page size in bytes.
* @param rwLock Read-write lock for pages.
*/
@@ -240,14 +235,12 @@ public class PersistentPageMemory implements PageMemory {
PageReadWriteManager pageStoreManager,
WriteDirtyPage flushDirtyPageForReplacement,
CheckpointTimeoutLock checkpointTimeoutLock,
- Supplier<@Nullable CheckpointProgress> checkpointProgressSupplier,
// TODO: IGNITE-17017 Move to common config
int pageSize,
OffheapReadWriteLock rwLock
) {
this.storageProfileView = (PersistentPageMemoryProfileView) storageProfileConfiguration.value();
this.ioRegistry = ioRegistry;
- this.checkpointProgressSupplier = checkpointProgressSupplier;
this.sizes = concat(segmentSizes, checkpointBufferSize);
this.pageStoreManager = pageStoreManager;
this.checkpointTimeoutLock = checkpointTimeoutLock;
@@ -279,14 +272,18 @@ public class PersistentPageMemory implements PageMemory {
delayedPageReplacementTracker = new DelayedPageReplacementTracker(pageSize, flushDirtyPageForReplacement, LOG, sizes.length - 1);
- // TODO IGNITE-24548 Use this initialization code.
- // new PagesWriteThrottle(
- // this,
- // checkpointProgressSupplier,
- // checkpointTimeoutLock::checkpointLockIsHeldByThread,
- // false
- // );
- writeThrottle = null;
+ this.writeThrottle = null;
+ }
+
+ /**
+ * Temporary method to enable throttling in tests.
+ *
+ * @param writeThrottle Page write throttling instance.
+ */
+ // TODO IGNITE-24548 Make a proper implementation.
+ @TestOnly
+ public void initThrottling(PagesWriteThrottlePolicy writeThrottle) {
+ this.writeThrottle = writeThrottle;
}
/** {@inheritDoc} */
diff --git a/modules/page-memory/src/main/java/org/apache/ignite/internal/pagememory/persistence/checkpoint/CheckpointTimeoutLock.java b/modules/page-memory/src/main/java/org/apache/ignite/internal/pagememory/persistence/checkpoint/CheckpointTimeoutLock.java
index a2d7cdf5bb4..58c06b73374 100644
--- a/modules/page-memory/src/main/java/org/apache/ignite/internal/pagememory/persistence/checkpoint/CheckpointTimeoutLock.java
+++ b/modules/page-memory/src/main/java/org/apache/ignite/internal/pagememory/persistence/checkpoint/CheckpointTimeoutLock.java
@@ -26,6 +26,7 @@ import static org.apache.ignite.lang.ErrorGroups.CriticalWorkers.SYSTEM_CRITICAL
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeoutException;
import java.util.function.Supplier;
import org.apache.ignite.internal.failure.FailureContext;
import org.apache.ignite.internal.failure.FailureManager;
@@ -165,23 +166,30 @@ public class CheckpointTimeoutLock {
if (urgency != CheckpointUrgency.MUST_TRIGGER) {
// Allow to take the checkpoint read lock, if urgency is not "must trigger". We optimistically assume that
- // triggerred checkpoint will start soon, without us having to explicitly wait for it and without page memory
+ // triggered checkpoint will start soon, without us having to explicitly wait for it and without page memory
// overflow.
return;
}
checkpointReadWriteLock.readUnlock();
- if (timeout > 0 && coarseCurrentTimeMillis() - start >= timeout) {
+ long elapsed = coarseCurrentTimeMillis() - start;
+ if (timeout > 0 && elapsed >= timeout) {
failCheckpointReadLock();
}
try {
- getUninterruptibly(checkpoint.futureFor(LOCK_RELEASED));
+ if (timeout > 0) {
+ getUninterruptibly(checkpoint.futureFor(LOCK_RELEASED), timeout - elapsed);
+ } else {
+ getUninterruptibly(checkpoint.futureFor(LOCK_RELEASED));
+ }
} catch (ExecutionException e) {
throw new IgniteInternalException("Failed to wait for checkpoint begin", e.getCause());
} catch (CancellationException e) {
throw new IgniteInternalException("Failed to wait for checkpoint begin", e);
+ } catch (TimeoutException e) {
+ failCheckpointReadLock();
}
}
} catch (CheckpointReadLockTimeoutException e) {
diff --git a/modules/page-memory/src/main/java/org/apache/ignite/internal/pagememory/persistence/store/AbstractFilePageStoreIo.java b/modules/page-memory/src/main/java/org/apache/ignite/internal/pagememory/persistence/store/AbstractFilePageStoreIo.java
index 0f1ca9de55f..6a4eb3df00e 100644
--- a/modules/page-memory/src/main/java/org/apache/ignite/internal/pagememory/persistence/store/AbstractFilePageStoreIo.java
+++ b/modules/page-memory/src/main/java/org/apache/ignite/internal/pagememory/persistence/store/AbstractFilePageStoreIo.java
@@ -504,6 +504,8 @@ public abstract class AbstractFilePageStoreIo implements Closeable {
int curCrc32 = FastCrc.calcCrc(pageBuf, pageSize());
if ((savedCrc32 ^ curCrc32) != 0) {
+ pageBuf.rewind();
+
throw new IgniteInternalDataIntegrityViolationException("Failed to read page (CRC validation failed) "
+ "[id=" + hexLong(pageId) + ", off=" + pageOff
+ ", filePath=" + filePath + ", fileSize=" + fileIo.size()
diff --git a/modules/page-memory/src/main/java/org/apache/ignite/internal/pagememory/persistence/throttling/PagesWriteThrottlePolicy.java b/modules/page-memory/src/main/java/org/apache/ignite/internal/pagememory/persistence/throttling/PagesWriteThrottlePolicy.java
index 201ab85ad99..85cbfb888fb 100644
--- a/modules/page-memory/src/main/java/org/apache/ignite/internal/pagememory/persistence/throttling/PagesWriteThrottlePolicy.java
+++ b/modules/page-memory/src/main/java/org/apache/ignite/internal/pagememory/persistence/throttling/PagesWriteThrottlePolicy.java
@@ -17,6 +17,8 @@
package org.apache.ignite.internal.pagememory.persistence.throttling;
+import java.util.concurrent.TimeUnit;
+
/**
* Throttling policy, encapsulates logic of delaying write operations.
*
@@ -60,7 +62,7 @@ package org.apache.ignite.internal.pagememory.persistence.throttling;
public interface PagesWriteThrottlePolicy {
// TODO Maybe make it configurable in IGNITE-24548
/** Min park time which triggers logging. */
- long LOGGING_THRESHOLD = 10;
+ long LOGGING_THRESHOLD = TimeUnit.SECONDS.toNanos(1);
/** Checkpoint buffer fullfill upper bound. */
float CP_BUF_FILL_THRESHOLD = 2f / 3;
diff --git a/modules/page-memory/src/main/java/org/apache/ignite/internal/pagememory/persistence/throttling/PagesWriteThrottle.java b/modules/page-memory/src/main/java/org/apache/ignite/internal/pagememory/persistence/throttling/TargetRatioPagesWriteThrottle.java
similarity index 88%
rename from modules/page-memory/src/main/java/org/apache/ignite/internal/pagememory/persistence/throttling/PagesWriteThrottle.java
rename to modules/page-memory/src/main/java/org/apache/ignite/internal/pagememory/persistence/throttling/TargetRatioPagesWriteThrottle.java
index ba35871aefa..f2421bf7085 100644
--- a/modules/page-memory/src/main/java/org/apache/ignite/internal/pagememory/persistence/throttling/PagesWriteThrottle.java
+++ b/modules/page-memory/src/main/java/org/apache/ignite/internal/pagememory/persistence/throttling/TargetRatioPagesWriteThrottle.java
@@ -30,9 +30,9 @@ import org.apache.ignite.internal.pagememory.persistence.checkpoint.CheckpointPr
* Throttles threads that generate dirty pages during ongoing checkpoint.
* Designed to avoid zero dropdowns that can happen if checkpoint buffer is overflowed.
*/
-public class PagesWriteThrottle implements PagesWriteThrottlePolicy {
+public class TargetRatioPagesWriteThrottle implements PagesWriteThrottlePolicy {
/** Logger. */
- private static final IgniteLogger LOG = Loggers.forClass(PagesWriteThrottle.class);
+ private static final IgniteLogger LOG = Loggers.forClass(TargetRatioPagesWriteThrottle.class);
/** Page memory. */
private final PersistentPageMemory pageMemory;
@@ -40,9 +40,6 @@ public class PagesWriteThrottle implements PagesWriteThrottlePolicy {
/** Database manager. */
private final Supplier<CheckpointProgress> cpProgress;
- /** If true, throttle will only protect from checkpoint buffer overflow, not from dirty pages ratio cap excess. */
- private final boolean throttleOnlyPagesInCheckpoint;
-
/** Checkpoint lock state checker. */
private final CheckpointLockStateChecker stateChecker;
@@ -66,21 +63,18 @@ public class PagesWriteThrottle implements PagesWriteThrottlePolicy {
* @param pageMemory Page memory.
* @param cpProgress Database manager.
* @param stateChecker checkpoint lock state checker.
- * @param throttleOnlyPagesInCheckpoint If {@code true}, throttle will only protect from checkpoint buffer overflow.
*/
- public PagesWriteThrottle(
+ public TargetRatioPagesWriteThrottle(
PersistentPageMemory pageMemory,
Supplier<CheckpointProgress> cpProgress,
- CheckpointLockStateChecker stateChecker,
- boolean throttleOnlyPagesInCheckpoint
+ CheckpointLockStateChecker stateChecker
) {
this.pageMemory = pageMemory;
this.cpProgress = cpProgress;
this.stateChecker = stateChecker;
- this.throttleOnlyPagesInCheckpoint = throttleOnlyPagesInCheckpoint;
cpBufferWatchdog = new CheckpointBufferOverflowWatchdog(pageMemory);
- assert throttleOnlyPagesInCheckpoint || cpProgress != null
+ assert cpProgress != null
: "cpProgress must be not null if ratio based throttling mode is used";
}
@@ -93,7 +87,7 @@ public class PagesWriteThrottle implements PagesWriteThrottlePolicy {
shouldThrottle = isCpBufferOverflowThresholdExceeded();
}
- if (!shouldThrottle && !throttleOnlyPagesInCheckpoint) {
+ if (!shouldThrottle) {
CheckpointProgress progress = cpProgress.get();
if (progress == null) {
diff --git a/modules/page-memory/src/test/java/org/apache/ignite/internal/pagememory/persistence/checkpoint/PageMemoryThrottlingTest.java b/modules/page-memory/src/test/java/org/apache/ignite/internal/pagememory/persistence/checkpoint/PageMemoryThrottlingTest.java
new file mode 100644
index 00000000000..3d294d2a415
--- /dev/null
+++ b/modules/page-memory/src/test/java/org/apache/ignite/internal/pagememory/persistence/checkpoint/PageMemoryThrottlingTest.java
@@ -0,0 +1,430 @@
+/*
+ * 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.pagememory.persistence.checkpoint;
+
+import static org.apache.ignite.internal.configuration.ConfigurationTestUtils.fixConfiguration;
+import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.fail;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.clearInvocations;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.file.OpenOption;
+import java.nio.file.Path;
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.stream.IntStream;
+import org.apache.ignite.internal.configuration.testframework.ConfigurationExtension;
+import org.apache.ignite.internal.configuration.testframework.InjectConfiguration;
+import org.apache.ignite.internal.failure.FailureManager;
+import org.apache.ignite.internal.fileio.AsyncFileIoFactory;
+import org.apache.ignite.internal.fileio.FileIoFactory;
+import org.apache.ignite.internal.lang.IgniteInternalCheckedException;
+import org.apache.ignite.internal.lang.RunnableX;
+import org.apache.ignite.internal.pagememory.DataRegion;
+import org.apache.ignite.internal.pagememory.PageIdAllocator;
+import org.apache.ignite.internal.pagememory.configuration.schema.PageMemoryCheckpointConfiguration;
+import org.apache.ignite.internal.pagememory.configuration.schema.PersistentPageMemoryProfileConfiguration;
+import org.apache.ignite.internal.pagememory.freelist.io.PagesListNodeIo;
+import org.apache.ignite.internal.pagememory.io.PageIoRegistry;
+import org.apache.ignite.internal.pagememory.persistence.FakePartitionMeta.FakePartitionMetaFactory;
+import org.apache.ignite.internal.pagememory.persistence.GroupPartitionId;
+import org.apache.ignite.internal.pagememory.persistence.PartitionMetaManager;
+import org.apache.ignite.internal.pagememory.persistence.PersistentPageMemory;
+import org.apache.ignite.internal.pagememory.persistence.store.FilePageStore;
+import org.apache.ignite.internal.pagememory.persistence.store.FilePageStoreManager;
+import org.apache.ignite.internal.pagememory.persistence.throttling.PagesWriteThrottlePolicy;
+import org.apache.ignite.internal.pagememory.persistence.throttling.TargetRatioPagesWriteThrottle;
+import org.apache.ignite.internal.storage.configurations.StorageProfileConfiguration;
+import org.apache.ignite.internal.testframework.BaseIgniteAbstractTest;
+import org.apache.ignite.internal.testframework.WorkDirectory;
+import org.apache.ignite.internal.testframework.WorkDirectoryExtension;
+import org.apache.ignite.internal.util.Constants;
+import org.apache.ignite.internal.util.IgniteUtils;
+import org.apache.ignite.internal.util.OffheapReadWriteLock;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+/**
+ * Tests {@link PersistentPageMemory} and {@link PagesWriteThrottlePolicy} interactions.
+ */
+@ExtendWith(ConfigurationExtension.class)
+@ExtendWith(WorkDirectoryExtension.class)
+public class PageMemoryThrottlingTest extends BaseIgniteAbstractTest {
+ private static final int PAGE_SIZE = 4096;
+
+ private static final int SEGMENT_SIZE = 128 * Constants.MiB;
+
+ private static final int CHECKPOINT_BUFFER_SIZE = 16 * Constants.MiB;
+
+ private static final int GROUP_ID = 1;
+
+ private static final int PART_ID = 1;
+
+ private static PageIoRegistry ioRegistry;
+
+ @WorkDirectory
+ private Path workDir;
+
+ // We use very small readLock timeout here on purpose.
+ @InjectConfiguration("mock {checkpointThreads = 1, readLockTimeout = 50}")
+ private PageMemoryCheckpointConfiguration checkpointConfig;
+
+ @InjectConfiguration("mock.engine = aipersist")
+ private StorageProfileConfiguration profileConfig;
+
+ private FilePageStoreManager pageStoreManager;
+
+ private CheckpointManager checkpointManager;
+
+ private PersistentPageMemory pageMemory;
+
+ private FileIoFactory fileIoFactory;
+
+ @BeforeAll
+ static void beforeAll() {
+ ioRegistry = new PageIoRegistry();
+
+ ioRegistry.loadFromServiceLoader();
+ }
+
+ @AfterAll
+ static void afterAll() {
+ ioRegistry = null;
+ }
+
+ @BeforeEach
+ void setUp() throws Exception {
+ FailureManager failureManager = mock(FailureManager.class);
+ when(failureManager.process(any())).thenThrow(new AssertionError("Unexpected error"));
+
+ fileIoFactory = spy(new AsyncFileIoFactory());
+ pageStoreManager = new FilePageStoreManager("test", workDir, fileIoFactory, PAGE_SIZE, failureManager);
+
+ List<DataRegion<PersistentPageMemory>> dataRegions = new CopyOnWriteArrayList<>();
+
+ PartitionMetaManager partitionMetaManager = new PartitionMetaManager(ioRegistry, PAGE_SIZE, new FakePartitionMetaFactory());
+
+ checkpointManager = new CheckpointManager(
+ "test",
+ null,
+ failureManager,
+ checkpointConfig,
+ pageStoreManager,
+ partitionMetaManager,
+ dataRegions,
+ ioRegistry,
+ () -> {},
+ PAGE_SIZE
+ );
+
+ var aiPersistProfileConfig = (PersistentPageMemoryProfileConfiguration) fixConfiguration(profileConfig);
+
+ pageMemory = new PersistentPageMemory(
+ aiPersistProfileConfig,
+ ioRegistry,
+ new long[]{SEGMENT_SIZE},
+ CHECKPOINT_BUFFER_SIZE,
+ pageStoreManager,
+ checkpointManager::writePageToDeltaFilePageStore,
+ checkpointManager.checkpointTimeoutLock(),
+ PAGE_SIZE,
+ new OffheapReadWriteLock(2)
+ );
+
+ pageStoreManager.start();
+ pageMemory.start();
+
+ dataRegions.add(() -> pageMemory);
+
+ checkpointManager.start();
+
+ GroupPartitionId groupPartitionId = new GroupPartitionId(GROUP_ID, PART_ID);
+
+ FilePageStore filePageStore = pageStoreManager.readOrCreateStore(groupPartitionId, ByteBuffer.allocate(PAGE_SIZE));
+ filePageStore.ensure();
+
+ pageStoreManager.addStore(groupPartitionId, filePageStore);
+ partitionMetaManager.addMeta(groupPartitionId, partitionMetaManager.readOrCreateMeta(
+ null,
+ groupPartitionId,
+ filePageStore,
+ ByteBuffer.allocateDirect(PAGE_SIZE).order(ByteOrder.LITTLE_ENDIAN)
+ ));
+ }
+
+ @AfterEach
+ void tearDown() throws Exception {
+ IgniteUtils.closeAll(
+ checkpointManager == null ? null : checkpointManager::stop,
+ pageMemory == null ? null : () -> pageMemory.stop(true),
+ pageStoreManager == null ? null : pageStoreManager::stop
+ );
+ }
+
+ /**
+ * Tests that page allocation results in {@link PagesWriteThrottlePolicy#onMarkDirty(boolean)} call.
+ */
+ @Test
+ void pageAllocationNotifiedThrottler() throws Exception {
+ PagesWriteThrottlePolicy writeThrottle = mock(PagesWriteThrottlePolicy.class);
+
+ pageMemory.initThrottling(writeThrottle);
+
+ checkpointManager.checkpointTimeoutLock().checkpointReadLock();
+
+ try {
+ pageMemory.allocatePageNoReuse(GROUP_ID, PART_ID, PageIdAllocator.FLAG_AUX);
+
+ verify(writeThrottle).onMarkDirty(eq(false));
+ } finally {
+ checkpointManager.checkpointTimeoutLock().checkpointReadUnlock();
+ }
+ }
+
+ /**
+ * Tests that page write lock usage without page modification does not result in {@link PagesWriteThrottlePolicy#onMarkDirty(boolean)}.
+ */
+ @Test
+ void pageUnlockWithoutMarkingDirty() throws Exception {
+ PagesWriteThrottlePolicy writeThrottle = mock(PagesWriteThrottlePolicy.class);
+
+ pageMemory.initThrottling(writeThrottle);
+
+ long pageId;
+
+ checkpointManager.checkpointTimeoutLock().checkpointReadLock();
+ try {
+ pageId = pageMemory.allocatePageNoReuse(GROUP_ID, PART_ID, PageIdAllocator.FLAG_AUX);
+
+ initPage(pageId);
+ } finally {
+ checkpointManager.checkpointTimeoutLock().checkpointReadUnlock();
+ }
+
+ CheckpointProgress checkpointFuture = checkpointManager.forceCheckpoint("must clean the dirty page");
+ assertThat(checkpointFuture.futureFor(CheckpointState.FINISHED), willCompleteSuccessfully());
+
+ checkpointManager.checkpointTimeoutLock().checkpointReadLock();
+ try {
+ clearInvocations(writeThrottle);
+
+ acquireAndReleaseWriteLock(pageId, false);
+
+ verify(writeThrottle, times(0)).onMarkDirty(anyBoolean());
+ } finally {
+ checkpointManager.checkpointTimeoutLock().checkpointReadUnlock();
+ }
+ }
+
+ /**
+ * Tests that two consecutive page updates lead to a single {@link PagesWriteThrottlePolicy#onMarkDirty(boolean)} call.
+ */
+ @Test
+ void pageMarkedDirtyOnlyOnce() {
+ PagesWriteThrottlePolicy writeThrottle = mock(PagesWriteThrottlePolicy.class);
+
+ pageMemory.initThrottling(writeThrottle);
+
+ AtomicLong pageId = new AtomicLong();
+
+ runInLock(() -> {
+ pageId.set(pageMemory.allocatePageNoReuse(GROUP_ID, PART_ID, PageIdAllocator.FLAG_AUX));
+
+ initPage(pageId.get());
+ });
+
+ CheckpointProgress checkpointFuture = checkpointManager.forceCheckpoint("must clean the dirty page");
+ assertThat(checkpointFuture.futureFor(CheckpointState.FINISHED), willCompleteSuccessfully());
+
+ runInLock(() -> {
+ clearInvocations(writeThrottle);
+
+ acquireAndReleaseWriteLock(pageId.get(), true);
+ acquireAndReleaseWriteLock(pageId.get(), true);
+
+ verify(writeThrottle).onMarkDirty(anyBoolean());
+ });
+ }
+
+ /**
+ * Tests that checkpoint events are properly propagated to the throttler.
+ */
+ @Test
+ void checkpointEvents() {
+ PagesWriteThrottlePolicy writeThrottle = mock(PagesWriteThrottlePolicy.class);
+
+ pageMemory.initThrottling(writeThrottle);
+
+ AtomicLong pageId = new AtomicLong();
+
+ runInLock(() -> {
+ pageId.set(pageMemory.allocatePageNoReuse(GROUP_ID, PART_ID, PageIdAllocator.FLAG_AUX));
+
+ initPage(pageId.get());
+ });
+
+ CheckpointProgress checkpointFuture = checkpointManager.forceCheckpoint("must clean the dirty page");
+ assertThat(checkpointFuture.futureFor(CheckpointState.FINISHED), willCompleteSuccessfully());
+
+ verify(writeThrottle).onBeginCheckpoint();
+ verify(writeThrottle).onFinishCheckpoint();
+ }
+
+ /**
+ * Tests that page memory wakes up throttled thread while writing checkpoint buffer pages.
+ *
+ * @throws Exception If failed.
+ */
+ @Test
+ void wakeupThrottledThreads() throws Exception {
+ PagesWriteThrottlePolicy writeThrottle = mock(PagesWriteThrottlePolicy.class);
+ pageMemory.initThrottling(writeThrottle);
+
+ int pages = CHECKPOINT_BUFFER_SIZE / PAGE_SIZE * 9 / 10;
+ long[] pageIds = new long[pages];
+
+ runInLock(() -> {
+ for (int i = 0; i < pages; i++) {
+ long pageId = pageMemory.allocatePageNoReuse(GROUP_ID, PART_ID, PageIdAllocator.FLAG_AUX);
+
+ pageIds[i] = pageId;
+
+ initPage(pageId);
+ }
+ });
+
+ CountDownLatch cdl = new CountDownLatch(1);
+
+ doAnswer(invocation -> {
+ cdl.await();
+
+ return invocation.callRealMethod();
+ }).when(fileIoFactory).create(any(), any(OpenOption[].class));
+
+ CheckpointProgress checkpointFuture = checkpointManager.forceCheckpoint("reset dirty pages set in test");
+ assertThat(checkpointFuture.futureFor(CheckpointState.LOCK_TAKEN), willCompleteSuccessfully());
+
+ runInLock(() -> {
+ for (int i = 0; i < pages; i++) {
+ acquireAndReleaseWriteLock(pageIds[i], true);
+ }
+ });
+
+ cdl.countDown();
+
+ assertThat(checkpointFuture.futureFor(CheckpointState.FINISHED), willCompleteSuccessfully());
+
+ verify(writeThrottle).wakeupThrottledThreads();
+ }
+
+ /**
+ * Tests that there's no checkpoint read lock timeouts during the high load.
+ */
+ @Test
+ void hugeLoadDoesNotBreakCheckpointReadLock() {
+ pageMemory.initThrottling(new TargetRatioPagesWriteThrottle(
+ pageMemory,
+ checkpointManager::currentCheckpointProgress,
+ checkpointManager.checkpointTimeoutLock()::checkpointLockIsHeldByThread
+ ));
+
+ long[] pageIds = new long[SEGMENT_SIZE / PAGE_SIZE * 2];
+
+ // Unlike regular "for" loop, "forEach" makes "i" effectively final.
+ IntStream.range(0, SEGMENT_SIZE / PAGE_SIZE * 2).forEach(i -> runInLock(() -> {
+ long pageId = pageMemory.allocatePageNoReuse(GROUP_ID, PART_ID, PageIdAllocator.FLAG_AUX);
+
+ pageIds[i] = pageId;
+
+ initPage(pageId);
+ }));
+
+ for (int i = 0; i < pageIds.length * 10; i++) {
+ runInLock(() -> {
+ long pageId = pageIds[ThreadLocalRandom.current().nextInt(pageIds.length)];
+
+ acquireAndReleaseWriteLock(pageId, true);
+ });
+ }
+ }
+
+ private void runInLock(RunnableX runnable) {
+ checkpointManager.checkpointTimeoutLock().checkpointReadLock();
+
+ try {
+ runnable.run();
+ } catch (AssertionError e) {
+ throw e;
+ } catch (Throwable e) {
+ fail(e);
+ } finally {
+ checkpointManager.checkpointTimeoutLock().checkpointReadUnlock();
+ }
+ }
+
+ private void initPage(long pageId) throws IgniteInternalCheckedException {
+ long page = pageMemory.acquirePage(GROUP_ID, pageId);
+
+ try {
+ long pageAddr = pageMemory.writeLock(GROUP_ID, pageId, page, true);
+
+ try {
+ PagesListNodeIo.VERSIONS.latest().initNewPage(pageAddr, pageId, PAGE_SIZE);
+ } finally {
+ pageMemory.writeUnlock(GROUP_ID, pageId, page, true);
+ }
+ } finally {
+ pageMemory.releasePage(GROUP_ID, pageId, page);
+ }
+ }
+
+ private void acquireAndReleaseWriteLock(long pageId, boolean markDirty) throws IgniteInternalCheckedException {
+ long page = pageMemory.acquirePage(GROUP_ID, pageId);
+
+ try {
+ long pageAddr = pageMemory.writeLock(GROUP_ID, pageId, page);
+
+ try {
+ assertNotEquals(0L, pageAddr);
+ } finally {
+ pageMemory.writeUnlock(GROUP_ID, pageId, page, markDirty);
+ }
+ } finally {
+ pageMemory.releasePage(GROUP_ID, pageId, page);
+ }
+ }
+}
diff --git a/modules/page-memory/src/test/java/org/apache/ignite/internal/pagememory/persistence/replacement/AbstractPageReplacementTest.java b/modules/page-memory/src/test/java/org/apache/ignite/internal/pagememory/persistence/replacement/AbstractPageReplacementTest.java
index fb97478d408..53ea18712c9 100644
--- a/modules/page-memory/src/test/java/org/apache/ignite/internal/pagememory/persistence/replacement/AbstractPageReplacementTest.java
+++ b/modules/page-memory/src/test/java/org/apache/ignite/internal/pagememory/persistence/replacement/AbstractPageReplacementTest.java
@@ -164,7 +164,6 @@ public abstract class AbstractPageReplacementTest extends IgniteAbstractTest {
filePageStoreManager,
checkpointManager::writePageToDeltaFilePageStore,
checkpointManager.checkpointTimeoutLock(),
- () -> null,
PAGE_SIZE,
new OffheapReadWriteLock(OffheapReadWriteLock.DEFAULT_CONCURRENCY_LEVEL)
);
diff --git a/modules/storage-page-memory/src/main/java/org/apache/ignite/internal/storage/pagememory/PersistentPageMemoryDataRegion.java b/modules/storage-page-memory/src/main/java/org/apache/ignite/internal/storage/pagememory/PersistentPageMemoryDataRegion.java
index 1fd0d2bda76..9a6dda287c6 100644
--- a/modules/storage-page-memory/src/main/java/org/apache/ignite/internal/storage/pagememory/PersistentPageMemoryDataRegion.java
+++ b/modules/storage-page-memory/src/main/java/org/apache/ignite/internal/storage/pagememory/PersistentPageMemoryDataRegion.java
@@ -22,7 +22,6 @@ import static org.apache.ignite.internal.util.Constants.MiB;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicLong;
-import java.util.function.Supplier;
import org.apache.ignite.internal.pagememory.DataRegion;
import org.apache.ignite.internal.pagememory.configuration.schema.PersistentPageMemoryProfileConfiguration;
import org.apache.ignite.internal.pagememory.configuration.schema.PersistentPageMemoryProfileView;
@@ -30,7 +29,6 @@ import org.apache.ignite.internal.pagememory.io.PageIoRegistry;
import org.apache.ignite.internal.pagememory.persistence.PartitionMetaManager;
import org.apache.ignite.internal.pagememory.persistence.PersistentPageMemory;
import org.apache.ignite.internal.pagememory.persistence.checkpoint.CheckpointManager;
-import org.apache.ignite.internal.pagememory.persistence.checkpoint.CheckpointProgress;
import org.apache.ignite.internal.pagememory.persistence.store.FilePageStoreManager;
import org.apache.ignite.internal.storage.StorageException;
import org.apache.ignite.internal.util.OffheapReadWriteLock;
@@ -103,8 +101,6 @@ class PersistentPageMemoryDataRegion implements DataRegion<PersistentPageMemory>
public void start() {
PersistentPageMemoryProfileView dataRegionConfigView = (PersistentPageMemoryProfileView) cfg.value();
- Supplier<CheckpointProgress> currentCheckpointProgress = checkpointManager::currentCheckpointProgress;
-
PersistentPageMemory pageMemory = new PersistentPageMemory(
cfg,
ioRegistry,
@@ -113,7 +109,6 @@ class PersistentPageMemoryDataRegion implements DataRegion<PersistentPageMemory>
filePageStoreManager,
checkpointManager::writePageToDeltaFilePageStore,
checkpointManager.checkpointTimeoutLock(),
- currentCheckpointProgress,
pageSize,
new OffheapReadWriteLock(OffheapReadWriteLock.DEFAULT_CONCURRENCY_LEVEL)
);
diff --git a/modules/storage-page-memory/src/test/java/org/apache/ignite/internal/storage/pagememory/PersistentPageMemoryNoLoadTest.java b/modules/storage-page-memory/src/test/java/org/apache/ignite/internal/storage/pagememory/PersistentPageMemoryNoLoadTest.java
index 05217ecdaba..0b014b48278 100644
--- a/modules/storage-page-memory/src/test/java/org/apache/ignite/internal/storage/pagememory/PersistentPageMemoryNoLoadTest.java
+++ b/modules/storage-page-memory/src/test/java/org/apache/ignite/internal/storage/pagememory/PersistentPageMemoryNoLoadTest.java
@@ -465,7 +465,6 @@ public class PersistentPageMemoryNoLoadTest extends AbstractPageMemoryNoLoadSelf
filePageStoreManager == null ? new TestPageReadWriteManager() : filePageStoreManager,
flushDirtyPageForReplacement,
checkpointManager == null ? mockCheckpointTimeoutLock(true) : checkpointManager.checkpointTimeoutLock(),
- checkpointManager == null ? () -> null : checkpointManager::currentCheckpointProgress,
PAGE_SIZE,
new OffheapReadWriteLock(OffheapReadWriteLock.DEFAULT_CONCURRENCY_LEVEL)
);