This is an automated email from the ASF dual-hosted git repository. michaelo pushed a commit to branch redisson in repository https://gitbox.apache.org/repos/asf/maven-resolver.git
The following commit(s) were added to refs/heads/redisson by this push: new f35a408 Reformat f35a408 is described below commit f35a408c660ca78501bea2bdff228a5f57972cbf Author: Michael Osipov <micha...@apache.org> AuthorDate: Tue Aug 11 10:15:19 2020 +0200 Reformat --- .../synccontext/RedissonSyncContextFactory.java | 227 +++++++++++++-------- 1 file changed, 137 insertions(+), 90 deletions(-) diff --git a/maven-resolver-synccontext-redisson/src/main/java/org/eclipse/aether/synccontext/RedissonSyncContextFactory.java b/maven-resolver-synccontext-redisson/src/main/java/org/eclipse/aether/synccontext/RedissonSyncContextFactory.java index b06e83e..067d600 100644 --- a/maven-resolver-synccontext-redisson/src/main/java/org/eclipse/aether/synccontext/RedissonSyncContextFactory.java +++ b/maven-resolver-synccontext-redisson/src/main/java/org/eclipse/aether/synccontext/RedissonSyncContextFactory.java @@ -58,7 +58,8 @@ import org.slf4j.LoggerFactory; * if required. <br> * This factory is considered experimental and is intended to be used as a singleton. */ -public class RedissonSyncContextFactory implements SyncContextFactory { +public class RedissonSyncContextFactory implements SyncContextFactory +{ private static final String DEFAULT_CONFIG_FILE_NAME = "maven-resolver-redisson.yaml"; private static final String DEFAULT_REDIS_ADDRESS = "redis://localhost:6379"; @@ -68,27 +69,34 @@ public class RedissonSyncContextFactory implements SyncContextFactory { private static final String CONFIG_PROP_CONFIG_FILE = "aether.syncContext.redisson.configFile"; - private static final Logger LOGGER = LoggerFactory.getLogger(RedissonSyncContextFactory.class); + private static final Logger LOGGER = LoggerFactory.getLogger( RedissonSyncContextFactory.class ); - private static final RedissonClient redissonClient = createRedissonClient(); - private static final String localhostDiscriminator = createLocalhostDiscriminator(); + private static RedissonClient redissonClient = createRedissonClient(); + private static String localhostDiscriminator = createLocalhostDiscriminator(); - private static RedissonClient createRedissonClient() { + private static RedissonClient createRedissonClient() + { Path configFilePath = null; - String configFile = ConfigUtils.getString(System.getProperties(), null, CONFIG_PROP_CONFIG_FILE); - if (configFile != null && !configFile.isEmpty()) { - configFilePath = Paths.get(configFile); - if (Files.notExists(configFilePath)) { - throw new IllegalArgumentException("The specified Redisson config file does not exist: " + configFilePath); + String configFile = ConfigUtils.getString( System.getProperties(), null, CONFIG_PROP_CONFIG_FILE ); + if ( configFile != null && !configFile.isEmpty() ) + { + configFilePath = Paths.get( configFile ); + if ( Files.notExists( configFilePath ) ) + { + throw new IllegalArgumentException( "The specified Redisson config file does not exist: " + + configFilePath ); } } - if (configFilePath == null) { - String mavenConf = ConfigUtils.getString(System.getProperties(), null, "maven.conf"); - if (mavenConf != null && !mavenConf.isEmpty()) { - configFilePath = Paths.get(mavenConf, DEFAULT_CONFIG_FILE_NAME); - if (Files.notExists(configFilePath)) { + if ( configFilePath == null ) + { + String mavenConf = ConfigUtils.getString( System.getProperties(), null, "maven.conf" ); + if ( mavenConf != null && !mavenConf.isEmpty() ) + { + configFilePath = Paths.get( mavenConf, DEFAULT_CONFIG_FILE_NAME ); + if ( Files.notExists( configFilePath ) ) + { configFilePath = null; } } @@ -96,46 +104,59 @@ public class RedissonSyncContextFactory implements SyncContextFactory { Config config = null; - if (configFilePath != null) { - LOGGER.trace("Reading Redisson config file from '{}'", configFilePath); - try (InputStream is = Files.newInputStream(configFilePath)) { - config = Config.fromYAML(is); - } catch (IOException e) { - throw new IllegalStateException("Failed to read Redisson config file: " + configFilePath, e); + if ( configFilePath != null ) + { + LOGGER.trace( "Reading Redisson config file from '{}'", configFilePath ); + try ( InputStream is = Files.newInputStream( configFilePath ) ) + { + config = Config.fromYAML( is ); } - } else { + catch ( IOException e ) + { + throw new IllegalStateException( "Failed to read Redisson config file: " + configFilePath, e ); + } + } + else + { config = new Config(); - config.useSingleServer().setAddress(DEFAULT_REDIS_ADDRESS) - .setClientName(DEFAULT_CLIENT_NAME); + config.useSingleServer() + .setAddress( DEFAULT_REDIS_ADDRESS ) + .setClientName( DEFAULT_CLIENT_NAME ); } - RedissonClient redissonClient = Redisson.create(config); - LOGGER.trace("Created Redisson client with id '{}'", redissonClient.getId()); + RedissonClient redissonClient = Redisson.create( config ); + LOGGER.trace( "Created Redisson client with id '{}'", redissonClient.getId() ); return redissonClient; } - private static String createLocalhostDiscriminator() { - try { + private static String createLocalhostDiscriminator() + { + try + { return InetAddress.getLocalHost().getHostName(); - } catch (UnknownHostException e) { - LOGGER.warn("Failed to calculate localhost descriminator, using '{}'", - DEFAULT_DISCRIMINATOR, e); + } + catch ( UnknownHostException e ) + { + LOGGER.warn( "Failed to calculate localhost descriminator, using '{}'", + DEFAULT_DISCRIMINATOR, e ); return DEFAULT_DISCRIMINATOR; } } - public SyncContext newInstance(RepositorySystemSession session, boolean shared) { - return new RedissonSyncContext(session, localhostDiscriminator, redissonClient, shared); + public SyncContext newInstance( RepositorySystemSession session, boolean shared ) + { + return new RedissonSyncContext( session, localhostDiscriminator, redissonClient, shared ); } - static class RedissonSyncContext implements SyncContext { + static class RedissonSyncContext implements SyncContext + { private static final String CONFIG_DISCRIMINATOR = "aether.syncContext.redisson.discriminator"; private static final String KEY_PREFIX = "maven:resolver:"; - private static final Logger LOGGER = LoggerFactory.getLogger(RedissonSyncContext.class); + private static final Logger LOGGER = LoggerFactory.getLogger( RedissonSyncContext.class ); private final RepositorySystemSession session; private final String localhostDiscriminator; @@ -143,103 +164,125 @@ public class RedissonSyncContextFactory implements SyncContextFactory { private final boolean shared; private final Map<String, RReadWriteLock> locks = new LinkedHashMap<>(); - private RedissonSyncContext(RepositorySystemSession session, String localhostDiscriminator, - RedissonClient redisson, boolean shared) { + private RedissonSyncContext( RepositorySystemSession session, String localhostDiscriminator, + RedissonClient redisson, boolean shared ) + { this.session = session; this.localhostDiscriminator = localhostDiscriminator; this.redissonClient = redisson; this.shared = shared; } - public void acquire(Collection<? extends Artifact> artifacts, - Collection<? extends Metadata> metadatas) { + public void acquire( Collection<? extends Artifact> artifacts, + Collection<? extends Metadata> metadatas ) + { // Deadlock prevention: https://stackoverflow.com/a/16780988/696632 // We must acquire multiple locks always in the same order! Collection<String> keys = new TreeSet<>(); - if (artifacts != null) { - for (Artifact artifact : artifacts) { + if ( artifacts != null ) + { + for ( Artifact artifact : artifacts ) + { // TODO Should we include extension and classifier too? String key = "artifact:" + artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getBaseVersion(); - keys.add(key); + keys.add( key ); } } - if (metadatas != null) { - for (Metadata metadata : metadatas) { + if ( metadatas != null ) + { + for ( Metadata metadata : metadatas ) + { String key; - if (!metadata.getGroupId().isEmpty()) { - StringBuilder keyBuilder = new StringBuilder("metadata:"); - keyBuilder.append(metadata.getGroupId()); - if (!metadata.getArtifactId().isEmpty()) { - keyBuilder.append(':').append(metadata.getArtifactId()); - if (!metadata.getVersion().isEmpty()) { - keyBuilder.append(':').append(metadata.getVersion()); + if ( !metadata.getGroupId().isEmpty() ) + { + StringBuilder keyBuilder = new StringBuilder( "metadata:" ); + keyBuilder.append( metadata.getGroupId() ); + if ( !metadata.getArtifactId().isEmpty() ) + { + keyBuilder.append( ':' ).append( metadata.getArtifactId() ); + if ( !metadata.getVersion().isEmpty() ) + { + keyBuilder.append( ':' ).append( metadata.getVersion() ); } } key = keyBuilder.toString(); - } else { + } + else + { key = "metadata:ROOT"; } - keys.add(key); + keys.add( key ); } } - if (keys.isEmpty()) { + if ( keys.isEmpty() ) + { return; } String discriminator = createDiscriminator(); - LOGGER.trace("Using Redis key discriminator '{}' during this session", discriminator); + LOGGER.trace( "Using Redis key discriminator '{}' during this session", discriminator ); - LOGGER.trace("Need {} {} lock(s) for {}", keys.size(), shared ? "read" : "write", keys); + LOGGER.trace( "Need {} {} lock(s) for {}", keys.size(), shared ? "read" : "write", keys ); int acquiredLockCount = 0; int reacquiredLockCount = 0; - for (String key : keys) { - RReadWriteLock rwLock = locks.get(key); - if (rwLock == null) { + for ( String key : keys ) + { + RReadWriteLock rwLock = locks.get( key ); + if ( rwLock == null ) + { rwLock = redissonClient - .getReadWriteLock(KEY_PREFIX + discriminator + ":" + key); - locks.put(key, rwLock); + .getReadWriteLock( KEY_PREFIX + discriminator + ":" + key ); + locks.put( key, rwLock ); acquiredLockCount++; - } else { + } + else + { reacquiredLockCount++; } RLock actualLock = shared ? rwLock.readLock() : rwLock.writeLock(); - LOGGER.trace("Acquiring {} lock for '{}' (currently held: {}, already locked: {})", - shared ? "read" : "write", key, actualLock.getHoldCount(), - actualLock.isLocked()); - // If this still produces a deadlock we might need to switch to #tryLock() with n - // attempts + LOGGER.trace( "Acquiring {} lock for '{}' (currently held: {}, already locked: {})", + shared ? "read" : "write", key, actualLock.getHoldCount(), + actualLock.isLocked() ); + // If this still produces a deadlock we might need to switch to #tryLock() with n attempts actualLock.lock(); } - LOGGER.trace("Total new locks acquired: {}, total existing locks reacquired: {}", - acquiredLockCount, reacquiredLockCount); + LOGGER.trace( "Total new locks acquired: {}, total existing locks reacquired: {}", + acquiredLockCount, reacquiredLockCount ); } - private String createDiscriminator() { - String discriminator = ConfigUtils.getString(session, null, CONFIG_DISCRIMINATOR); + private String createDiscriminator() + { + String discriminator = ConfigUtils.getString( session, null, CONFIG_DISCRIMINATOR ); - if (discriminator == null || discriminator.isEmpty()) { + if ( discriminator == null || discriminator.isEmpty() ) + { File basedir = session.getLocalRepository().getBasedir(); discriminator = localhostDiscriminator + ":" + basedir; - try { + try + { Map<String, Object> checksums = ChecksumUtils.calc( - discriminator.toString().getBytes(StandardCharsets.UTF_8), - Collections.singletonList("SHA-1")); - Object checksum = checksums.get("SHA-1"); + discriminator.toString().getBytes( StandardCharsets.UTF_8 ), + Collections.singletonList( "SHA-1" ) ); + Object checksum = checksums.get( "SHA-1" ); - if (checksum instanceof Exception) + if ( checksum instanceof Exception ) + { throw (Exception) checksum; + } - return String.valueOf(checksum); - } catch (Exception e) { + return String.valueOf( checksum ); + } + catch ( Exception e ) + { // TODO Should this be warn? - LOGGER.trace("Failed to calculate discriminator digest, using '{}'", - DEFAULT_DISCRIMINATOR_DIGEST, e); + LOGGER.trace( "Failed to calculate discriminator digest, using '{}'", + DEFAULT_DISCRIMINATOR_DIGEST, e ); return DEFAULT_DISCRIMINATOR_DIGEST; } } @@ -247,26 +290,30 @@ public class RedissonSyncContextFactory implements SyncContextFactory { return discriminator; } - public void close() { - if (locks.isEmpty()) { + public void close() + { + if ( locks.isEmpty() ) + { return; } // Release locks in reverse insertion order - Deque<String> keys = new LinkedList<>(locks.keySet()); + Deque<String> keys = new LinkedList<>( locks.keySet() ); Iterator<String> keysIter = keys.descendingIterator(); - while (keysIter.hasNext()) { + while ( keysIter.hasNext() ) + { String key = keysIter.next(); - RReadWriteLock rwLock = locks.get(key); + RReadWriteLock rwLock = locks.get( key ); RLock actualLock = shared ? rwLock.readLock() : rwLock.writeLock(); - while (actualLock.getHoldCount() > 0) { - LOGGER.trace("Releasing {} lock for '{}' (currently held: {})", - shared ? "read" : "write", key, actualLock.getHoldCount()); + while ( actualLock.getHoldCount() > 0 ) + { + LOGGER.trace( "Releasing {} lock for '{}' (currently held: {})", + shared ? "read" : "write", key, actualLock.getHoldCount() ); actualLock.unlock(); } } // TODO Should we count reentrant ones too? - LOGGER.trace("Total locks released: {}", locks.size()); + LOGGER.trace( "Total locks released: {}", locks.size() ); locks.clear(); }