public abstract class Lock
extends java.lang.Object
| Modifier and Type | Field and Description |
|---|---|
protected java.time.LocalDateTime |
expiresAt
It should never be null(after acquisition), just acquired(after now) or expired(before now)
|
protected long |
leaseMillis |
protected LockKey |
lockKey |
protected LockService |
lockService |
static java.lang.String |
LOG_EXPIRED_TEMPLATE |
protected io.flamingock.internal.util.id.RunnerId |
owner |
protected long |
retryFrequencyMillis |
protected long |
stopTryingAfterMillis |
protected io.flamingock.internal.util.TimeService |
timeService |
| Constructor and Description |
|---|
Lock(io.flamingock.internal.util.id.RunnerId owner,
LockKey lockKey,
long leaseMillis,
long stopTryingAfterMillis,
long retryFrequencyMillis,
LockService lockService,
io.flamingock.internal.util.TimeService timeService,
boolean refreshDaemonEnabled) |
| Modifier and Type | Method and Description |
|---|---|
void |
ensure()
Ensures the lock is safely acquired(safely here means it's acquired with enough margin to operate),
or throws an exception otherwise.
|
java.time.LocalDateTime |
expiresAt() |
boolean |
extend()
Refreshes the lock if it's already taken.
|
protected void |
handleLockException(boolean acquiringLock,
java.time.Instant shouldStopTryingAt,
LockServiceException ex) |
boolean |
isExpired() |
boolean |
isReleased()
Whether
release() has been called on this Lock. |
void |
release()
This should be called once all the process guarded by the lock(those who assumed the lock is ensured) has finished.
|
void |
startDaemonIfEnabled()
Starts the lock refresh daemon if
refreshDaemonEnabled was set on this Lock and
one isn't already running for this instance. |
java.lang.String |
toString() |
protected void |
updateLease(long leaseMillis) |
protected void |
waitForLock(java.time.LocalDateTime expiresAt) |
public static final java.lang.String LOG_EXPIRED_TEMPLATE
protected final LockKey lockKey
protected final LockService lockService
protected final io.flamingock.internal.util.TimeService timeService
protected final io.flamingock.internal.util.id.RunnerId owner
protected final long leaseMillis
protected final long retryFrequencyMillis
protected final long stopTryingAfterMillis
protected volatile java.time.LocalDateTime expiresAt
public Lock(io.flamingock.internal.util.id.RunnerId owner,
LockKey lockKey,
long leaseMillis,
long stopTryingAfterMillis,
long retryFrequencyMillis,
LockService lockService,
io.flamingock.internal.util.TimeService timeService,
boolean refreshDaemonEnabled)
public final void ensure()
throws LockException
In case the lock is about to expire, it will try to refresh it. In this scenario, the lock won't be considered ensured until it's successfully extended. However, this scenario shouldn't happen, when a well configured daemon is set up.
LockException - if it cannot be ensured. Either is expired or, close to be expired and cannot be extended.public final boolean extend()
throws LockException
LockException - if there is any problem refreshing the lock or it's not acquired at all.public final void release()
public final boolean isReleased()
release() has been called on this Lock. Once true, never reverts.
Read by the refresh daemon to decide whether to keep refreshing.public java.time.LocalDateTime expiresAt()
protected final void updateLease(long leaseMillis)
public final boolean isExpired()
public final void startDaemonIfEnabled()
refreshDaemonEnabled was set on this Lock and
one isn't already running for this instance. Idempotent: subsequent calls while a daemon
is already alive are no-ops, so callers higher up in the planner/cloud-mapper code paths
cannot accidentally spawn parallel daemons for the same Lock. The daemon is bound to this
specific Lock instance and is interrupted by release() so it terminates promptly
when the lock's lifecycle ends.public java.lang.String toString()
toString in class java.lang.Objectprotected void handleLockException(boolean acquiringLock,
java.time.Instant shouldStopTryingAt,
LockServiceException ex)
protected void waitForLock(java.time.LocalDateTime expiresAt)