浏览代码

improve file info process logging

jrivard@gmail.com 6 年之前
父节点
当前提交
2a931ee011

+ 5 - 1
server/src/main/java/password/pwm/util/java/ConditionalTaskExecutor.java

@@ -78,7 +78,11 @@ public class ConditionalTaskExecutor
         this.predicate = predicate;
     }
 
-
+    public static ConditionalTaskExecutor forPeriodicTask( final Runnable task, final TimeDuration timeDuration )
+    {
+        return new ConditionalTaskExecutor( task, new TimeDurationPredicate( timeDuration ) );
+    }
+    
     public static class TimeDurationPredicate implements Supplier<Boolean>
     {
         private final TimeDuration timeDuration;

+ 36 - 9
server/src/main/java/password/pwm/util/java/FileSystemUtility.java

@@ -36,23 +36,44 @@ import java.nio.file.Files;
 import java.time.Instant;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicLong;
 
 public class FileSystemUtility
 {
-
     private static final PwmLogger LOGGER = PwmLogger.forClass( FileSystemUtility.class );
 
     public static List<FileSummaryInformation> readFileInformation( final File rootFile )
             throws PwmUnrecoverableException, IOException
     {
-        return readFileInformation( rootFile, "" );
+        final AtomicInteger fileCounter = new AtomicInteger();
+        final AtomicLong byteCounter = new AtomicLong();
+        final Instant startTime = Instant.now();
+        final ConditionalTaskExecutor debugLogger = ConditionalTaskExecutor.forPeriodicTask(
+                () -> LOGGER.trace( () -> "file info reading for path " + rootFile.getAbsolutePath() + " in progress, "
+                        + fileCounter.get() + " files, "
+                        + StringUtil.formatDiskSizeforDebug( byteCounter.get() )
+                        + " bytes, scanned in " + TimeDuration.compactFromCurrent( startTime )
+                ),
+                TimeDuration.SECONDS_10
+        );
+
+        final List<FileSummaryInformation> results = readFileInformation( rootFile, "", debugLogger, fileCounter, byteCounter );
+        LOGGER.trace( () -> "completed file info reading for path " + rootFile.getAbsolutePath() + ", "
+                + fileCounter.get() + " files, "
+                + StringUtil.formatDiskSizeforDebug( byteCounter.get() )
+                + " bytes, scanned in " + TimeDuration.compactFromCurrent( startTime ) );
+        return results;
     }
 
-    protected static List<FileSummaryInformation> readFileInformation(
+    private static List<FileSummaryInformation> readFileInformation(
             final File rootFile,
-            final String relativePath
+            final String relativePath,
+            final ConditionalTaskExecutor debugLogger,
+            final AtomicInteger fileCounter,
+            final AtomicLong byteCounter
     )
-            throws PwmUnrecoverableException, IOException
+            throws PwmUnrecoverableException
     {
         final ArrayList<FileSummaryInformation> results = new ArrayList<>();
         final File[] files = rootFile.listFiles();
@@ -63,19 +84,25 @@ public class FileSystemUtility
                 final String path = relativePath + loopFile.getName();
                 if ( loopFile.isDirectory() )
                 {
-                    results.addAll( readFileInformation( loopFile, path + "/" ) );
+                    final String subPath = path + File.separator;
+                    results.addAll( readFileInformation( loopFile, subPath, debugLogger, fileCounter, byteCounter ) );
                 }
                 else
                 {
-                    results.add( fileInformationForFile( loopFile ) );
+                    final FileSummaryInformation fileInformation = fileInformationForFile( loopFile );
+                    results.add( fileInformation );
+                    fileCounter.incrementAndGet();
+                    byteCounter.addAndGet( fileInformation.getSize() );
                 }
+
+                debugLogger.conditionallyExecuteTask();
             }
         }
         return results;
     }
 
-    public static FileSummaryInformation fileInformationForFile( final File file )
-            throws IOException, PwmUnrecoverableException
+    private static FileSummaryInformation fileInformationForFile( final File file )
+            throws PwmUnrecoverableException
     {
         if ( file == null || !file.exists() )
         {

+ 1 - 1
server/src/main/java/password/pwm/util/localdb/XodusLocalDB.java

@@ -140,7 +140,7 @@ public class XodusLocalDB implements LocalDBProvider
 
         try
         {
-            if ( getDirtyFile().exists() )
+            if ( !getDirtyFile().exists() )
             {
                 Files.createFile( getDirtyFile().toPath() );
                 LOGGER.trace( () -> "created openLock file" );

+ 3 - 2
server/src/main/java/password/pwm/util/secure/SecureEngine.java

@@ -63,6 +63,7 @@ public class SecureEngine
     private static final PwmLogger LOGGER = PwmLogger.forClass( SecureEngine.class );
 
     private static final int HASH_BUFFER_SIZE = 1024 * 4;
+    private static final int HASH_FILE_BUFFER_SIZE = 1024 * 64;
 
     private static final NonceGenerator AES_GCM_NONCE_GENERATOR = new NonceGenerator( 8, 8 );
 
@@ -278,7 +279,7 @@ public class SecureEngine
             final File file,
             final PwmHashAlgorithm hashAlgorithm
     )
-            throws IOException, PwmUnrecoverableException
+            throws PwmUnrecoverableException
     {
         FileInputStream fileInputStream = null;
         try
@@ -286,7 +287,7 @@ public class SecureEngine
             final MessageDigest messageDigest = MessageDigest.getInstance( hashAlgorithm.getAlgName() );
             fileInputStream = new FileInputStream( file );
             final FileChannel fileChannel = fileInputStream.getChannel();
-            final ByteBuffer byteBuffer = ByteBuffer.allocateDirect( 1024 * 8 );
+            final ByteBuffer byteBuffer = ByteBuffer.allocateDirect( HASH_FILE_BUFFER_SIZE );
 
             while ( fileChannel.read( byteBuffer ) > 0 )
             {