public class MarkFile extends Object implements AutoCloseable
MarkFile is used to mark the presence of a running component and to track liveness.
The assumptions are: (1) the version field is an int in size, (2) the timestamp field is a long in size, and (3) the version field comes before the timestamp field.
| Constructor and Description |
|---|
MarkFile(File markFile,
boolean shouldPreExist,
int versionFieldOffset,
int timestampFieldOffset,
int totalFileLength,
long timeoutMs,
EpochClock epochClock,
IntConsumer versionCheck,
Consumer<String> logger)
Create a
MarkFile if none present. |
MarkFile(File directory,
String filename,
boolean warnIfDirectoryExists,
boolean dirDeleteOnStart,
int versionFieldOffset,
int timestampFieldOffset,
int totalFileLength,
long timeoutMs,
EpochClock epochClock,
IntConsumer versionCheck,
Consumer<String> logger)
Create a directory and mark file if none present.
|
MarkFile(File directory,
String filename,
int versionFieldOffset,
int timestampFieldOffset,
long timeoutMs,
EpochClock epochClock,
IntConsumer versionCheck,
Consumer<String> logger)
Map a pre-existing
MarkFile if one present and is active. |
MarkFile(MappedByteBuffer mappedBuffer,
int versionFieldOffset,
int timestampFieldOffset)
Manage a
MarkFile given a mapped file and offsets of version and timestamp. |
MarkFile(UnsafeBuffer buffer,
int versionFieldOffset,
int timestampFieldOffset)
Manage a
MarkFile given a buffer and offsets of version and timestamp. |
| Modifier and Type | Method and Description |
|---|---|
UnsafeBuffer |
buffer()
Returns the underlying
UnsafeBuffer. |
void |
close() |
void |
deleteDirectory(boolean ignoreFailures)
Delete parent directory.
|
static void |
ensureDirectoryExists(File directory,
String filename,
boolean warnIfDirectoryExists,
boolean dirDeleteOnStart,
int versionFieldOffset,
int timestampFieldOffset,
long timeoutMs,
EpochClock epochClock,
IntConsumer versionCheck,
Consumer<String> logger)
Ensure the directory exists, i.e.
|
static boolean |
isActive(MappedByteBuffer byteBuffer,
EpochClock epochClock,
long timeoutMs,
int versionFieldOffset,
int timestampFieldOffset,
IntConsumer versionCheck,
Consumer<String> logger)
Check if
MarkFile is active, i.e. |
boolean |
isClosed()
Checks if
MarkFile is closed. |
static MappedByteBuffer |
mapExistingFile(File markFile,
Consumer<String> logger,
long offset,
long length)
Map existing
MarkFile. |
static MappedByteBuffer |
mapExistingMarkFile(File markFile,
int versionFieldOffset,
int timestampFieldOffset,
long timeoutMs,
EpochClock epochClock,
IntConsumer versionCheck,
Consumer<String> logger)
Map existing
MarkFile. |
static MappedByteBuffer |
mapNewOrExistingMarkFile(File markFile,
boolean shouldPreExist,
int versionFieldOffset,
int timestampFieldOffset,
long totalFileLength,
long timeoutMs,
EpochClock epochClock,
IntConsumer versionCheck,
Consumer<String> logger)
Map new of existing
MarkFile. |
MappedByteBuffer |
mappedByteBuffer()
Returns the underlying
MappedByteBuffer. |
File |
markFile()
Returns
MarkFile. |
File |
parentDirectory()
Returns parent directory.
|
void |
signalReady(int version)
Perform an ordered put of the version field.
|
protected static void |
sleep(long durationMs)
Put thread to sleep for the given duration and restore interrupted status if thread is interrupted while
sleeping.
|
void |
timestampOrdered(long timestamp)
Set timestamp field using an ordered put.
|
long |
timestampVolatile()
Perform volatile read of the timestamp field.
|
long |
timestampWeak()
Perform weak/plain read of the timestamp field.
|
int |
versionVolatile()
Perform volatile read of the version field.
|
int |
versionWeak()
Perform weak/plain read of the version field.
|
static MappedByteBuffer |
waitForFileMapping(Consumer<String> logger,
File markFile,
long deadlineMs,
EpochClock epochClock)
Await the creation of the
MarkFile. |
public MarkFile(File directory, String filename, boolean warnIfDirectoryExists, boolean dirDeleteOnStart, int versionFieldOffset, int timestampFieldOffset, int totalFileLength, long timeoutMs, EpochClock epochClock, IntConsumer versionCheck, Consumer<String> logger)
Total length of Mark file will be mapped until close() is called.
directory - for the Mark file.filename - of the Mark file.warnIfDirectoryExists - for logging purposes.dirDeleteOnStart - if desired.versionFieldOffset - to use for version field access.timestampFieldOffset - to use for timestamp field access.totalFileLength - to allocate when creating new Mark file.timeoutMs - for the activity check (in milliseconds).epochClock - to use for time checks.versionCheck - to use for existing Mark file and version field.logger - to use to signal progress or null.public MarkFile(File markFile, boolean shouldPreExist, int versionFieldOffset, int timestampFieldOffset, int totalFileLength, long timeoutMs, EpochClock epochClock, IntConsumer versionCheck, Consumer<String> logger)
MarkFile if none present. Checking if an active MarkFile exists and is active.
Existing MarkFile is used if not active.
Total length of Mark file will be mapped until close() is called.
markFile - to use.shouldPreExist - or not.versionFieldOffset - to use for version field access.timestampFieldOffset - to use for timestamp field access.totalFileLength - to allocate when creating new MarkFile.timeoutMs - for the activity check (in milliseconds).epochClock - to use for time checks.versionCheck - to use for existing MarkFile and version field.logger - to use to signal progress or null.public MarkFile(File directory, String filename, int versionFieldOffset, int timestampFieldOffset, long timeoutMs, EpochClock epochClock, IntConsumer versionCheck, Consumer<String> logger)
MarkFile if one present and is active.
Total length of MarkFile will be mapped until close() is called.
directory - for the MarkFile file.filename - of the MarkFile file.versionFieldOffset - to use for version field access.timestampFieldOffset - to use for timestamp field access.timeoutMs - for the activity check (in milliseconds) and for how long to wait for file to exist.epochClock - to use for time checks.versionCheck - to use for existing MarkFile file and version field.logger - to use to signal progress or null.public MarkFile(MappedByteBuffer mappedBuffer, int versionFieldOffset, int timestampFieldOffset)
MarkFile given a mapped file and offsets of version and timestamp.
If mappedBuffer is not null, then it will be unmapped upon close().
mappedBuffer - for the MarkFile fields.versionFieldOffset - for the version field.timestampFieldOffset - for the timestamp field.public MarkFile(UnsafeBuffer buffer, int versionFieldOffset, int timestampFieldOffset)
MarkFile given a buffer and offsets of version and timestamp.buffer - for the MarkFile fieldsversionFieldOffset - for the version fieldtimestampFieldOffset - for the timestamp fieldpublic boolean isClosed()
MarkFile is closed.true if MarkFile is closed.public void close()
close in interface AutoCloseablepublic void signalReady(int version)
version - to be signaled.public int versionVolatile()
public int versionWeak()
public void timestampOrdered(long timestamp)
timestamp - to be set.public long timestampVolatile()
public long timestampWeak()
public void deleteDirectory(boolean ignoreFailures)
ignoreFailures - should the failures be silently ignored.public File parentDirectory()
public MappedByteBuffer mappedByteBuffer()
MappedByteBuffer.MappedByteBuffer.public UnsafeBuffer buffer()
UnsafeBuffer.UnsafeBuffer.public static void ensureDirectoryExists(File directory, String filename, boolean warnIfDirectoryExists, boolean dirDeleteOnStart, int versionFieldOffset, int timestampFieldOffset, long timeoutMs, EpochClock epochClock, IntConsumer versionCheck, Consumer<String> logger)
directory - to create.filename - of the MarkFile.warnIfDirectoryExists - should print warning if directory already exists.dirDeleteOnStart - should directory be deleted if it already exists. When the flag is set to
false the check will be made to see if the MarkFile is active.
Note: the directory will be deleted anyway even if the flag is false.
versionFieldOffset - offset of the version field.timestampFieldOffset - offset of the timestamp field.timeoutMs - timeout in milliseconds.epochClock - epoch clock.versionCheck - MarkFile version check function.logger - to use for reporting warnings.IllegalStateException - if MarkFile already exists and is active and
dirDeleteOnStart=false.public static MappedByteBuffer waitForFileMapping(Consumer<String> logger, File markFile, long deadlineMs, EpochClock epochClock)
MarkFile.logger - to use for warnings.markFile - the MarkFile.deadlineMs - deadline timeout in milliseconds.epochClock - epoch clock.MappedByteBuffer for the MarkFile.IllegalStateException - if deadline timeout is reached.public static MappedByteBuffer mapExistingMarkFile(File markFile, int versionFieldOffset, int timestampFieldOffset, long timeoutMs, EpochClock epochClock, IntConsumer versionCheck, Consumer<String> logger)
MarkFile.markFile - the MarkFile.versionFieldOffset - offset of the version field.timestampFieldOffset - offset of the timestamp field.timeoutMs - timeout in milliseconds.epochClock - epoch clock.versionCheck - version check function.logger - for the warnings.MappedByteBuffer for the MarkFile.IllegalStateException - if timeout is reached.IllegalStateException - if MarkFile has wrong size.public static MappedByteBuffer mapNewOrExistingMarkFile(File markFile, boolean shouldPreExist, int versionFieldOffset, int timestampFieldOffset, long totalFileLength, long timeoutMs, EpochClock epochClock, IntConsumer versionCheck, Consumer<String> logger)
MarkFile.markFile - the MarkFile.shouldPreExist - should MarkFile already exist.versionFieldOffset - offset of the version field.timestampFieldOffset - offset of the timestamp field.totalFileLength - total file length to be mapped.timeoutMs - timeout in milliseconds.epochClock - epoch clock.versionCheck - version check function.logger - for the warnings.MappedByteBuffer for the MarkFile.IllegalStateException - if timeout is reached.public static MappedByteBuffer mapExistingFile(File markFile, Consumer<String> logger, long offset, long length)
MarkFile.markFile - the MarkFile.logger - for the warnings.offset - offset to map at.length - to map.MappedByteBuffer for the MarkFile.public static boolean isActive(MappedByteBuffer byteBuffer, EpochClock epochClock, long timeoutMs, int versionFieldOffset, int timestampFieldOffset, IntConsumer versionCheck, Consumer<String> logger)
MarkFile is active, i.e. still in use.byteBuffer - the MappedByteBuffer.epochClock - epoch clock.timeoutMs - timeout in milliseconds.versionFieldOffset - offset of the version field.timestampFieldOffset - offset of the timestamp field.versionCheck - version check function.logger - for the warnings.true if MarkFile is active.protected static void sleep(long durationMs)
durationMs - sleep duration in milliseconds.Copyright © 2014-2022 Real Logic Limited. All Rights Reserved.