nizhikov commented on a change in pull request #8928:
URL: https://github.com/apache/ignite/pull/8928#discussion_r612154513



##########
File path: 
modules/core/src/test/java/org/apache/ignite/internal/processors/performancestatistics/CheckpointTest.java
##########
@@ -0,0 +1,273 @@
+/*
+ * 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.performancestatistics;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.file.OpenOption;
+import java.util.UUID;
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.locks.LockSupport;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.cluster.ClusterState;
+import org.apache.ignite.configuration.DataRegionConfiguration;
+import org.apache.ignite.configuration.DataStorageConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.IgniteInternalFuture;
+import org.apache.ignite.internal.processors.cache.persistence.file.FileIO;
+import 
org.apache.ignite.internal.processors.cache.persistence.file.FileIODecorator;
+import 
org.apache.ignite.internal.processors.cache.persistence.file.FileIOFactory;
+import 
org.apache.ignite.internal.processors.cache.persistence.file.RandomAccessFileIOFactory;
+import org.apache.ignite.internal.processors.metric.MetricRegistry;
+import org.apache.ignite.internal.processors.metric.impl.AtomicLongMetric;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.apache.ignite.testframework.junits.WithSystemProperty;
+import org.junit.Test;
+
+import static 
org.apache.ignite.IgniteSystemProperties.IGNITE_OVERRIDE_WRITE_THROTTLING_ENABLED;
+import static 
org.apache.ignite.internal.processors.cache.persistence.DataStorageMetricsImpl.DATASTORAGE_METRIC_PREFIX;
+import static org.apache.ignite.testframework.GridTestUtils.waitForCondition;
+
+/**
+ * Tests checkpoint performance statistics.
+ */
+public class CheckpointTest extends AbstractPerformanceStatisticsTest {
+    /** Slow checkpoint enabled. */
+    private static final AtomicBoolean slowCheckpointEnabled = new 
AtomicBoolean(false);
+
+    /** {@inheritDoc} */
+    @Override protected IgniteConfiguration getConfiguration(String 
igniteInstanceName) throws Exception {
+        IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
+
+        cfg.setCacheConfiguration(defaultCacheConfiguration());
+        cfg.setDataStorageConfiguration(new DataStorageConfiguration()
+            .setMetricsEnabled(true)
+            .setDefaultDataRegionConfiguration(new DataRegionConfiguration()
+                .setMaxSize(10 * 1024 * 1024)
+                .setCheckpointPageBufferSize(1024 * 1024)
+                .setMetricsEnabled(true)
+                .setPersistenceEnabled(true))
+            .setWriteThrottlingEnabled(true)
+            .setFileIOFactory(new SlowCheckpointFileIOFactory())
+            .setCheckpointThreads(1));
+
+        return cfg;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void beforeTestsStarted() throws Exception {
+        super.beforeTestsStarted();
+
+        cleanPersistenceDir();
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void afterTest() throws Exception {
+        super.afterTest();
+
+        slowCheckpointEnabled.set(false);
+
+        stopAllGrids();
+
+        cleanPersistenceDir();
+    }
+
+    /** @throws Exception If failed. */
+    @Test
+    public void testCheckpoint() throws Exception {
+        IgniteEx srv = startGrid();
+
+        srv.cluster().state(ClusterState.ACTIVE);
+
+        MetricRegistry mreg = 
srv.context().metric().registry(DATASTORAGE_METRIC_PREFIX);
+
+        AtomicLongMetric lastBeforeLockDuration = 
mreg.findMetric("LastCheckpointBeforeLockDuration");
+        AtomicLongMetric lastLockWaitDuration = 
mreg.findMetric("LastCheckpointLockWaitDuration");
+        AtomicLongMetric lastListenersExecDuration = 
mreg.findMetric("LastCheckpointListenersExecuteDuration");
+        AtomicLongMetric lastMarcDuration = 
mreg.findMetric("LastCheckpointMarkDuration");
+        AtomicLongMetric lastLockHoldDuration = 
mreg.findMetric("LastCheckpointLockHoldDuration");
+        AtomicLongMetric lastPagesWriteDuration = 
mreg.findMetric("LastCheckpointPagesWriteDuration");
+        AtomicLongMetric lastFsyncDuration = 
mreg.findMetric("LastCheckpointFsyncDuration");
+        AtomicLongMetric lastWalRecordFsyncDuration = 
mreg.findMetric("LastCheckpointWalRecordFsyncDuration");
+        AtomicLongMetric lastWriteEntryDuration = 
mreg.findMetric("LastCheckpointWriteEntryDuration");
+        AtomicLongMetric lastSplitAndSortPagesDuration =
+            mreg.findMetric("LastCheckpointSplitAndSortPagesDuration");
+        AtomicLongMetric lastDuration = 
mreg.findMetric("LastCheckpointDuration");
+        AtomicLongMetric lastStart = mreg.findMetric("LastCheckpointStart");
+        AtomicLongMetric lastTotalPages = 
mreg.findMetric("LastCheckpointTotalPagesNumber");
+        AtomicLongMetric lastDataPages = 
mreg.findMetric("LastCheckpointDataPagesNumber");
+        AtomicLongMetric lastCOWPages = 
mreg.findMetric("LastCheckpointCopiedOnWritePagesNumber");
+
+        // wait for checkpoint to finish on node start
+        assertTrue(waitForCondition(() -> 0 < lastStart.value(), TIMEOUT));
+
+        startCollectStatisticsWithImmediatelyFlush();
+
+        forceCheckpoint();
+
+        assertTrue(waitForCondition(() -> {
+            try {
+                AtomicInteger cnt = new AtomicInteger();
+
+                readFiles(statisticsFiles(), new TestHandler() {
+                    @Override public void checkpoint(UUID nodeId, long 
beforeLockDuration, long lockWaitDuration,
+                        long listenersExecDuration, long markDuration, long 
lockHoldDuration, long pagesWriteDuration,
+                        long fsyncDuration, long walCpRecordFsyncDuration, 
long writeCpEntryDuration,
+                        long splitAndSortCpPagesDuration, long totalDuration, 
long cpStartTime, int pagesSize,
+                        int dataPagesWritten, int cowPagesWritten) {
+                        assertEquals(srv.localNode().id(), nodeId);
+                        assertEquals(lastBeforeLockDuration.value(), 
beforeLockDuration);
+                        assertEquals(lastLockWaitDuration.value(), 
lockWaitDuration);
+                        assertEquals(lastListenersExecDuration.value(), 
listenersExecDuration);
+                        assertEquals(lastMarcDuration.value(), markDuration);
+                        assertEquals(lastLockHoldDuration.value(), 
lockHoldDuration);
+                        assertEquals(lastPagesWriteDuration.value(), 
pagesWriteDuration);
+                        assertEquals(lastFsyncDuration.value(), fsyncDuration);
+                        assertEquals(lastWalRecordFsyncDuration.value(), 
walCpRecordFsyncDuration);
+                        assertEquals(lastWriteEntryDuration.value(), 
writeCpEntryDuration);
+                        assertEquals(lastSplitAndSortPagesDuration.value(), 
splitAndSortCpPagesDuration);
+                        assertEquals(lastDuration.value(), totalDuration);
+                        assertEquals(lastStart.value(), cpStartTime);
+                        assertEquals(lastTotalPages.value(), pagesSize);
+                        assertEquals(lastDataPages.value(), dataPagesWritten);
+                        assertEquals(lastCOWPages.value(), cowPagesWritten);
+
+                        cnt.incrementAndGet();
+                    }
+                });
+
+                return cnt.get() == 1;
+            }
+            catch (Exception e) {
+                throw new RuntimeException(e);
+            }
+        }, TIMEOUT));
+    }
+
+    /** @throws Exception if failed. */
+    @Test
+    public void testThrottleSpeedBased() throws Exception {
+        checkThrottling();
+    }
+
+    /** @throws Exception if failed. */
+    @Test
+    @WithSystemProperty(key = IGNITE_OVERRIDE_WRITE_THROTTLING_ENABLED, value 
= "TARGET_RATIO_BASED")
+    public void testThrottleTargetRatioBased() throws Exception {
+        checkThrottling();
+    }
+
+    /** @throws Exception if failed. */
+    public void checkThrottling() throws Exception {
+        IgniteEx srv = startGrid();
+
+        srv.cluster().state(ClusterState.ACTIVE);
+
+        IgniteCache<Long, Long> cache = 
srv.getOrCreateCache(DEFAULT_CACHE_NAME);
+
+        long start = U.currentTimeMillis();
+
+        startCollectStatisticsWithImmediatelyFlush();
+
+        int keysCnt = 1024;
+
+        AtomicBoolean stop = new AtomicBoolean();
+
+        slowCheckpointEnabled.set(true);
+
+        IgniteInternalFuture<?> fut = GridTestUtils.runAsync(() -> {
+            while (!stop.get())
+                cache.put(ThreadLocalRandom.current().nextLong(keysCnt), 
ThreadLocalRandom.current().nextLong());
+        });
+
+        assertTrue(waitForCondition(() -> {
+            try {
+                AtomicInteger cnt = new AtomicInteger();
+
+                readFiles(statisticsFiles(), new TestHandler() {
+                    @Override public void pagesWriteThrottle(UUID nodeId, long 
endTime, long duration) {
+                        assertEquals(srv.localNode().id(), nodeId);
+
+                        assertTrue(start <= endTime);
+                        assertTrue(duration >= 0);
+
+                        cnt.incrementAndGet();
+                    }
+                });
+
+                return cnt.get() > 0;
+            }
+            catch (Exception e) {
+                throw new RuntimeException(e);
+            }
+        }, TIMEOUT));
+
+        stop.set(true);
+
+        fut.get(TIMEOUT);
+    }
+
+    /**
+     * Create File I/O that emulates poor checkpoint write speed.
+     */
+    private static class SlowCheckpointFileIOFactory implements FileIOFactory {
+        /** Serial version uid. */
+        private static final long serialVersionUID = 0L;
+
+        /** Default checkpoint park nanos. */
+        private static final int CHECKPOINT_PARK_NANOS = 5_000_000;

Review comment:
       This should be long.




-- 
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.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to