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