| 1 |
${esc.hash} Named Locks |
| 2 |
|
| 3 |
<!-- |
| 4 |
Licensed to the Apache Software Foundation (ASF) under one |
| 5 |
or more contributor license agreements. See the NOTICE file |
| 6 |
distributed with this work for additional information |
| 7 |
regarding copyright ownership. The ASF licenses this file |
| 8 |
to you under the Apache License, Version 2.0 (the |
| 9 |
"License"); you may not use this file except in compliance |
| 10 |
with the License. You may obtain a copy of the License at |
| 11 |
|
| 12 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
|
| 14 |
Unless required by applicable law or agreed to in writing, |
| 15 |
software distributed under the License is distributed on an |
| 16 |
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 |
KIND, either express or implied. See the License for the |
| 18 |
specific language governing permissions and limitations |
| 19 |
under the License. |
| 20 |
--> |
| 21 |
|
| 22 |
Named locks are essentially locks that are assigned to some given (opaque) ID. If you work with multiple |
| 23 |
resources that each can have unique ID assigned (i.e., file with an absolute path, some entities with unique ID), |
| 24 |
then you can use named locks to make sure they are being protected from concurrent read and write actions. |
| 25 |
|
| 26 |
The building block for named locks is `org.eclipse.aether.named.NamedLock` that is acquired from factory |
| 27 |
`org.eclipse.aether.named.NamedLockFactory` for given "name". Two threads (or processes, or even distributed |
| 28 |
processes, depending on backing implementation) that use same "name" can then coordinate each other by acquiring |
| 29 |
"shared" or "exclusive" access to resource mapped to "name". Named locks treat "name" just as an opaque identifier. |
| 30 |
The design attempts for similarity with existing Java constructs like `ReentrantReadWriteLock`, |
| 31 |
and share similar properties as well. |
| 32 |
|
| 33 |
Named locks properties: |
| 34 |
* Reentrant: named locks allow you to reacquire same (shared or exclusive) lock you already have. |
| 35 |
* Lock downgrading: named locks allow "lock downgrading", but semantically this results in no-op (as thread already owns |
| 36 |
exclusive lock). |
| 37 |
* Lock upgrading: is not supported and implementations will "fail fast" (throw runtime exception) for code that attempts |
| 38 |
to perform it. If required, proposed alternative is [DCL](https://en.wikipedia.org/wiki/Double-checked_locking). |
| 39 |
* Interruption of acquisition: named locks support interruption during acquisition. |
| 40 |
|
| 41 |
Named locks special properties: |
| 42 |
* Coordination happens on Thread level (always) and goes all way up to processes or distributed processes (backing |
| 43 |
implementation dependant) |
| 44 |
* Named locks can be JVM local only, host wide (file locking) or even fully distributed (Hazelcast, Redisson). |
| 45 |
|
| 46 |
Named locks provide support classes for implementations, and provide out of the box several lock and name mapper implementations. |
| 47 |
|
| 48 |
Following implementations are "local" (local to JVM) named lock implementations: |
| 49 |
|
| 50 |
- `rwlock-local` implemented in `org.eclipse.aether.named.providers.LocalReadWriteLockNamedLockFactory` that uses |
| 51 |
JVM `java.util.concurrent.locks.ReentrantReadWriteLock`. |
| 52 |
- `semaphore-local` implemented in `org.eclipse.aether.named.providers.LocalSemaphoreNamedLockFactory` that uses |
| 53 |
JVM `java.util.concurrent.Semaphore`. |
| 54 |
- `noop` implemented in `org.eclipse.aether.named.providers.NoopNamedLockFactory` that uses no locking. |
| 55 |
|
| 56 |
Note about "local" locks: they are in-JVM, in a way, they properly coordinate in case of multithreaded access from |
| 57 |
same JVM, but do not cover accesses across multiple processes and/or multiple hosts access. |
| 58 |
In other words, local named locks are only suited within one JVM with a multithreaded access. |
| 59 |
|
| 60 |
Following named lock implementations use underlying file system advisory file locking: |
| 61 |
|
| 62 |
- `file-lock` implemented in `org.eclipse.aether.named.providers.FileLockNamedLockFactory` that uses |
| 63 |
JVM `java.nio.channels.FileLock`. |
| 64 |
|
| 65 |
The `file-lock` implementation uses file system advisory file locking, hence, concurrently running Maven processes |
| 66 |
set up to use `file-lock` implementation can safely share one local repository. This is almost certain on |
| 67 |
local file systems across all operating systems. In case of NFS mounts, file advisory locking MAY work if NFSv4+ |
| 68 |
used with complete setup (with all the necessary services like `RPC` and `portmapper` needed to implement NFS advisory |
| 69 |
file locking, check your NFS and/or OS manuals for details). In short: if your (local or remote) FS correctly support |
| 70 |
and implements advisory locking, it should work. Local FS usually does, while with NFS your mileage may vary. |
| 71 |
|
| 72 |
Finally, "distributed" named lock implementations are the following (separate modules which require additional dependencies and remote services): |
| 73 |
|
| 74 |
- `rwlock-redisson` implemented in `org.eclipse.aether.named.redisson.RedissonReadWriteLockNamedLockFactory`. |
| 75 |
- `semaphore-redisson` implemented in `org.eclipse.aether.named.redisson.RedissonSemaphoreNamedLockFactory`. |
| 76 |
- `semaphore-hazelcast-client` implemented in `org.eclipse.aether.named.hazelcast.HazelcastClientCPSemaphoreNamedLockFactory`. |
| 77 |
- `semaphore-hazelcast` implemented in `org.eclipse.aether.named.hazelcast.HazelcastCPSemaphoreNamedLockFactory`. |
| 78 |
|
| 79 |
Sharing a local repository between multiple hosts (i.e., on a busy CI server) may be best done with one of distributed named lock, |
| 80 |
if NFS locking is not working for you. |
| 81 |
|
| 82 |
The aforementioned (opaque) IDs need to be mapped from artifacts and metadata. |
| 83 |
|
| 84 |
Out of the box, name mapper implementations are the following: |
| 85 |
|
| 86 |
- `static` implemented in `org.eclipse.aether.internal.impl.synccontext.named.StaticNameMapper`. |
| 87 |
- `gav` implemented in `org.eclipse.aether.internal.impl.synccontext.named.GAVNameMapper`. |
| 88 |
- `gaecv` implemented in `org.eclipse.aether.internal.impl.synccontext.named.GAECVNameMapper`. |
| 89 |
- `discriminating` implemented in `org.eclipse.aether.internal.impl.synccontext.named.DiscriminatingNameMapper`. |
| 90 |
- `file-gav` implemented in `org.eclipse.aether.internal.impl.synccontext.named.FileGAVNameMapper`. |
| 91 |
- `file-gaecv` implemented in `org.eclipse.aether.internal.impl.synccontext.named.FileGAECVNameMapper`. |
| 92 |
- `file-hgav` implemented in `org.eclipse.aether.internal.impl.synccontext.named.FileHGAVNameMapper`. |
| 93 |
- `file-hgaecv` implemented in `org.eclipse.aether.internal.impl.synccontext.named.FileHGAECVNameMapper`. |
| 94 |
- `file-static` implemented in `org.eclipse.aether.internal.impl.synccontext.named.FileStaticNameMapper`. |
| 95 |
|
| 96 |
Note: the `file-*` name mapper MUST be used with `file-lock` named locking, no other mapper will work with it. |
| 97 |
|
| 98 |
${esc.hash}${esc.hash} Diagnostic collection in case of failures |
| 99 |
|
| 100 |
If you experience locking failures, to get access to full dump (may be huge in case of big builds) on failure, |
| 101 |
enable lock diagnostic collection. When enabled, "diagnostic dump" will be emitted by factory |
| 102 |
to INFO log (console by default). Please note, that enabling diagnostic dump may increase heap requirement (as |
| 103 |
diagnostic is collected in memory). |
| 104 |
|
| 105 |
To enable diagnostic collection, set Java System Property `aether.named.diagnostic.enabled` to `true`. |
| 106 |
|
| 107 |
If diagnostic collection is not enabled, Resolver will "just" fail, telling about failed lock, but not reveal any |
| 108 |
information about other active locks and threads. |