Merge branch 'gerrit/stabilization-5949a1cb71'

Change-Id: I46c5d89fc13ce9b8f3aec55ece7b5281920c8759
diff --git a/asterixdb/asterix-transactions/src/main/java/org/apache/asterix/transaction/management/resource/PersistentLocalResourceRepository.java b/asterixdb/asterix-transactions/src/main/java/org/apache/asterix/transaction/management/resource/PersistentLocalResourceRepository.java
index 508eb76..b259809 100644
--- a/asterixdb/asterix-transactions/src/main/java/org/apache/asterix/transaction/management/resource/PersistentLocalResourceRepository.java
+++ b/asterixdb/asterix-transactions/src/main/java/org/apache/asterix/transaction/management/resource/PersistentLocalResourceRepository.java
@@ -57,7 +57,6 @@
 import org.apache.asterix.common.utils.StorageConstants;
 import org.apache.asterix.common.utils.StoragePathUtil;
 import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.filefilter.IOFileFilter;
 import org.apache.hyracks.api.exceptions.HyracksDataException;
 import org.apache.hyracks.api.io.FileReference;
 import org.apache.hyracks.api.io.IIOManager;
@@ -93,41 +92,10 @@
     private static final FilenameFilter MASK_FILES_FILTER =
             (dir, name) -> name.startsWith(StorageConstants.MASK_FILE_PREFIX);
     private static final int MAX_CACHED_RESOURCES = 1000;
-    private static final IOFileFilter METADATA_FILES_FILTER = new IOFileFilter() {
-        @Override
-        public boolean accept(File file) {
-            return file.getName().equals(StorageConstants.METADATA_FILE_NAME);
-        }
-
-        @Override
-        public boolean accept(File dir, String name) {
-            return false;
-        }
-    };
-
-    private static final IOFileFilter METADATA_MASK_FILES_FILTER = new IOFileFilter() {
-        @Override
-        public boolean accept(File file) {
-            return file.getName().equals(METADATA_FILE_MASK_NAME);
-        }
-
-        @Override
-        public boolean accept(File dir, String name) {
-            return false;
-        }
-    };
-
-    private static final IOFileFilter ALL_DIR_FILTER = new IOFileFilter() {
-        @Override
-        public boolean accept(File file) {
-            return true;
-        }
-
-        @Override
-        public boolean accept(File dir, String name) {
-            return true;
-        }
-    };
+    private static final FilenameFilter METADATA_FILES_FILTER =
+            (dir, name) -> name.equals(StorageConstants.METADATA_FILE_NAME);
+    private static final FilenameFilter METADATA_MASK_FILES_FILTER =
+            (dir, name) -> name.equals(METADATA_FILE_MASK_NAME);
 
     // Finals
     private final IIOManager ioManager;
@@ -257,7 +225,10 @@
             throws HyracksDataException {
         Map<Long, LocalResource> resourcesMap = new HashMap<>();
         for (Path root : storageRoots) {
-            final Collection<File> files = FileUtils.listFiles(root.toFile(), METADATA_FILES_FILTER, ALL_DIR_FILTER);
+            if (!Files.exists(root) || !Files.isDirectory(root)) {
+                continue;
+            }
+            final Collection<File> files = IoUtil.getMatchingFiles(root, METADATA_FILES_FILTER);
             try {
                 for (File file : files) {
                     final LocalResource localResource = readLocalResource(file);
@@ -462,8 +433,7 @@
 
     public void deleteCorruptedResources() throws HyracksDataException {
         for (Path root : storageRoots) {
-            final Collection<File> metadataMaskFiles =
-                    FileUtils.listFiles(root.toFile(), METADATA_MASK_FILES_FILTER, ALL_DIR_FILTER);
+            final Collection<File> metadataMaskFiles = IoUtil.getMatchingFiles(root, METADATA_MASK_FILES_FILTER);
             for (File metadataMaskFile : metadataMaskFiles) {
                 final File resourceFile = new File(metadataMaskFile.getParent(), METADATA_FILE_NAME);
                 if (resourceFile.exists()) {
diff --git a/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/util/IoUtil.java b/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/util/IoUtil.java
index 09ecb15..825fdd6 100644
--- a/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/util/IoUtil.java
+++ b/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/util/IoUtil.java
@@ -19,11 +19,16 @@
 package org.apache.hyracks.api.util;
 
 import java.io.File;
+import java.io.FileFilter;
 import java.io.FileNotFoundException;
+import java.io.FilenameFilter;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.NoSuchFileException;
 import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Objects;
 
 import org.apache.commons.io.FileUtils;
 import org.apache.hyracks.api.exceptions.ErrorCode;
@@ -132,4 +137,35 @@
         return files;
     }
 
+    /**
+     * Gets a collection of files matching {@code filter} by searching {@code root} directory and
+     * all of its subdirectories
+     *
+     * @param root
+     * @param filter
+     * @return a collection of matching files
+     */
+    public static Collection<File> getMatchingFiles(Path root, FilenameFilter filter) {
+        if (!Files.isDirectory(root)) {
+            throw new IllegalArgumentException("Parameter 'root' is not a directory: " + root);
+        }
+        Objects.requireNonNull(filter);
+        Collection<File> files = new ArrayList<>();
+        FileFilter dirOrMatchingFileFilter = file -> file.isDirectory() || filter.accept(file, file.getName());
+        collectDirFiles(root.toFile(), dirOrMatchingFileFilter, files);
+        return files;
+    }
+
+    private static void collectDirFiles(File dir, FileFilter filter, Collection<File> files) {
+        File[] matchingFiles = dir.listFiles(filter);
+        if (matchingFiles != null) {
+            for (File file : matchingFiles) {
+                if (file.isDirectory()) {
+                    collectDirFiles(file, filter, files);
+                } else {
+                    files.add(file);
+                }
+            }
+        }
+    }
 }