sergey-chugunov-1985 commented on a change in pull request #8681:
URL: https://github.com/apache/ignite/pull/8681#discussion_r568387101



##########
File path: 
modules/core/src/test/java/org/apache/ignite/internal/processors/cache/persistence/wal/WalArchiveConsistencyTest.java
##########
@@ -0,0 +1,295 @@
+/*
+ * 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.persistence.wal;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Consumer;
+import org.apache.ignite.cluster.ClusterState;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.DataRegionConfiguration;
+import org.apache.ignite.configuration.DataStorageConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.configuration.WALMode;
+import org.apache.ignite.internal.IgniteEx;
+import 
org.apache.ignite.internal.processors.cache.persistence.GridCacheDatabaseSharedManager;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.testframework.junits.WithSystemProperty;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import static 
org.apache.ignite.IgniteSystemProperties.IGNITE_THRESHOLD_WAL_ARCHIVE_SIZE_PERCENTAGE;
+import static 
org.apache.ignite.configuration.DataStorageConfiguration.DFLT_WAL_PATH;
+import static org.apache.ignite.testframework.GridTestUtils.waitForCondition;
+
+/**
+ * Class for testing cases when WAL archive configuration was changed and the 
node was able to start.
+ */
+@RunWith(Parameterized.class)
+@WithSystemProperty(key = IGNITE_THRESHOLD_WAL_ARCHIVE_SIZE_PERCENTAGE, value 
= "0.0")
+public class WalArchiveConsistencyTest extends GridCommonAbstractTest {
+    /**
+     * WAL mode.
+     */
+    @Parameterized.Parameter
+    public WALMode walMode;
+
+    /**
+     * @return Test parameters.
+     */
+    @Parameterized.Parameters(name = "walMode={0}")
+    public static Collection<Object[]> parameters() {
+        return Arrays.asList(
+            new Object[] {WALMode.LOG_ONLY},
+            new Object[] {WALMode.FSYNC}
+

Review comment:
       empty line

##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/wal/FileWriteAheadLogManager.java
##########
@@ -1521,21 +1525,129 @@ private void checkOrPrepareFiles() throws 
StorageException {
             }
         }
 
-        File[] allFiles = walWorkDir.listFiles(WAL_SEGMENT_FILE_FILTER);
+        FileDescriptor[] walFiles = 
scan(walWorkDir.listFiles(WAL_SEGMENT_FILE_FILTER));
 
-        if (isArchiverEnabled() && !F.isEmpty(allFiles) && allFiles.length > 
dsCfg.getWalSegments()) {
-            throw new StorageException("Failed to initialize wal (work 
directory contains incorrect " +
-                "number of segments) [cur=" + allFiles.length + ", expected=" 
+ dsCfg.getWalSegments() + ']');
-        }
+        if (F.isEmpty(walFiles))
+            createFile(new File(walWorkDir, fileName(0)));
 
-        // Allocate the first segment synchronously. All other segments will 
be allocated by archiver in background.
-        if (F.isEmpty(allFiles)) {
-            File first = new File(walWorkDir, fileName(0));
+        if (isArchiverEnabled()) {

Review comment:
       this part contains a lot of different logic: moving segments to arch 
dir, renaming last segment, formatting segments etc
   
   I strongly believe all these (isolated, to some extent) pieces of code 
should live in their own methods with descriptive names and corresponding 
javadocs. This should improve readability of our code greatly.

##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/wal/FileWriteAheadLogManager.java
##########
@@ -1521,21 +1525,129 @@ private void checkOrPrepareFiles() throws 
StorageException {
             }
         }
 
-        File[] allFiles = walWorkDir.listFiles(WAL_SEGMENT_FILE_FILTER);
+        FileDescriptor[] walFiles = 
scan(walWorkDir.listFiles(WAL_SEGMENT_FILE_FILTER));
 
-        if (isArchiverEnabled() && !F.isEmpty(allFiles) && allFiles.length > 
dsCfg.getWalSegments()) {
-            throw new StorageException("Failed to initialize wal (work 
directory contains incorrect " +
-                "number of segments) [cur=" + allFiles.length + ", expected=" 
+ dsCfg.getWalSegments() + ']');
-        }
+        if (F.isEmpty(walFiles))
+            createFile(new File(walWorkDir, fileName(0)));
 
-        // Allocate the first segment synchronously. All other segments will 
be allocated by archiver in background.
-        if (F.isEmpty(allFiles)) {
-            File first = new File(walWorkDir, fileName(0));
+        if (isArchiverEnabled()) {
+            List<FileDescriptor> toMove = new ArrayList<>();
+            @Nullable FileDescriptor toRen = null;
+
+            if (!F.isEmpty(walFiles) && (walFiles.length > 
dsCfg.getWalSegments() || walFiles[0].idx() != 0)) {
+                toMove.addAll(F.asList(walFiles));
+
+                FileDescriptor rmv = toMove.remove(toMove.size() - 1);
+                toRen = rmv.idx() != rmv.idx() % dsCfg.getWalSegments() ? rmv 
: null;
+            }
+
+            
toMove.addAll(F.asList(scan(walWorkDir.listFiles(WAL_SEGMENT_FILE_COMPACTED_FILTER))));
+
+            if (!toMove.isEmpty() || toRen != null) {
+                log.warning("Invalid WAL working directory contents found, for 
archiving to work, will need " +
+                    (toMove.isEmpty() ? "" : "move WAL segments to archive " + 
(toRen == null ? "" : "and ")) +
+                    (toRen == null ? "" : "rename last WAL segment") + ", this 
may take some time [" +
+                    (toMove.isEmpty() ? "" : "toMove=" + 
F.viewReadOnly(toMove, fd -> fd.file().getName())) +
+                    (toRen == null ? "" : (toMove.isEmpty() ? "" : ", ") + 
"toRename=[" + toRen.file().getName() +
+                        " -> " + fileName(toRen.idx() % 
dsCfg.getWalSegments()) + ']') + ']');
+
+                for (FileDescriptor fd : toMove) {
+                    File tmpDst = new File(walArchiveDir, fd.file().getName() 
+ TMP_SUFFIX);
+                    File dst = new File(walArchiveDir, fd.file().getName());
+
+                    try {
+                        Files.copy(fd.file().toPath(), tmpDst.toPath());
+
+                        Files.move(tmpDst.toPath(), dst.toPath());
+
+                        Files.delete(fd.file().toPath());
+
+                        if (log.isInfoEnabled()) {

Review comment:
       I can easily imagine a situation when we have dozens or even hundreds 
segments to move, printing out a message for each will blow up our logs 
immediately.
   
   Suggest replacing with some batching.




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