Skip to content

Commit

Permalink
[MRESOLVER-219] Implement NamedLock with advisory file locking
Browse files Browse the repository at this point in the history
This closes #131
  • Loading branch information
cstamas authored and michael-o committed Dec 12, 2021
1 parent 2425b62 commit 50df10d
Show file tree
Hide file tree
Showing 14 changed files with 885 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@
import org.eclipse.aether.internal.impl.synccontext.named.DiscriminatingNameMapper;
import org.eclipse.aether.internal.impl.synccontext.named.NameMapper;
import org.eclipse.aether.internal.impl.synccontext.named.StaticNameMapper;
import org.eclipse.aether.internal.impl.synccontext.named.FileGAVNameMapper;
import org.eclipse.aether.named.NamedLockFactory;
import org.eclipse.aether.named.providers.FileLockNamedLockFactory;
import org.eclipse.aether.named.providers.LocalReadWriteLockNamedLockFactory;
import org.eclipse.aether.named.providers.LocalSemaphoreNamedLockFactory;
import org.eclipse.aether.impl.UpdateCheckManager;
Expand Down Expand Up @@ -171,13 +173,17 @@ protected void configure()
.to( GAVNameMapper.class ).in( Singleton.class );
bind( NameMapper.class ).annotatedWith( Names.named( DiscriminatingNameMapper.NAME ) )
.to( DiscriminatingNameMapper.class ).in( Singleton.class );
bind( NameMapper.class ).annotatedWith( Names.named( FileGAVNameMapper.NAME ) )
.to( FileGAVNameMapper.class ).in( Singleton.class );

bind( NamedLockFactory.class ).annotatedWith( Names.named( NoopNamedLockFactory.NAME ) )
.to( NoopNamedLockFactory.class ).in( Singleton.class );
bind( NamedLockFactory.class ).annotatedWith( Names.named( LocalReadWriteLockNamedLockFactory.NAME ) )
.to( LocalReadWriteLockNamedLockFactory.class ).in( Singleton.class );
bind( NamedLockFactory.class ).annotatedWith( Names.named( LocalSemaphoreNamedLockFactory.NAME ) )
.to( LocalSemaphoreNamedLockFactory.class ).in( Singleton.class );
bind( NamedLockFactory.class ).annotatedWith( Names.named( FileLockNamedLockFactory.NAME ) )
.to( FileLockNamedLockFactory.class ).in( Singleton.class );

install( new Slf4jModule() );

Expand All @@ -188,24 +194,28 @@ protected void configure()
Map<String, NameMapper> provideNameMappers(
@Named( StaticNameMapper.NAME ) NameMapper staticNameMapper,
@Named( GAVNameMapper.NAME ) NameMapper gavNameMapper,
@Named( DiscriminatingNameMapper.NAME ) NameMapper discriminatingNameMapper )
@Named( DiscriminatingNameMapper.NAME ) NameMapper discriminatingNameMapper,
@Named( FileGAVNameMapper.NAME ) NameMapper fileGavNameMapper )
{
Map<String, NameMapper> nameMappers = new HashMap<>();
nameMappers.put( StaticNameMapper.NAME, staticNameMapper );
nameMappers.put( GAVNameMapper.NAME, gavNameMapper );
nameMappers.put( DiscriminatingNameMapper.NAME, discriminatingNameMapper );
nameMappers.put( FileGAVNameMapper.NAME, fileGavNameMapper );
return Collections.unmodifiableMap( nameMappers );
}

