001package org.hl7.fhir.utilities.npm;
002
003import lombok.Getter;
004import lombok.With;
005import lombok.extern.slf4j.Slf4j;
006import org.hl7.fhir.utilities.Utilities;
007import org.hl7.fhir.utilities.filesystem.ManagedFileAccess;
008
009import javax.annotation.Nonnull;
010import javax.annotation.Nullable;
011import java.io.File;
012import java.io.IOException;
013import java.io.RandomAccessFile;
014import java.nio.ByteBuffer;
015import java.nio.channels.FileChannel;
016import java.nio.channels.FileLock;
017import java.nio.charset.StandardCharsets;
018import java.nio.file.*;
019import java.util.concurrent.ConcurrentHashMap;
020import java.util.concurrent.TimeUnit;
021import java.util.concurrent.TimeoutException;
022import java.util.concurrent.locks.ReadWriteLock;
023import java.util.concurrent.locks.ReentrantReadWriteLock;
024
025@Slf4j
026public class FilesystemPackageCacheManagerLocks {
027
028  private static final ConcurrentHashMap<File, FilesystemPackageCacheManagerLocks> cacheFolderLockManagers = new ConcurrentHashMap<>();
029
030  @Getter
031  private final CacheLock cacheLock = new CacheLock();
032
033  private final ConcurrentHashMap<File, PackageLock> packageLocks = new ConcurrentHashMap<>();
034
035  private final File cacheFolder;
036
037  private static final LockParameters lockParameters = new LockParameters();
038
039  public static class LockParameters {
040    @Getter @With
041    private final long lockTimeoutTime;
042    @Getter @With
043    private final TimeUnit lockTimeoutTimeUnit;
044
045    public LockParameters() {
046      this(60L, TimeUnit.SECONDS);
047    }
048
049    public LockParameters(long lockTimeoutTime, TimeUnit lockTimeoutTimeUnit) {
050      this.lockTimeoutTime = lockTimeoutTime;
051      this.lockTimeoutTimeUnit = lockTimeoutTimeUnit;
052    }
053  }
054
055  /**
056   * This method is intended to be used only for testing purposes.
057   * <p/>
058   * To ensure that only one instance of the FilesystemPackageCacheManagerLocks is created for a given cacheFolder, use
059   * the static org.hl7.fhir.utilities.npm.FilesystemPackageCacheManagerLocks#getFilesystemPackageCacheManagerLocks(java.io.File) method.
060   * <p/>
061   * Get all the locks necessary to manage a filesystem cache.
062   *
063   * @param cacheFolder
064   * @throws IOException
065   */
066  public FilesystemPackageCacheManagerLocks(File cacheFolder) throws IOException {
067    this.cacheFolder = cacheFolder;
068  }
069
070
071  /**
072   * Returns a single FilesystemPackageCacheManagerLocks instance for the given cacheFolder.
073   * <p/>
074   * If an instance already exists, it is returned. Otherwise, a new instance is created.
075   * <p/>
076   * Using this method ensures that only one instance of FilesystemPackageCacheManagerLocks is created for a given
077   * cacheFolder, which is useful if multiple ValidationEngine instances are running in parallel.
078   *
079   * @param cacheFolder
080   * @return
081   * @throws IOException
082   */
083  public static FilesystemPackageCacheManagerLocks getFilesystemPackageCacheManagerLocks(File cacheFolder) throws IOException {
084    return cacheFolderLockManagers.computeIfAbsent(cacheFolder, k -> {
085      try {
086        return new FilesystemPackageCacheManagerLocks(k);
087      } catch (IOException e) {
088        throw new RuntimeException(e);
089      }
090    });
091  }
092
093  public class CacheLock {
094    private final ReadWriteLock lock;
095
096    protected CacheLock() {
097      lock = new ReentrantReadWriteLock();
098    }
099
100    public ReadWriteLock getLock() {
101      return lock;
102    }
103
104    public <T> T doWriteWithLock(FilesystemPackageCacheManager.CacheLockFunction<T> f) throws IOException {
105      lock.writeLock().lock();
106      T result = null;
107      try {
108        result = f.get();
109      } finally {
110        lock.writeLock().unlock();
111      }
112      return result;
113    }
114
115    public boolean canLockFileBeHeldByThisProcess(File lockFile) throws IOException {
116      return doWriteWithLock(() -> {
117      try (FileChannel channel = new RandomAccessFile(lockFile, "rw").getChannel()) {
118        FileLock fileLock = channel.tryLock(0, Long.MAX_VALUE, false);
119        if (fileLock != null) {
120          fileLock.release();
121          channel.close();
122          return true;
123        }
124      }
125      return false;});
126    }
127  }
128
129  public class PackageLock {
130    @Getter
131    private final File lockFile;
132    private final ReadWriteLock lock;
133
134    protected PackageLock(File lockFile, ReadWriteLock lock) {
135      this.lockFile = lockFile;
136      this.lock = lock;
137    }
138
139    private void checkForLockFileWaitForDeleteIfExists(File lockFile, @Nonnull LockParameters lockParameters) throws IOException {
140      if (!lockFile.exists()) {
141        return;
142      }
143
144      // Check if the file is locked by a process. If it is not, it is likely an incomplete package cache install, and
145      // we should throw an exception.
146      if (lockFile.isFile()) {
147        try (FileChannel channel = new RandomAccessFile(lockFile, "rw").getChannel()) {
148          FileLock fileLock = channel.tryLock(0, Long.MAX_VALUE, false);
149          if (fileLock != null) {
150            fileLock.release();
151            channel.close();
152            throw new IOException("Lock file exists, but is not locked by a process: " + lockFile.getName());
153          }
154          log.debug("File is locked ('"+lockFile.getAbsolutePath()+"').");
155        }
156      }
157      try {
158        waitForLockFileDeletion(lockFile, lockParameters);
159      } catch (InterruptedException e) {
160        Thread.currentThread().interrupt();
161        throw new IOException("Thread interrupted while waiting for lock", e);
162      }
163    }
164
165    /*
166     Wait for the lock file to be deleted. If the lock file is not deleted within the timeout or if the thread is
167     interrupted, an IOException is thrown.
168     */
169    private void waitForLockFileDeletion(File lockFile, @Nonnull LockParameters lockParameters) throws IOException, InterruptedException {
170
171      try (WatchService watchService = FileSystems.getDefault().newWatchService()) {
172        Path dir = lockFile.getParentFile().toPath();
173        dir.register(watchService, StandardWatchEventKinds.ENTRY_DELETE);
174
175        WatchKey key = watchService.poll(lockParameters.lockTimeoutTime, lockParameters.lockTimeoutTimeUnit);
176        if (key == null) {
177          // It is possible that the lock file is deleted before the watch service is registered, so if we timeout at
178          // this point, we should check if the lock file still exists.
179          if (lockFile.exists()) {
180            throw new TimeoutException("Timeout waiting for lock file deletion: " + lockFile.getName());
181          }
182        } else {
183          for (WatchEvent<?> event : key.pollEvents()) {
184            WatchEvent.Kind<?> kind = event.kind();
185            if (kind == StandardWatchEventKinds.ENTRY_DELETE) {
186              Path deletedFilePath = (Path) event.context();
187              if (deletedFilePath.toString().equals(lockFile.getName())) {
188                return;
189              }
190            }
191            key.reset();
192          }
193        }
194      } catch (TimeoutException e) {
195        throw new IOException("Package cache timed out waiting for lock.", e);
196      }
197    }
198
199
200    /**
201     * Wraps the execution of a package read function with the appropriate cache, package, and .lock file locking and
202     * checks.
203     *
204     * @param function The function to execute
205     * @param lockParameters The parameters for the lock
206     * @return The return of the function
207     * @param <T> The return type of the function
208     * @throws IOException If an error occurs while managing locking.
209     */
210    public <T> T doReadWithLock(FilesystemPackageCacheManager.CacheLockFunction<T> function, @Nullable LockParameters lockParameters) throws IOException {
211      final LockParameters resolvedLockParameters = lockParameters != null ? lockParameters : FilesystemPackageCacheManagerLocks.lockParameters;
212
213      cacheLock.getLock().readLock().lock();
214      lock.readLock().lock();
215
216      checkForLockFileWaitForDeleteIfExists(lockFile, resolvedLockParameters);
217
218      T result = null;
219      try {
220        result = function.get();
221      } finally {
222        lock.readLock().unlock();
223        cacheLock.getLock().readLock().unlock();
224      }
225      return result;
226    }
227
228    /**
229     * Wraps the execution of a package write function with the appropriate cache, package, and .lock file locking and
230     * checks.
231     *
232     * @param function The function to execute
233     * @param lockParameters The parameters for the lock
234     * @return The return of the function
235     * @param <T> The return type of the function
236     * @throws IOException If an error occurs while managing locking.
237     */
238    public <T> T doWriteWithLock(FilesystemPackageCacheManager.CacheLockFunction<T> function, @Nullable LockParameters lockParameters) throws IOException {
239      final LockParameters resolvedLockParameters = lockParameters != null ? lockParameters : FilesystemPackageCacheManagerLocks.lockParameters;
240
241      cacheLock.getLock().writeLock().lock();
242      lock.writeLock().lock();
243
244       /*TODO Eventually, this logic should exist in a Lockfile class so that it isn't duplicated between the main code and
245          the test code.
246        */
247      try (FileChannel channel = new RandomAccessFile(lockFile, "rw").getChannel()) {
248
249        FileLock fileLock = channel.tryLock(0, Long.MAX_VALUE, false);
250
251        if (fileLock == null) {
252          waitForLockFileDeletion(lockFile, resolvedLockParameters);
253          fileLock = channel.tryLock(0, Long.MAX_VALUE, false);
254        }
255        if (fileLock == null) {
256          throw new IOException("Failed to acquire lock on file: " + lockFile.getName());
257        }
258
259        if (!lockFile.isFile()) {
260          final ByteBuffer buff = ByteBuffer.wrap(String.valueOf(ProcessHandle.current().pid()).getBytes(StandardCharsets.UTF_8));
261          channel.write(buff);
262        }
263        T result = null;
264        try {
265          result = function.get();
266        } finally {
267
268          lockFile.renameTo(ManagedFileAccess.file(File.createTempFile(lockFile.getName(), ".lock-renamed").getAbsolutePath()));
269
270          fileLock.release();
271          channel.close();
272
273          if (!lockFile.delete()) {
274            lockFile.deleteOnExit();
275          }
276
277          lock.writeLock().unlock();
278          cacheLock.getLock().writeLock().unlock();
279        }
280        return result;
281      } catch (InterruptedException e) {
282        Thread.currentThread().interrupt();
283        throw new IOException("Thread interrupted while waiting for lock", e);
284      }
285    }
286  }
287
288  public synchronized PackageLock getPackageLock(String packageName) throws IOException {
289    File lockFile = ManagedFileAccess.file(Utilities.path(cacheFolder.getAbsolutePath(), packageName + ".lock"));
290    return packageLocks.computeIfAbsent(lockFile, (k) -> new PackageLock(k, new ReentrantReadWriteLock()));
291  }
292}