View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.named.providers;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import java.io.IOException;
25  import java.io.UncheckedIOException;
26  import java.net.URI;
27  import java.nio.channels.FileChannel;
28  import java.nio.file.Files;
29  import java.nio.file.Path;
30  import java.nio.file.Paths;
31  import java.nio.file.StandardOpenOption;
32  import java.util.concurrent.ConcurrentHashMap;
33  import java.util.concurrent.ConcurrentMap;
34  
35  import org.eclipse.aether.named.NamedLock;
36  import org.eclipse.aether.named.NamedLockKey;
37  import org.eclipse.aether.named.support.FileLockNamedLock;
38  import org.eclipse.aether.named.support.NamedLockFactorySupport;
39  import org.eclipse.aether.named.support.NamedLockSupport;
40  
41  import static org.eclipse.aether.named.support.Retry.retry;
42  
43  /**
44   * Named locks factory of {@link FileLockNamedLock}s. This is a bit of special implementation, as it
45   * expects locks names to be proper URI string representations (use {@code file:} protocol for default
46   * file system).
47   *
48   * @since 1.7.3
49   */
50  @Singleton
51  @Named(FileLockNamedLockFactory.NAME)
52  public class FileLockNamedLockFactory extends NamedLockFactorySupport {
53      public static final String NAME = "file-lock";
54  
55      // Logic borrowed from Commons-Lang3: we really need only this, to decide do we "delete on close" or not
56      private static final boolean IS_WINDOWS =
57              System.getProperty("os.name", "unknown").startsWith("Windows");
58  
59      /**
60       * Tweak: on Windows, the presence of <em>StandardOpenOption#DELETE_ON_CLOSE</em> causes concurrency issues. This
61       * flag allows to have it removed from effective flags, at the cost that lockfile directory becomes crowded
62       * with 0 byte sized lock files that are never cleaned up. Default value is {@code true} on non-Windows OS.
63       * See <a href="https://bugs.openjdk.org/browse/JDK-8252883">JDK-8252883</a> for Windows related bug. Users
64       * on Windows can still force "delete on close" by explicitly setting this property to {@code true}.
65       *
66       * @see <a href="https://bugs.openjdk.org/browse/JDK-8252883">JDK-8252883</a>
67       * @configurationSource {@link System#getProperty(String, String)}
68       * @configurationType {@link java.lang.Boolean}
69       * @configurationDefaultValue true
70       */
71      public static final String SYSTEM_PROP_DELETE_LOCK_FILES = "aether.named.file-lock.deleteLockFiles";
72  
73      private static final boolean DELETE_LOCK_FILES =
74              Boolean.parseBoolean(System.getProperty(SYSTEM_PROP_DELETE_LOCK_FILES, Boolean.toString(!IS_WINDOWS)));
75  
76      /**
77       * Tweak: on Windows, the presence of <em>StandardOpenOption#DELETE_ON_CLOSE</em> causes concurrency issues. This
78       * flag allows to implement similar fix as referenced JDK bug report: retry and hope the best. Default value is
79       * 5 attempts (will retry 4 times).
80       *
81       * @see <a href="https://bugs.openjdk.org/browse/JDK-8252883">JDK-8252883</a>
82       * @configurationSource {@link System#getProperty(String, String)}
83       * @configurationType {@link java.lang.Integer}
84       * @configurationDefaultValue 5
85       */
86      public static final String SYSTEM_PROP_ATTEMPTS = "aether.named.file-lock.attempts";
87  
88      private static final int ATTEMPTS = Integer.parseInt(System.getProperty(SYSTEM_PROP_ATTEMPTS, "5"));
89  
90      /**
91       * Tweak: When {@link #SYSTEM_PROP_ATTEMPTS} used, the amount of milliseconds to sleep between subsequent retries. Default
92       * value is 50 milliseconds.
93       *
94       * @configurationSource {@link System#getProperty(String, String)}
95       * @configurationType {@link java.lang.Long}
96       * @configurationDefaultValue 50
97       */
98      public static final String SYSTEM_PROP_SLEEP_MILLIS = "aether.named.file-lock.sleepMillis";
99  
100     private static final long SLEEP_MILLIS = Long.parseLong(System.getProperty(SYSTEM_PROP_SLEEP_MILLIS, "50"));
101 
102     private final ConcurrentMap<NamedLockKey, FileChannel> fileChannels;
103 
104     public FileLockNamedLockFactory() {
105         this.fileChannels = new ConcurrentHashMap<>();
106     }
107 
108     @Override
109     protected NamedLockSupport createLock(final NamedLockKey key) {
110         Path path = Paths.get(URI.create(key.name()));
111         FileChannel fileChannel = fileChannels.computeIfAbsent(key, k -> openFileChannel(key, path));
112         if (!fileChannel.isOpen()) {
113             // Channel was closed externally (I/O error, NFS hiccup, etc.). Evict the stale entry
114             // and open a fresh one. remove(key, fileChannel) is atomic: it only removes if the
115             // value is still this exact (stale) instance, avoiding races with other threads that
116             // may have already replaced it.
117             fileChannels.remove(key, fileChannel);
118             fileChannel = fileChannels.computeIfAbsent(key, k -> openFileChannel(key, path));
119         }
120         return new FileLockNamedLock(key, fileChannel, this);
121     }
122 
123     private FileChannel openFileChannel(NamedLockKey key, Path path) {
124         try {
125             Files.createDirectories(path.getParent());
126             FileChannel channel = retry(
127                     ATTEMPTS,
128                     SLEEP_MILLIS,
129                     () -> {
130                         if (DELETE_LOCK_FILES) {
131                             return FileChannel.open(
132                                     path,
133                                     StandardOpenOption.READ,
134                                     StandardOpenOption.WRITE,
135                                     StandardOpenOption.CREATE,
136                                     StandardOpenOption.DELETE_ON_CLOSE);
137                         } else {
138                             return FileChannel.open(
139                                     path, StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
140                         }
141                     },
142                     null,
143                     null);
144 
145             if (channel == null) {
146                 throw new IllegalStateException(
147                         "Could not open file channel for '" + key + "' after " + ATTEMPTS + " attempts; giving up");
148             }
149             return channel;
150         } catch (InterruptedException e) {
151             Thread.currentThread().interrupt();
152             throw new RuntimeException("Interrupted while opening file channel for '" + key + "'", e);
153         } catch (IOException e) {
154             throw new UncheckedIOException("Failed to open file channel for '" + key + "'", e);
155         }
156     }
157 
158     @Override
159     protected void destroyLock(final NamedLock namedLock) {
160         // Keep the FileChannel open in the fileChannels map for reuse by future createLock() calls.
161         // Opening a FileChannel is a syscall (open/creat) that shows up as a hotspot when locks are
162         // acquired and released frequently (e.g., per-artifact resolution in primed builds).
163         // Channels are closed on factory shutdown via doShutdown().
164     }
165 
166     @Override
167     protected void doShutdown() {
168         for (FileChannel channel : fileChannels.values()) {
169             try {
170                 channel.close();
171             } catch (IOException e) {
172                 logger.warn("Failed to close file channel", e);
173             }
174         }
175         fileChannels.clear();
176     }
177 }