@Provides
@Singleton
Map<String, NamedLockFactory> provideNamedLockFactories(
@Named( LocalReadWriteLockNamedLockFactory.NAME ) NamedLockFactory localRwLock,
@Named( LocalSemaphoreNamedLockFactory.NAME ) NamedLockFactory localSemaphore )
@Named( LocalSemaphoreNamedLockFactory.NAME ) NamedLockFactory localSemaphore,
@Named( FileLockNamedLockFactory.NAME ) NamedLockFactory fileLockFactory )
{
Map<String, NamedLockFactory> factories = new HashMap<>();
factories.put( LocalReadWriteLockNamedLockFactory.NAME, localRwLock );
factories.put( LocalSemaphoreNamedLockFactory.NAME, localSemaphore );
factories.put( FileLockNamedLockFactory.NAME, fileLockFactory );
return Collections.unmodifiableMap( factories );
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package org.eclipse.aether.internal.impl.synccontext.named;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.metadata.Metadata;
import org.eclipse.aether.named.support.FileSystemFriendly;

import javax.inject.Named;
import javax.inject.Singleton;

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Path;
import java.util.Collection;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
* A {@link NameMapper} that creates same name mapping as Takari Local Repository does, with
* {@code baseDir} (local repo). Part of code blatantly copies parts of the Takari
* {@code LockingSyncContext}.
*
* @see <a href="https://github.com/takari/takari-local-repository/blob/24133e50a0478dccb5620ac2f2255187608f165b/src/main/java/io/takari/aether/concurrency/LockingSyncContext.java">Takari
* LockingSyncContext.java</a>
*/
@Singleton
@Named( FileGAVNameMapper.NAME )
public class FileGAVNameMapper
implements NameMapper, FileSystemFriendly
{
public static final String NAME = "file-gav";

private static final String LOCK_SUFFIX = ".resolverlock";

private static final char SEPARATOR = '~';

private final ConcurrentMap<String, Path> baseDirs;

public FileGAVNameMapper()
{
this.baseDirs = new ConcurrentHashMap<>();
}

@Override
public TreeSet<String> nameLocks( final RepositorySystemSession session,
final Collection<? extends Artifact> artifacts,
final Collection<? extends Metadata> metadatas )
{
File localRepositoryBasedir = session.getLocalRepository().getBasedir();
// here we abuse concurrent hash map to make sure costly getCanonicalFile is invoked only once
Path baseDir = baseDirs.computeIfAbsent(
localRepositoryBasedir.getPath(), k ->
{
try
{
return new File( localRepositoryBasedir, ".locks" ).getCanonicalFile().toPath();
}
catch ( IOException e )
{
throw new UncheckedIOException( e );
}
}
);

TreeSet<String> paths = new TreeSet<>();
if ( artifacts != null )
{
for ( Artifact artifact : artifacts )
{
paths.add( getPath( baseDir, artifact ) + LOCK_SUFFIX );
}
}
if ( metadatas != null )
{
for ( Metadata metadata : metadatas )
{
paths.add( getPath( baseDir, metadata ) + LOCK_SUFFIX );
}
}
return paths;
}

private String getPath( final Path baseDir, final Artifact artifact )
{
// NOTE: Don't use LRM.getPath*() as those paths could be different across processes, e.g. due to staging LRMs.
String path = artifact.getGroupId()
+ SEPARATOR + artifact.getArtifactId()
+ SEPARATOR + artifact.getBaseVersion();
return baseDir.resolve( path ).toAbsolutePath().toString();
}

private String getPath( final Path baseDir, final Metadata metadata )
{
// NOTE: Don't use LRM.getPath*() as those paths could be different across processes, e.g. due to staging.
String path = "";
if ( metadata.getGroupId().length() > 0 )
{
path += metadata.getGroupId();
if ( metadata.getArtifactId().length() > 0 )
{
path += SEPARATOR + metadata.getArtifactId();
if ( metadata.getVersion().length() > 0 )
{
path += SEPARATOR + metadata.getVersion();
}
}
}
return baseDir.resolve( path ).toAbsolutePath().toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
import org.eclipse.aether.metadata.Metadata;
import org.eclipse.aether.named.NamedLock;
import org.eclipse.aether.named.NamedLockFactory;
import org.eclipse.aether.named.support.FileSystemFriendly;
import org.eclipse.aether.util.ConfigUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -56,6 +58,14 @@ public NamedLockFactoryAdapter( final NameMapper nameMapper, final NamedLockFact
{
this.nameMapper = Objects.requireNonNull( nameMapper );
this.namedLockFactory = Objects.requireNonNull( namedLockFactory );
// TODO: this is ad-hoc "validation", experimental and likely to change
if ( this.namedLockFactory instanceof FileSystemFriendly
&& !( this.nameMapper instanceof FileSystemFriendly ) )
{
throw new IllegalArgumentException(
"Misconfiguration: FS friendly lock factory requires FS friendly name mapper"
);
}
}

public SyncContext newInstance( final RepositorySystemSession session, final boolean shared )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

import org.eclipse.aether.named.NamedLockFactory;
import org.eclipse.aether.named.providers.FileLockNamedLockFactory;
import org.eclipse.aether.named.providers.LocalReadWriteLockNamedLockFactory;
import org.eclipse.aether.named.providers.LocalSemaphoreNamedLockFactory;
import org.eclipse.aether.named.providers.NoopNamedLockFactory;
Expand Down Expand Up @@ -84,12 +85,14 @@ public SimpleNamedLockFactorySelector()
factories.put( NoopNamedLockFactory.NAME, new NoopNamedLockFactory() );
factories.put( LocalReadWriteLockNamedLockFactory.NAME, new LocalReadWriteLockNamedLockFactory() );
factories.put( LocalSemaphoreNamedLockFactory.NAME, new LocalSemaphoreNamedLockFactory() );
factories.put( FileLockNamedLockFactory.NAME, new FileLockNamedLockFactory() );
this.namedLockFactory = selectNamedLockFactory( factories, getFactoryName() );

Map<String, NameMapper> nameMappers = new HashMap<>();
nameMappers.put( StaticNameMapper.NAME, new StaticNameMapper() );
nameMappers.put( GAVNameMapper.NAME, new GAVNameMapper() );
nameMappers.put( DiscriminatingNameMapper.NAME, new DiscriminatingNameMapper( new GAVNameMapper() ) );
nameMappers.put( FileGAVNameMapper.NAME, new FileGAVNameMapper() );
this.nameMapper = selectNameMapper( nameMappers, getNameMapperName() );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@ For the `aether.syncContext.named.factory` property following values are allowed

- `rwlock-local` (default), uses JVM `ReentrantReadWriteLock` per lock name, usable for MT builds.
- `semaphore-local`, uses JVM `Semaphore` per lock name, usable for MT builds.
- `file-lock`, uses advisory file locking, usable for MP builds (must be used with `file-gav` name mapping).
- `noop`, implement no-op locking (no locking). For experimenting only. Has same functionality as old "nolock"
SyncContextFactory implementation.

For the `aether.syncContext.named.nameMapper` property following values are allowed:

- `discriminating` (default), uses hostname + local repo + GAV to create unique lock names for artifacts.
- `gav` uses GAV to create unique lock names for artifacts and metadata. Is not unique if multiple local repositories are involved.
- `file-gav` uses GAV and session to create absolute file paths (to be used with `file-lock` factory)
- `static` uses static (same) string as lock name for any input. Effectively providing functionality same as old
"global" locking SyncContextFactory.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.eclipse.aether.internal.impl.synccontext;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import org.eclipse.aether.internal.impl.synccontext.named.FileGAVNameMapper;
import org.eclipse.aether.named.providers.FileLockNamedLockFactory;
import org.junit.BeforeClass;

public class FileLockAdapterTest
extends NamedLockFactoryAdapterTestSupport
{
@BeforeClass
public static void createNamedLockFactory()
{
nameMapper = new FileGAVNameMapper();
namedLockFactory = new FileLockNamedLockFactory();
createAdapter();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package org.eclipse.aether.named.providers;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import javax.inject.Named;
import javax.inject.Singleton;

import org.eclipse.aether.named.support.FileLockNamedLock;
import org.eclipse.aether.named.support.FileSystemFriendly;
import org.eclipse.aether.named.support.NamedLockFactorySupport;
import org.eclipse.aether.named.support.NamedLockSupport;

/**
* Named locks factory of {@link FileLockNamedLock}s. This is a bit special implementation, as it
* expects locks names to be fully qualified absolute file system paths.
*
* @since TBD
*/
@Singleton
@Named( FileLockNamedLockFactory.NAME )
public class FileLockNamedLockFactory
extends NamedLockFactorySupport
implements FileSystemFriendly
{
public static final String NAME = "file-lock";

private final ConcurrentMap<String, FileChannel> fileChannels;

public FileLockNamedLockFactory()
{
this.fileChannels = new ConcurrentHashMap<>();
}

@Override
protected NamedLockSupport createLock( final String name )
{
Path path = Paths.get( name );
FileChannel fileChannel = fileChannels.computeIfAbsent( name, k ->
{
try
{
Files.createDirectories( path.getParent() );
return FileChannel.open(
path,
StandardOpenOption.READ, StandardOpenOption.WRITE,
StandardOpenOption.CREATE, StandardOpenOption.DELETE_ON_CLOSE
);
}
catch ( IOException e )
{
throw new UncheckedIOException( "Failed to open file channel for '"
+ name + "'", e );
}
} );
return new FileLockNamedLock( name, fileChannel, this );
}

@Override
protected void destroyLock( final String name )
{
FileChannel fileChannel = fileChannels.remove( name );
if ( fileChannel == null )
{
throw new IllegalStateException( "File channel expected, but does not exist: " + name );
}

try
{
fileChannel.close();
}
catch ( IOException e )
{
throw new UncheckedIOException( "Failed to close file channel for '"
+ name + "'", e );
}
}
}

0 comments on commit 50df10d

Please sign in to comment.