Class Underlock
git processes.-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionvoidedit(IoBiConsumer<? super BufferedReader, ? super Writer> mutator, boolean withBackup) Edits the file, complete with locking to prevent concurrent edits by other cluster nodes orgitprocesses.voideditRaw(IoBiConsumer<? super InputStream, ? super OutputStream> mutator, boolean withBackup) Edits the file, complete with locking to prevent concurrent edits by other cluster nodes orgitprocesses.static Underlockstatic Underlocklock()Acquires a lock on the file, returning aUnderlock.LockedFilethat can be used to release the lock.voidlockIndefinitely(String content) Checks whether the lock is currently held, and if not, creates the lock file with the providedcontentas the file content.voidReleases apreviously acquired lock, including stale lock files, or locks that were created throughlockIndefinitely(String).voidreleaseIfNotHeld(Runnable cleanupTask) Releases apreviously acquired lock, including stale lock files, or locks that were created throughlockIndefinitely(String).voidwrite(ReadableByteChannel source) Acquires a lock on the file, and then writes the contents of the providedReadableByteChannelto it.
-
Method Details
-
forFile
-
forFile
-
edit
public void edit(IoBiConsumer<? super BufferedReader, ? super Writer> mutator, boolean withBackup) throws IOException, OverlappingFileLockExceptionEdits the file, complete with locking to prevent concurrent edits by other cluster nodes orgitprocesses.If the file already exists, the
mutatorreceives anReaderfor it. Otherwise, the first argument will benull. Themutatoralways receives anWriter, for emitting the new contents.The primary use case for this method is guarding an edit with a lock.
- Parameters:
mutator- a function to use for mutating the filewithBackup- whether the old version of the file should be saved with a.bakextension- Throws:
IOException- if themutatorfails, or if the file cannot be lockedOverlappingFileLockException- if the file can't be locked because it has already been locked
-
editRaw
public void editRaw(IoBiConsumer<? super InputStream, ? super OutputStream> mutator, boolean withBackup) throws IOExceptionEdits the file, complete with locking to prevent concurrent edits by other cluster nodes orgitprocesses.If the file already exists, the
mutatorreceives anInputStreamfor it. Otherwise, the first argument will benull. Themutatoralways receives anOutputStream, for emitting the new contents.The primary use case for this method is guarding an edit with a lock.
- Parameters:
mutator- a function to use for mutating the filewithBackup- whether the old version of the file should be saved with a.bakextension- Throws:
IOException- if themutatorfails, or if the file cannot be lockedOverlappingFileLockException- if the file can't be locked because it has already been locked
-
lock
Acquires a lock on the file, returning aUnderlock.LockedFilethat can be used to release the lock.- Returns:
- a
Underlock.LockedFilethat when closed, releases the lock - Throws:
IOException- if an I/O error occurred when acquiring the lockOverlappingFileLockException- if the lock could not be acquired within the specified timeout
-
lockIndefinitely
public void lockIndefinitely(@Nullable String content) throws IOException, OverlappingFileLockException Checks whether the lock is currently held, and if not, creates the lock file with the providedcontentas the file content.The effect of this operation is that any attempt to acquire an exclusive lock or edit the target file under lock will fail, which is useful for situations where the lock should survive node restarts, (e.g. pausing of garbage collection).
- Throws:
IOException- if an I/O error occurred when acquiring the lockOverlappingFileLockException- if the lock could not be acquired within the specified timeout
-
releaseIfNotHeld
Releases apreviously acquired lock, including stale lock files, or locks that were created throughlockIndefinitely(String). If the lock file does not exist, nothing happens.- Throws:
IOException- if the lock cannot be releasedOverlappingFileLockException- if another process or thread is actively holding the lock
-
releaseIfNotHeld
Releases apreviously acquired lock, including stale lock files, or locks that were created throughlockIndefinitely(String). If the lock is successfully released, the provided cleanup task is run. If the lock file does not exist the cleanup task is not run and nothing happens.- Throws:
IOException- if the lock cannot be releasedOverlappingFileLockException- if another process or thread is actively holding the lock
-
write
Acquires a lock on the file, and then writes the contents of the providedReadableByteChannelto it. Contents are written to the lock first and then renamed into place to produce an (effectively) atomic write even on NFS.- Parameters:
source- the contents to write- Throws:
IOException- if the file cannot be locked, or the contents cannot be writtenOverlappingFileLockException- if the file can't be locked because it has already been locked
-