IGNITE-15243 Return the IGNITE_THRESHOLD_WAL_ARCHIVE_SIZE_PERCENTAGE and deprecate it (#9298)

diff --git a/modules/core/src/main/java/org/apache/ignite/IgniteSystemProperties.java b/modules/core/src/main/java/org/apache/ignite/IgniteSystemProperties.java
index 7007013..3a3a4f8 100644
--- a/modules/core/src/main/java/org/apache/ignite/IgniteSystemProperties.java
+++ b/modules/core/src/main/java/org/apache/ignite/IgniteSystemProperties.java
@@ -1357,6 +1357,17 @@
     public static final String IGNITE_CHECKPOINT_TRIGGER_ARCHIVE_SIZE_PERCENTAGE = "IGNITE_CHECKPOINT_TRIGGER_ARCHIVE_SIZE_PERCENTAGE";
 
     /**
+     * Property for setup percentage of WAL archive size to calculate
+     * threshold since which removing of old archive should be started.
+     *
+     * @deprecated Use {@link DataStorageConfiguration#setMinWalArchiveSize}.
+     */
+    @SystemProperty(value = "Property for setup percentage of WAL archive size to calculate threshold since which " +
+        "removing of old archive should be started", type = Double.class)
+    public static final String IGNITE_THRESHOLD_WAL_ARCHIVE_SIZE_PERCENTAGE =
+        "IGNITE_THRESHOLD_WAL_ARCHIVE_SIZE_PERCENTAGE";
+
+    /**
      * Threshold time (in millis) to print warning to log if waiting for next wal segment took longer than the threshold.
      *
      * Default value is 1000 ms.
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/IgniteCacheDatabaseSharedManager.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/IgniteCacheDatabaseSharedManager.java
index cd9ab69..a170751 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/IgniteCacheDatabaseSharedManager.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/IgniteCacheDatabaseSharedManager.java
@@ -96,6 +96,8 @@
 import static java.util.Objects.isNull;
 import static java.util.Objects.nonNull;
 import static org.apache.ignite.IgniteSystemProperties.IGNITE_REUSE_MEMORY_ON_DEACTIVATE;
+import static org.apache.ignite.IgniteSystemProperties.IGNITE_THRESHOLD_WAL_ARCHIVE_SIZE_PERCENTAGE;
+import static org.apache.ignite.IgniteSystemProperties.getDouble;
 import static org.apache.ignite.configuration.DataStorageConfiguration.DFLT_DATA_REG_DEFAULT_NAME;
 import static org.apache.ignite.configuration.DataStorageConfiguration.DFLT_PAGE_SIZE;
 import static org.apache.ignite.configuration.DataStorageConfiguration.HALF_MAX_WAL_ARCHIVE_SIZE;
@@ -646,14 +648,31 @@
 
             long min = memCfg.getMinWalArchiveSize();
 
-            if (min != HALF_MAX_WAL_ARCHIVE_SIZE && min > max) {
-                throw new IgniteCheckedException(String.format(
-                    "DataRegionConfiguration.minWalArchiveSize must be less than or equal to " +
-                        "DataRegionConfiguration.maxWalArchiveSize or equal to %d " +
-                        "(to be half of maxWalArchiveSize), current settings:" + U.nl() +
-                        "DataRegionConfiguration.minWalArchiveSize: %d bytes" + U.nl() +
-                        "DataRegionConfiguration.maxWalArchiveSize: %d bytes",
-                    HALF_MAX_WAL_ARCHIVE_SIZE, min, max));
+            double percentage = getDouble(IGNITE_THRESHOLD_WAL_ARCHIVE_SIZE_PERCENTAGE, -1);
+
+            if (min != HALF_MAX_WAL_ARCHIVE_SIZE) {
+                if (min > max) {
+                    throw new IgniteCheckedException(String.format(
+                        "DataRegionConfiguration.minWalArchiveSize must be less than or equal to " +
+                            "DataRegionConfiguration.maxWalArchiveSize or equal to %d " +
+                            "(to be half of maxWalArchiveSize), current settings:" + U.nl() +
+                            "DataRegionConfiguration.minWalArchiveSize: %d bytes" + U.nl() +
+                            "DataRegionConfiguration.maxWalArchiveSize: %d bytes",
+                        HALF_MAX_WAL_ARCHIVE_SIZE, min, max));
+                }
+            }
+            else if (percentage != -1) {
+                log.warning(String.format(
+                    "%s is deprecated, use DataRegionConfiguration.minWalArchiveSize instead",
+                    IGNITE_THRESHOLD_WAL_ARCHIVE_SIZE_PERCENTAGE
+                ));
+
+                if ((long)(max * percentage) > max) {
+                    throw new IgniteCheckedException(String.format(
+                        "%s must be less than or equal to 1.0",
+                        IGNITE_THRESHOLD_WAL_ARCHIVE_SIZE_PERCENTAGE
+                    ));
+                }
             }
         }
     }
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/wal/FileWriteAheadLogManager.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/wal/FileWriteAheadLogManager.java
index 33ca3c7..cca29c7 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/wal/FileWriteAheadLogManager.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/wal/FileWriteAheadLogManager.java
@@ -134,9 +134,11 @@
 import static java.util.stream.Collectors.toList;
 import static org.apache.ignite.IgniteSystemProperties.IGNITE_CHECKPOINT_TRIGGER_ARCHIVE_SIZE_PERCENTAGE;
 import static org.apache.ignite.IgniteSystemProperties.IGNITE_THRESHOLD_WAIT_TIME_NEXT_WAL_SEGMENT;
+import static org.apache.ignite.IgniteSystemProperties.IGNITE_THRESHOLD_WAL_ARCHIVE_SIZE_PERCENTAGE;
 import static org.apache.ignite.IgniteSystemProperties.IGNITE_WAL_COMPRESSOR_WORKER_THREAD_CNT;
 import static org.apache.ignite.IgniteSystemProperties.IGNITE_WAL_MMAP;
 import static org.apache.ignite.IgniteSystemProperties.IGNITE_WAL_SERIALIZER_VERSION;
+import static org.apache.ignite.IgniteSystemProperties.getDouble;
 import static org.apache.ignite.configuration.DataStorageConfiguration.HALF_MAX_WAL_ARCHIVE_SIZE;
 import static org.apache.ignite.configuration.DataStorageConfiguration.UNLIMITED_WAL_ARCHIVE;
 import static org.apache.ignite.events.EventType.EVT_WAL_SEGMENT_ARCHIVED;
@@ -427,7 +429,7 @@
 
         fileHandleManagerFactory = new FileHandleManagerFactory(dsCfg);
 
-        double cpTriggerArchiveSizePercentage = IgniteSystemProperties.getDouble(
+        double cpTriggerArchiveSizePercentage = getDouble(
             IGNITE_CHECKPOINT_TRIGGER_ARCHIVE_SIZE_PERCENTAGE, DFLT_CHECKPOINT_TRIGGER_ARCHIVE_SIZE_PERCENTAGE);
 
         maxSegCountWithoutCheckpoint = (long)((U.adjustedWalHistorySize(dsCfg, log) * cpTriggerArchiveSizePercentage)
@@ -3569,6 +3571,9 @@
         long max = dsCfg.getMaxWalArchiveSize();
         long min = dsCfg.getMinWalArchiveSize();
 
-        return max == UNLIMITED_WAL_ARCHIVE ? max : min == HALF_MAX_WAL_ARCHIVE_SIZE ? max / 2 : min;
+        double percentage = getDouble(IGNITE_THRESHOLD_WAL_ARCHIVE_SIZE_PERCENTAGE, -1);
+
+        return max == UNLIMITED_WAL_ARCHIVE ? max : min != HALF_MAX_WAL_ARCHIVE_SIZE ? min :
+            percentage == -1 ? max / 2 : (long)(max * percentage);
     }
 }
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/persistence/IgniteCacheDatabaseSharedManagerSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/persistence/IgniteCacheDatabaseSharedManagerSelfTest.java
index 074e1b8..321907a 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/persistence/IgniteCacheDatabaseSharedManagerSelfTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/persistence/IgniteCacheDatabaseSharedManagerSelfTest.java
@@ -18,12 +18,16 @@
 package org.apache.ignite.internal.processors.cache.persistence;
 
 import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.IgniteSystemProperties;
 import org.apache.ignite.configuration.DataRegionConfiguration;
 import org.apache.ignite.configuration.DataStorageConfiguration;
 import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.testframework.junits.WithSystemProperty;
 import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
 import org.junit.Test;
 
+import static java.lang.System.setProperty;
+import static org.apache.ignite.IgniteSystemProperties.IGNITE_THRESHOLD_WAL_ARCHIVE_SIZE_PERCENTAGE;
 import static org.apache.ignite.configuration.DataStorageConfiguration.DFLT_WAL_SEGMENT_SIZE;
 import static org.apache.ignite.configuration.DataStorageConfiguration.HALF_MAX_WAL_ARCHIVE_SIZE;
 import static org.apache.ignite.configuration.DataStorageConfiguration.UNLIMITED_WAL_ARCHIVE;
@@ -40,21 +44,22 @@
      * @throws Exception If failed.
      */
     @Test
+    @WithSystemProperty(key = IGNITE_THRESHOLD_WAL_ARCHIVE_SIZE_PERCENTAGE, value = "-1")
     public void testCheckMinWalArchiveSize() throws Exception {
         DataStorageConfiguration cfg = new DataStorageConfiguration()
-            .setDefaultDataRegionConfiguration(new DataRegionConfiguration().setPersistenceEnabled(true));
-
-        cfg.setMaxWalArchiveSize(UNLIMITED_WAL_ARCHIVE);
+            .setDefaultDataRegionConfiguration(new DataRegionConfiguration().setPersistenceEnabled(true))
+            .setMaxWalArchiveSize(UNLIMITED_WAL_ARCHIVE);
 
         for (long i : F.asList(10L, 100L, HALF_MAX_WAL_ARCHIVE_SIZE))
             checkWalArchiveSizeConfiguration(cfg.setMinWalArchiveSize(i), log);
 
-        cfg.setMaxWalArchiveSize(DFLT_WAL_SEGMENT_SIZE);
+        int max = DFLT_WAL_SEGMENT_SIZE;
+        cfg.setMaxWalArchiveSize(max);
 
-        for (long i : F.asList(1L, 10L, HALF_MAX_WAL_ARCHIVE_SIZE, (long)DFLT_WAL_SEGMENT_SIZE))
+        for (long i : F.asList(1L, 10L, HALF_MAX_WAL_ARCHIVE_SIZE, (long)max))
             checkWalArchiveSizeConfiguration(cfg.setMinWalArchiveSize(i), log);
 
-        for (long i : F.asList(DFLT_WAL_SEGMENT_SIZE * 2, DFLT_WAL_SEGMENT_SIZE * 3)) {
+        for (long i : F.asList(max * 2, max * 3)) {
             assertThrows(
                 log,
                 () -> {
@@ -67,4 +72,45 @@
             );
         }
     }
+
+    /**
+     * Checking the correctness of validation
+     * {@link IgniteSystemProperties#IGNITE_THRESHOLD_WAL_ARCHIVE_SIZE_PERCENTAGE}.
+     *
+     * @throws Exception If failed.
+     */
+    @Test
+    public void testCheckIgniteThresholdWalArchiveSizePercentage() throws Exception {
+        DataStorageConfiguration cfg = new DataStorageConfiguration()
+            .setDefaultDataRegionConfiguration(new DataRegionConfiguration().setPersistenceEnabled(true))
+            .setMaxWalArchiveSize(UNLIMITED_WAL_ARCHIVE)
+            .setMinWalArchiveSize(HALF_MAX_WAL_ARCHIVE_SIZE);
+
+        for (double i : F.asList(0.1d, 1d, (double)HALF_MAX_WAL_ARCHIVE_SIZE)) {
+            setProperty(IGNITE_THRESHOLD_WAL_ARCHIVE_SIZE_PERCENTAGE, Double.toString(i));
+            checkWalArchiveSizeConfiguration(cfg, log);
+        }
+
+        cfg.setMaxWalArchiveSize(DFLT_WAL_SEGMENT_SIZE);
+
+        for (double i : F.asList(0.1d, 1d)) {
+            setProperty(IGNITE_THRESHOLD_WAL_ARCHIVE_SIZE_PERCENTAGE, Double.toString(i));
+            checkWalArchiveSizeConfiguration(cfg, log);
+        }
+
+        for (double i : F.asList(2d, 3d)) {
+            setProperty(IGNITE_THRESHOLD_WAL_ARCHIVE_SIZE_PERCENTAGE, Double.toString(i));
+
+            assertThrows(
+                log,
+                () -> {
+                    checkWalArchiveSizeConfiguration(cfg, log);
+
+                    return null;
+                },
+                IgniteCheckedException.class,
+                null
+            );
+        }
+    }
 }
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/persistence/db/wal/WalArchiveSizeConfigurationTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/persistence/db/wal/WalArchiveSizeConfigurationTest.java
index fe9adfe..56c848b 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/persistence/db/wal/WalArchiveSizeConfigurationTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/persistence/db/wal/WalArchiveSizeConfigurationTest.java
@@ -21,15 +21,18 @@
 import java.util.Collections;
 import java.util.List;
 import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.IgniteSystemProperties;
 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.util.typedef.internal.U;
 import org.apache.ignite.testframework.ListeningTestLogger;
+import org.apache.ignite.testframework.junits.WithSystemProperty;
 import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
 import org.junit.Test;
 
+import static org.apache.ignite.IgniteSystemProperties.IGNITE_THRESHOLD_WAL_ARCHIVE_SIZE_PERCENTAGE;
 import static org.apache.ignite.testframework.GridTestUtils.assertThrowsAnyCause;
 import static org.apache.ignite.testframework.GridTestUtils.getFieldValue;
 
@@ -147,6 +150,24 @@
     }
 
     /**
+     * Check that if the {@link IgniteSystemProperties#IGNITE_THRESHOLD_WAL_ARCHIVE_SIZE_PERCENTAGE}
+     * are set to more than 1.0, then there will be an error when starting the node.
+     */
+    @Test
+    @WithSystemProperty(key = IGNITE_THRESHOLD_WAL_ARCHIVE_SIZE_PERCENTAGE, value = "2")
+    public void testIncorrectIgniteThresholdWalArchiveSizePercentageProperty() {
+        DataStorageConfiguration dsCfg = new DataStorageConfiguration()
+            .setDefaultDataRegionConfiguration(new DataRegionConfiguration().setPersistenceEnabled(true));
+
+        assertThrowsAnyCause(
+            log,
+            () -> startGrid(0, (IgniteConfiguration cfg) -> cfg.setDataStorageConfiguration(dsCfg)),
+            IgniteCheckedException.class,
+            "IGNITE_THRESHOLD_WAL_ARCHIVE_SIZE_PERCENTAGE must be less than or equal to 1.0"
+        );
+    }
+
+    /**
      * Starts up a node in persistent or non-persistent mode and retrieves log messages.
      */
     private String getPersistentWalLogWarning(boolean isPersistenceEnabled) throws Exception {
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/persistence/wal/FileWriteAheadLogManagerSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/persistence/wal/FileWriteAheadLogManagerSelfTest.java
index 489cf29..d226154 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/persistence/wal/FileWriteAheadLogManagerSelfTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/persistence/wal/FileWriteAheadLogManagerSelfTest.java
@@ -19,9 +19,12 @@
 
 import org.apache.ignite.configuration.DataStorageConfiguration;
 import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.testframework.junits.WithSystemProperty;
 import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
 import org.junit.Test;
 
+import static java.lang.System.setProperty;
+import static org.apache.ignite.IgniteSystemProperties.IGNITE_THRESHOLD_WAL_ARCHIVE_SIZE_PERCENTAGE;
 import static org.apache.ignite.configuration.DataStorageConfiguration.HALF_MAX_WAL_ARCHIVE_SIZE;
 import static org.apache.ignite.configuration.DataStorageConfiguration.UNLIMITED_WAL_ARCHIVE;
 import static org.apache.ignite.internal.processors.cache.persistence.wal.FileWriteAheadLogManager.minWalArchiveSize;
@@ -34,10 +37,9 @@
      * Testing of {@link FileWriteAheadLogManager#minWalArchiveSize(DataStorageConfiguration)}.
      */
     @Test
+    @WithSystemProperty(key = IGNITE_THRESHOLD_WAL_ARCHIVE_SIZE_PERCENTAGE, value = "-1")
     public void testGettingMinWalArchiveSizeFromConfiguration() {
-        DataStorageConfiguration cfg = new DataStorageConfiguration();
-
-        cfg.setMaxWalArchiveSize(UNLIMITED_WAL_ARCHIVE);
+        DataStorageConfiguration cfg = new DataStorageConfiguration().setMaxWalArchiveSize(UNLIMITED_WAL_ARCHIVE);
 
         for (long i : F.asList(10L, 20L, HALF_MAX_WAL_ARCHIVE_SIZE))
             assertEquals(UNLIMITED_WAL_ARCHIVE, minWalArchiveSize(cfg.setMinWalArchiveSize(i)));
@@ -49,4 +51,30 @@
 
         assertEquals(50, minWalArchiveSize(cfg.setMinWalArchiveSize(HALF_MAX_WAL_ARCHIVE_SIZE)));
     }
+
+    /**
+     * Testing of {@link FileWriteAheadLogManager#minWalArchiveSize(DataStorageConfiguration)}.
+     */
+    @Test
+    public void testGettingMinWalArchiveSizeFromSystemProperty() {
+        DataStorageConfiguration cfg = new DataStorageConfiguration()
+            .setMaxWalArchiveSize(UNLIMITED_WAL_ARCHIVE)
+            .setMinWalArchiveSize(HALF_MAX_WAL_ARCHIVE_SIZE);
+
+        for (double i : F.asList(0.1d, 0.3d, (double)HALF_MAX_WAL_ARCHIVE_SIZE)) {
+            setProperty(IGNITE_THRESHOLD_WAL_ARCHIVE_SIZE_PERCENTAGE, Double.toString(i));
+            assertEquals(UNLIMITED_WAL_ARCHIVE, minWalArchiveSize(cfg));
+        }
+
+        int max = 100;
+        cfg.setMaxWalArchiveSize(max);
+
+        for (double i : F.asList(0.1d, 0.2d)) {
+            setProperty(IGNITE_THRESHOLD_WAL_ARCHIVE_SIZE_PERCENTAGE, Double.toString(i));
+            assertEquals((long)(i * max), minWalArchiveSize(cfg));
+        }
+
+        setProperty(IGNITE_THRESHOLD_WAL_ARCHIVE_SIZE_PERCENTAGE, Double.toString(0.5));
+        assertEquals(25, minWalArchiveSize(cfg.setMinWalArchiveSize(25)));
+    }
 }