You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by GitBox <gi...@apache.org> on 2019/04/01 17:19:43 UTC

[GitHub] [maven-resolver] rfscholte commented on a change in pull request #22: Introduce caching for tracking files.

rfscholte commented on a change in pull request #22: Introduce caching for tracking files.
URL: https://github.com/apache/maven-resolver/pull/22#discussion_r270969267
 
 

 ##########
 File path: maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/TrackingFileManager.java
 ##########
 @@ -131,52 +207,145 @@ public Properties update( File file, Map<String, String> updates )
                 raf.seek( 0 );
                 raf.write( stream.toByteArray() );
                 raf.setLength( raf.getFilePointer() );
+
+                this.entryTs = System.currentTimeMillis();
+                this.lastModifiedTs = file.lastModified();
             }
             catch ( IOException e )
             {
                 LOGGER.warn( "Failed to write tracking file {}", file, e );
             }
             finally
             {
-                release( lock, file );
-                close( raf, file );
+                release( lock );
+                close( raf );
             }
-        }
 
-        return props;
-    }
+            return props;
+        }
 
-    private void release( FileLock lock, File file )
-    {
-        if ( lock != null )
+        private void release( FileLock lock )
         {
-            try
+            if ( lock != null )
             {
-                lock.release();
+                try
+                {
+                    lock.release();
+                }
+                catch ( IOException e )
+                {
+                    LOGGER.warn( "Error releasing lock for tracking file {}", file, e );
+                }
             }
-            catch ( IOException e )
+        }
+
+        private void close( Closeable closeable )
+        {
+            if ( closeable != null )
             {
-                LOGGER.warn( "Error releasing lock for tracking file {}", file, e );
+                try
+                {
+                    closeable.close();
+                }
+                catch ( IOException e )
+                {
+                    LOGGER.warn( "Error closing tracking file {}", file, e );
+                }
             }
         }
-    }
 
-    private void close( Closeable closeable, File file )
-    {
-        if ( closeable != null )
+        private FileLock lock( FileChannel channel, long size, boolean shared ) throws IOException
         {
-            try
+            FileLock lock = null;
+
+            for ( int attempts = 8; attempts >= 0; attempts-- )
             {
-                closeable.close();
+                try
+                {
+                    lock = channel.lock( 0, size, shared );
+                    break;
+                }
+                catch ( OverlappingFileLockException e )
+                {
+                    if ( attempts <= 0 )
+                    {
+                        throw (IOException) new IOException().initCause( e );
+                    }
+                    try
+                    {
+                        Thread.sleep( 50L );
+                    }
+                    catch ( InterruptedException e1 )
+                    {
+                        Thread.currentThread().interrupt();
+                    }
+                }
             }
-            catch ( IOException e )
+
+            if ( lock == null )
             {
-                LOGGER.warn( "Error closing tracking file {}", file, e );
+                throw new IOException( "Could not lock file" );
             }
+
+            return lock;
+        }
+
+    }
+
+    /**
+     * Canonicalized files by their path (the same canonicalized file may be registered under different paths). This
+     * cache is especially useful on Windows platform where canonicalization is relatively expensive.
+     */
+    private static ConcurrentHashMap<String, File> canonicalizedCache = new ConcurrentHashMap<>();
 
 Review comment:
   In case of a cache I would prefer `WeakHashMap` + `Collections.synchronizedMap` 

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services