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 2020/07/20 10:11:08 UTC

[GitHub] [maven-resolver] rmannibucau commented on a change in pull request #65: [MRESOLVER-123] Provide a global locking sync context by default

rmannibucau commented on a change in pull request #65:
URL: https://github.com/apache/maven-resolver/pull/65#discussion_r457240942



##########
File path: maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultSyncContextFactory.java
##########
@@ -28,31 +30,59 @@
 import org.eclipse.aether.artifact.Artifact;
 import org.eclipse.aether.impl.SyncContextFactory;
 import org.eclipse.aether.metadata.Metadata;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.inject.Singleton;
 
 /**
- * A factory to create synchronization contexts. This default implementation actually does not provide any real
- * synchronization but merely completes the repository system.
+ * A factory to create synchronization contexts. This default implementation uses fair global locking
+ * based on {@link ReentrantReadWriteLock}. Explicit artifacts and metadata passed are ignored.
  */
 @Named
+@Singleton
 public class DefaultSyncContextFactory
     implements SyncContextFactory
 {
+    private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock( true );
 
     public SyncContext newInstance( RepositorySystemSession session, boolean shared )

Review comment:
       1. Is the session needed there? can't it be injected?
   2. Is shared needed there, shouldn't the context handle that since it is the one holding the underlying lock
   
   For me the factory should just load an impl and be useless if there is no pluggability there, context is where the abstraction is (see next comment)

##########
File path: maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultSyncContextFactory.java
##########
@@ -28,31 +30,59 @@
 import org.eclipse.aether.artifact.Artifact;
 import org.eclipse.aether.impl.SyncContextFactory;
 import org.eclipse.aether.metadata.Metadata;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.inject.Singleton;
 
 /**
- * A factory to create synchronization contexts. This default implementation actually does not provide any real
- * synchronization but merely completes the repository system.
+ * A factory to create synchronization contexts. This default implementation uses fair global locking
+ * based on {@link ReentrantReadWriteLock}. Explicit artifacts and metadata passed are ignored.
  */
 @Named
+@Singleton
 public class DefaultSyncContextFactory
     implements SyncContextFactory
 {
+    private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock( true );
 
     public SyncContext newInstance( RepositorySystemSession session, boolean shared )
     {
-        return new DefaultSyncContext();
+        return new DefaultSyncContext( shared ? lock.readLock() : lock.writeLock(), shared );
     }
 
     static class DefaultSyncContext
         implements SyncContext
     {
+        private static final Logger LOGGER = LoggerFactory.getLogger( DefaultSyncContext.class );
+
+        private final Lock lock;
+        private final boolean shared;
+        private int lockHoldCount;
+
+        DefaultSyncContext( Lock lock, boolean shared )

Review comment:
       this class is not threadsafe but should be otherwise this default impl does not work as soon as you use -T

##########
File path: maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultSyncContextFactory.java
##########
@@ -28,31 +30,59 @@
 import org.eclipse.aether.artifact.Artifact;
 import org.eclipse.aether.impl.SyncContextFactory;
 import org.eclipse.aether.metadata.Metadata;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.inject.Singleton;
 
 /**
- * A factory to create synchronization contexts. This default implementation actually does not provide any real
- * synchronization but merely completes the repository system.
+ * A factory to create synchronization contexts. This default implementation uses fair global locking
+ * based on {@link ReentrantReadWriteLock}. Explicit artifacts and metadata passed are ignored.
  */
 @Named
+@Singleton
 public class DefaultSyncContextFactory
     implements SyncContextFactory
 {
+    private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock( true );
 
     public SyncContext newInstance( RepositorySystemSession session, boolean shared )
     {
-        return new DefaultSyncContext();
+        return new DefaultSyncContext( shared ? lock.readLock() : lock.writeLock(), shared );
     }
 
     static class DefaultSyncContext
         implements SyncContext
     {
+        private static final Logger LOGGER = LoggerFactory.getLogger( DefaultSyncContext.class );
+
+        private final Lock lock;
+        private final boolean shared;
+        private int lockHoldCount;
+
+        DefaultSyncContext( Lock lock, boolean shared )
+        {
+            this.lock = lock;
+            this.shared = shared;
+        }
 
         public void acquire( Collection<? extends Artifact> artifact, Collection<? extends Metadata> metadata )
         {
+            LOGGER.trace( "Acquiring global {} lock (currently held: {})",
+                          shared ? "read" : "write", lockHoldCount );
+            lock.lock();
+            lockHoldCount++;
         }
 
         public void close()

Review comment:
       I would drop it in favor of th returned value of acquire.

##########
File path: maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultSyncContextFactory.java
##########
@@ -28,31 +30,59 @@
 import org.eclipse.aether.artifact.Artifact;
 import org.eclipse.aether.impl.SyncContextFactory;
 import org.eclipse.aether.metadata.Metadata;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.inject.Singleton;
 
 /**
- * A factory to create synchronization contexts. This default implementation actually does not provide any real
- * synchronization but merely completes the repository system.
+ * A factory to create synchronization contexts. This default implementation uses fair global locking
+ * based on {@link ReentrantReadWriteLock}. Explicit artifacts and metadata passed are ignored.
  */
 @Named
+@Singleton
 public class DefaultSyncContextFactory
     implements SyncContextFactory
 {
+    private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock( true );
 
     public SyncContext newInstance( RepositorySystemSession session, boolean shared )
     {
-        return new DefaultSyncContext();
+        return new DefaultSyncContext( shared ? lock.readLock() : lock.writeLock(), shared );
     }
 
     static class DefaultSyncContext
         implements SyncContext
     {
+        private static final Logger LOGGER = LoggerFactory.getLogger( DefaultSyncContext.class );
+
+        private final Lock lock;
+        private final boolean shared;
+        private int lockHoldCount;
+
+        DefaultSyncContext( Lock lock, boolean shared )
+        {
+            this.lock = lock;
+            this.shared = shared;
+        }
 
         public void acquire( Collection<? extends Artifact> artifact, Collection<? extends Metadata> metadata )

Review comment:
       Think it is better to return a Map of locks (or just Runnable which would be lock::unlock) since for more fine grained impls, some lock can fail whereas others would work. In such case, we should let the caller handle it properly.
   Can be:
   
       Map<Object, Runnable>
   
   for the simplest form (or typed if you prefer, just care that functionally it matches fine grained impls).




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