Mmuzaf commented on a change in pull request #8946:
URL: https://github.com/apache/ignite/pull/8946#discussion_r604252270



##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/filename/PdsConsistentIdProcessor.java
##########
@@ -122,22 +65,28 @@ public PdsConsistentIdProcessor(final GridKernalContext 
ctx) {
      * @param consistentId compatibility consistent ID
      * @return PDS folder settings compatible with previous versions.
      */
-    private PdsFolderSettings compatibleResolve(
+    private PdsFolderSettings<NodeFileLockHolder> compatibleResolve(
         @Nullable final File pstStoreBasePath,
         @NotNull final Serializable consistentId) {
 
         if (cfg.getConsistentId() != null) {
             // compatible mode from configuration is used fot this case, no 
locking, no consitent id change
-            return new PdsFolderSettings(pstStoreBasePath, 
cfg.getConsistentId());
+            return new PdsFolderSettings<>(pstStoreBasePath, 
cfg.getConsistentId());
         }
 
-        return new PdsFolderSettings(pstStoreBasePath, consistentId);
+        return new PdsFolderSettings<>(pstStoreBasePath, consistentId);
     }
 
     /** {@inheritDoc} */
-    @Override public PdsFolderSettings resolveFolders() throws 
IgniteCheckedException {
+    @Override public PdsFolderSettings<NodeFileLockHolder> resolveFolders() 
throws IgniteCheckedException {
         if (settings == null) {
-            settings = prepareNewSettings();
+            PdsFolderResolver<NodeFileLockHolder> resolver =
+                new PdsFolderResolver<>(cfg, log, 
ctx.discovery().consistentId(), this::tryLock);

Review comment:
       Should we use the `ClusterNode#consistentId` here and remove the 
deprecation? We already refactor this class.

##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/filename/PdsFolderResolver.java
##########
@@ -0,0 +1,524 @@
+/*
+ * 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.filename;
+
+import java.io.File;
+import java.io.FileFilter;
+import java.io.IOException;
+import java.io.Serializable;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.UUID;
+import java.util.function.Function;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.configuration.DataStorageConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.processors.cache.persistence.FileLockHolder;
+import org.apache.ignite.internal.util.typedef.internal.A;
+import org.apache.ignite.internal.util.typedef.internal.CU;
+import org.apache.ignite.internal.util.typedef.internal.SB;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+import static 
org.apache.ignite.IgniteSystemProperties.IGNITE_DATA_STORAGE_FOLDER_BY_CONSISTENT_ID;
+import static org.apache.ignite.IgniteSystemProperties.getBoolean;
+
+public class PdsFolderResolver<FLH extends FileLockHolder> {
+    /** Database subfolders constant prefix. */
+    private static final String DB_FOLDER_PREFIX = "node";
+
+    /** Node index and uid separator in subfolders name. */
+    private static final String NODEIDX_UID_SEPARATOR = "-";
+
+    /** Constant node subfolder prefix and node index pattern (nodeII, where 
II - node index as decimal integer) */
+    private static final String NODE_PATTERN = DB_FOLDER_PREFIX + "[0-9]*" + 
NODEIDX_UID_SEPARATOR;
+
+    /** Uuid as string pattern. */
+    private static final String UUID_STR_PATTERN = 
"[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}";
+
+    /**
+     * Subdir (nodeII-UID, where II - node index as decimal integer, UID - 
string representation of consistent ID)
+     * pattern.
+     */
+    private static final String SUBDIR_PATTERN = NODE_PATTERN + 
UUID_STR_PATTERN;
+
+    /** Database subfolders for new style filter. */
+    public static final FileFilter DB_SUBFOLDERS_NEW_STYLE_FILTER = new 
FileFilter() {
+        @Override public boolean accept(File pathname) {
+            return pathname.isDirectory() && 
pathname.getName().matches(SUBDIR_PATTERN);
+        }
+    };
+
+    /** Database subfolders for old style filter. */
+    private static final FileFilter DB_SUBFOLDERS_OLD_STYLE_FILTER = new 
FileFilter() {
+        @Override public boolean accept(File pathname) {
+            String path = pathname.toString();
+            return pathname.isDirectory()
+                && !"wal".equals(pathname.getName())
+                && 
!path.contains(DataStorageConfiguration.DFLT_BINARY_METADATA_PATH)
+                && 
!path.contains(DataStorageConfiguration.DFLT_MARSHALLER_PATH)
+                && !pathname.getName().matches(SUBDIR_PATTERN);
+        }
+    };
+
+    /** Database default folder. */
+    public static final String DB_DEFAULT_FOLDER = "db";
+
+    /** */
+    private final IgniteConfiguration cfg;
+
+    /** */
+    private final IgniteLogger log;
+
+    /** */
+    private final @Nullable Serializable consistentId;
+
+    /** */
+    private final Function<File, FLH> tryLock;
+
+    /**
+     * @param cfg Ignite configuration.
+     * @param log Logger.
+     * @param consistentId Constent id.
+     * @param tryLock Lock function.
+     */
+    public PdsFolderResolver(
+        IgniteConfiguration cfg,
+        IgniteLogger log,
+        @Nullable Serializable consistentId,
+        Function<File, FLH> tryLock
+    ) {
+        this.cfg = cfg;
+        this.log = log;
+        this.consistentId = consistentId;
+        this.tryLock = tryLock;
+    }
+
+    /**
+     * Prepares compatible PDS folder settings. No locking is performed, 
consistent ID is not overridden.
+     *
+     * @param pstStoreBasePath DB storage base path or null if persistence is 
not enabled.
+     * @param consistentId compatibility consistent ID
+     * @return PDS folder settings compatible with previous versions.
+     */
+    private PdsFolderSettings<FLH> compatibleResolve(
+        @Nullable final File pstStoreBasePath,
+        @Nullable final Serializable consistentId) {
+
+        if (cfg.getConsistentId() != null) {
+            // compatible mode from configuration is used fot this case, no 
locking, no consitent id change
+            return new PdsFolderSettings<>(pstStoreBasePath, 
cfg.getConsistentId());
+        }
+
+        if (consistentId == null)
+            return new PdsFolderSettings<>(pstStoreBasePath, consistentId);
+
+        return null;
+    }
+
+    /**
+     * Creates new settings when we don't have cached one.
+     *
+     * @return new settings with prelocked directory (if appropriate).
+     * @throws IgniteCheckedException if IO failed.
+     */
+    public PdsFolderSettings<FLH> resolve() throws IgniteCheckedException {
+        final File pstStoreBasePath = resolvePersistentStoreBasePath();
+
+        if (!CU.isPersistenceEnabled(cfg))
+            return compatibleResolve(pstStoreBasePath, consistentId);
+
+        if (cfg.isClientMode() || cfg.isDaemon())
+            return new PdsFolderSettings<>(pstStoreBasePath, 
UUID.randomUUID());
+
+        if (getBoolean(IGNITE_DATA_STORAGE_FOLDER_BY_CONSISTENT_ID, false))
+            return compatibleResolve(pstStoreBasePath, consistentId);
+
+        // compatible mode from configuration is used fot this case
+        if (cfg.getConsistentId() != null) {
+            // compatible mode from configuration is used fot this case, no 
locking, no consistent id change
+            return new PdsFolderSettings<>(pstStoreBasePath, 
cfg.getConsistentId());
+        }
+
+        if (consistentId != null) {
+            // The node scans the work directory and checks if there is a 
folder matching the consistent ID.
+            // If such a folder exists, we start up with this ID 
(compatibility mode)
+            final String subFolder = 
U.maskForFileName(consistentId.toString());
+
+            final FLH oldStyleFolderLockHolder = tryLock.apply(new 
File(pstStoreBasePath, subFolder));
+
+            if (oldStyleFolderLockHolder != null) {
+                return new PdsFolderSettings<>(pstStoreBasePath,
+                    subFolder,
+                    consistentId,
+                    oldStyleFolderLockHolder,
+                    true);
+            }
+        }
+
+        final File[] oldStyleFolders = 
pstStoreBasePath.listFiles(DB_SUBFOLDERS_OLD_STYLE_FILTER);
+
+        if (oldStyleFolders != null && oldStyleFolders.length != 0) {
+            for (File folder : oldStyleFolders) {
+                final String path = getPathDisplayableInfo(folder);
+
+                U.warn(log, "There is other non-empty storage folder under 
storage base directory [" + path + "]");
+            }
+        }
+
+        for (FolderCandidate next : 
getNodeIndexSortedCandidates(pstStoreBasePath)) {
+            final FLH fileLockHolder = tryLock.apply(next.subFolderFile());
+
+            if (fileLockHolder != null) {
+                if (log.isInfoEnabled())
+                    log.info("Successfully locked persistence storage folder 
[" + next.subFolderFile() + "]");
+
+                return new PdsFolderSettings<>(pstStoreBasePath,
+                    next.subFolderFile().getName(),
+                    next.uuid(),
+                    fileLockHolder,
+                    false);
+            }
+        }
+
+        return null;
+    }
+
+    /**
+     * @return New PDS folder.
+     * @throws IgniteCheckedException In case of error.
+     */
+    public PdsFolderSettings<FLH> generateNew() throws IgniteCheckedException {
+        final File pstStoreBasePath = resolvePersistentStoreBasePath();
+
+        // was not able to find free slot, allocating new
+        try (final FLH rootDirLock = lockRootDirectory(pstStoreBasePath)) {
+            final List<FolderCandidate> sortedCandidates = 
getNodeIndexSortedCandidates(pstStoreBasePath);
+            final int nodeIdx = sortedCandidates.isEmpty() ? 0 : 
(sortedCandidates.get(sortedCandidates.size() - 1).nodeIndex() + 1);
+
+            return generateAndLockNewDbStorage(pstStoreBasePath, nodeIdx);
+        }
+    }
+
+    /**
+     * Calculate overall folder size.
+     *
+     * @param dir directory to scan.
+     * @return total size in bytes.
+     */
+    private static FolderParams folderSize(File dir) {
+        final FolderParams params = new FolderParams();
+
+        visitFolder(dir, params);
+
+        return params;
+    }
+
+    /**
+     * Scans provided directory and its sub dirs, collects found metrics.
+     *
+     * @param dir directory to start scan from.
+     * @param params input/output.
+     */
+    private static void visitFolder(final File dir, final FolderParams params) 
{
+        for (File file : dir.listFiles()) {
+            if (file.isDirectory())
+                visitFolder(file, params);
+            else {
+                params.size += file.length();
+                params.lastModified = Math.max(params.lastModified, 
dir.lastModified());
+            }
+        }
+    }
+
+    /**
+     * @param folder folder to scan.
+     * @return folder displayable information.
+     */
+    @NotNull private String getPathDisplayableInfo(final File folder) {
+        final SB res = new SB();
+
+        res.a(getCanonicalPath(folder));
+        res.a(", ");
+        final FolderParams params = folderSize(folder);
+
+        res.a(params.size);
+        res.a(" bytes, modified ");
+        final SimpleDateFormat simpleDateFormat = new 
SimpleDateFormat("MM/dd/yyyy hh:mm a");
+
+        res.a(simpleDateFormat.format(params.lastModified));
+        res.a(" ");
+
+        return res.toString();
+    }
+
+    /**
+     * Returns the canonical pathname string of this abstract pathname.
+     *
+     * @param file path to convert.
+     * @return canonical pathname or at leas absolute if convert to canonical 
failed.
+     */
+    @NotNull private String getCanonicalPath(final File file) {
+        try {
+            return file.getCanonicalPath();
+        }
+        catch (IOException ignored) {
+            return file.getAbsolutePath();
+        }
+    }
+
+    /**
+     * Pad start of string with provided character.
+     *
+     * @param str sting to pad.
+     * @param minLength expected length.
+     * @param padChar padding character.
+     * @return padded string.
+     */
+    private static String padStart(String str, int minLength, char padChar) {
+        A.notNull(str, "String should not be empty");
+        if (str.length() >= minLength)
+            return str;
+
+        final SB sb = new SB(minLength);
+
+        for (int i = str.length(); i < minLength; ++i)
+            sb.a(padChar);
+
+        sb.a(str);
+
+        return sb.toString();
+    }
+
+    /**
+     * Creates new DB storage folder.
+     *
+     * @param pstStoreBasePath DB root path.
+     * @param nodeIdx next node index to use in folder name.
+     * @return new settings to be used in this node.
+     * @throws IgniteCheckedException if failed.
+     */
+    @NotNull private PdsFolderSettings<FLH> generateAndLockNewDbStorage(final 
File pstStoreBasePath,
+        final int nodeIdx) throws IgniteCheckedException {
+

Review comment:
       Unnecessary empty line.

##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/filename/PdsFolderResolver.java
##########
@@ -0,0 +1,524 @@
+/*
+ * 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.filename;
+
+import java.io.File;
+import java.io.FileFilter;
+import java.io.IOException;
+import java.io.Serializable;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.UUID;
+import java.util.function.Function;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.configuration.DataStorageConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.processors.cache.persistence.FileLockHolder;
+import org.apache.ignite.internal.util.typedef.internal.A;
+import org.apache.ignite.internal.util.typedef.internal.CU;
+import org.apache.ignite.internal.util.typedef.internal.SB;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+import static 
org.apache.ignite.IgniteSystemProperties.IGNITE_DATA_STORAGE_FOLDER_BY_CONSISTENT_ID;
+import static org.apache.ignite.IgniteSystemProperties.getBoolean;
+

Review comment:
       Please, add javadoc.

##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/filename/PdsFolderResolver.java
##########
@@ -0,0 +1,524 @@
+/*
+ * 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.filename;
+
+import java.io.File;
+import java.io.FileFilter;
+import java.io.IOException;
+import java.io.Serializable;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.UUID;
+import java.util.function.Function;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.configuration.DataStorageConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.processors.cache.persistence.FileLockHolder;
+import org.apache.ignite.internal.util.typedef.internal.A;
+import org.apache.ignite.internal.util.typedef.internal.CU;
+import org.apache.ignite.internal.util.typedef.internal.SB;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+import static 
org.apache.ignite.IgniteSystemProperties.IGNITE_DATA_STORAGE_FOLDER_BY_CONSISTENT_ID;
+import static org.apache.ignite.IgniteSystemProperties.getBoolean;
+
+public class PdsFolderResolver<FLH extends FileLockHolder> {
+    /** Database subfolders constant prefix. */
+    private static final String DB_FOLDER_PREFIX = "node";
+
+    /** Node index and uid separator in subfolders name. */
+    private static final String NODEIDX_UID_SEPARATOR = "-";
+
+    /** Constant node subfolder prefix and node index pattern (nodeII, where 
II - node index as decimal integer) */
+    private static final String NODE_PATTERN = DB_FOLDER_PREFIX + "[0-9]*" + 
NODEIDX_UID_SEPARATOR;
+
+    /** Uuid as string pattern. */
+    private static final String UUID_STR_PATTERN = 
"[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}";
+
+    /**
+     * Subdir (nodeII-UID, where II - node index as decimal integer, UID - 
string representation of consistent ID)
+     * pattern.
+     */
+    private static final String SUBDIR_PATTERN = NODE_PATTERN + 
UUID_STR_PATTERN;
+
+    /** Database subfolders for new style filter. */
+    public static final FileFilter DB_SUBFOLDERS_NEW_STYLE_FILTER = new 
FileFilter() {
+        @Override public boolean accept(File pathname) {
+            return pathname.isDirectory() && 
pathname.getName().matches(SUBDIR_PATTERN);
+        }
+    };
+
+    /** Database subfolders for old style filter. */
+    private static final FileFilter DB_SUBFOLDERS_OLD_STYLE_FILTER = new 
FileFilter() {
+        @Override public boolean accept(File pathname) {
+            String path = pathname.toString();
+            return pathname.isDirectory()
+                && !"wal".equals(pathname.getName())
+                && 
!path.contains(DataStorageConfiguration.DFLT_BINARY_METADATA_PATH)
+                && 
!path.contains(DataStorageConfiguration.DFLT_MARSHALLER_PATH)
+                && !pathname.getName().matches(SUBDIR_PATTERN);
+        }
+    };
+
+    /** Database default folder. */
+    public static final String DB_DEFAULT_FOLDER = "db";
+
+    /** */
+    private final IgniteConfiguration cfg;
+
+    /** */
+    private final IgniteLogger log;
+
+    /** */
+    private final @Nullable Serializable consistentId;
+
+    /** */
+    private final Function<File, FLH> tryLock;
+
+    /**
+     * @param cfg Ignite configuration.
+     * @param log Logger.
+     * @param consistentId Constent id.
+     * @param tryLock Lock function.
+     */
+    public PdsFolderResolver(
+        IgniteConfiguration cfg,
+        IgniteLogger log,
+        @Nullable Serializable consistentId,
+        Function<File, FLH> tryLock
+    ) {
+        this.cfg = cfg;
+        this.log = log;
+        this.consistentId = consistentId;
+        this.tryLock = tryLock;
+    }
+
+    /**
+     * Prepares compatible PDS folder settings. No locking is performed, 
consistent ID is not overridden.
+     *
+     * @param pstStoreBasePath DB storage base path or null if persistence is 
not enabled.
+     * @param consistentId compatibility consistent ID
+     * @return PDS folder settings compatible with previous versions.
+     */
+    private PdsFolderSettings<FLH> compatibleResolve(
+        @Nullable final File pstStoreBasePath,
+        @Nullable final Serializable consistentId) {
+
+        if (cfg.getConsistentId() != null) {
+            // compatible mode from configuration is used fot this case, no 
locking, no consitent id change
+            return new PdsFolderSettings<>(pstStoreBasePath, 
cfg.getConsistentId());
+        }
+
+        if (consistentId == null)
+            return new PdsFolderSettings<>(pstStoreBasePath, consistentId);
+
+        return null;
+    }
+
+    /**
+     * Creates new settings when we don't have cached one.

Review comment:
       We should fix the comment since a new consistent id generation was moved 
outside this method.




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