Skip to content

Commit b43bd65

Browse files
committed
finished rwlock
1 parent e3e429d commit b43bd65

File tree

5 files changed

+183
-47
lines changed

5 files changed

+183
-47
lines changed

coordination/src/main/java/tech/ydb/coordination/recipes/election/LeaderElection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ private void waitStartedState() throws InterruptedException {
277277
localState = state.get();
278278
}
279279

280-
if (localState == State.INITIAL || localState == State.CLOSED || localState == State.FAILED) {
280+
if (localState != State.STARTED) {
281281
throw new IllegalStateException("Unexpected state: " + localState.name());
282282
}
283283
}

coordination/src/main/java/tech/ydb/coordination/recipes/locks/InterProcessLock.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ boolean acquire(Duration waitDuration) throws Exception, LockAlreadyAcquiredExce
3636
* Releases the lock if it is held by this process.
3737
*
3838
* @return false if there was nothing to release
39-
* @throws InterruptedException if the thread is interrupted
39+
* @throws Exception if an unexpected error occurs
4040
* @throws LockReleaseFailedException if the lock release fails
4141
* @throws LockStateException if the lock is in invalid state for release
4242
*/
43-
boolean release() throws InterruptedException, LockReleaseFailedException, LockStateException;
43+
boolean release() throws Exception, LockReleaseFailedException, LockStateException;
4444

4545

4646
/**

coordination/src/main/java/tech/ydb/coordination/recipes/locks/InterProcessMutex.java

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.Closeable;
44
import java.time.Duration;
55
import java.time.Instant;
6+
import java.util.Objects;
67
import java.util.concurrent.ExecutionException;
78
import java.util.concurrent.Future;
89
import java.util.concurrent.atomic.AtomicReference;
@@ -24,12 +25,14 @@
2425
public class InterProcessMutex implements InterProcessLock, Closeable {
2526
private static final Logger logger = LoggerFactory.getLogger(InterProcessMutex.class);
2627

27-
private final AtomicReference<State> state = new AtomicReference<>(State.INITIAL);
28+
private final String lockName;
2829
private final CoordinationSession coordinationSession;
2930
private final Future<?> sessionConnectionTask;
3031
private final LockInternals lockInternals;
3132
private final ListenableContainer<CoordinationSession.State> sessionListenable;
3233

34+
private final AtomicReference<State> state = new AtomicReference<>(State.INITIAL);
35+
3336
/**
3437
* Internal state machine states
3538
*/
@@ -82,15 +85,14 @@ public InterProcessMutex(
8285
}
8386

8487
state.set(State.STARTING);
85-
logger.debug("Initializing InterProcessMutex for lock '{}'", lockName);
86-
88+
this.lockName = lockName;
8789
this.coordinationSession = client.createSession(coordinationNodePath);
8890
this.sessionListenable = new ListenableContainer<>();
8991
this.lockInternals = new LockInternals(coordinationSession, lockName);
9092

9193
coordinationSession.addStateListener(sessionState -> {
9294
if (sessionState == CoordinationSession.State.LOST || sessionState == CoordinationSession.State.CLOSED) {
93-
logger.error("Coordination session unexpectedly changed to {} state, marking mutex as FAILED",
95+
logger.error("Coordination session unexpectedly changed to '{}' state, marking mutex as 'FAILED'",
9496
sessionState);
9597
state.set(State.FAILED);
9698
}
@@ -109,9 +111,8 @@ public InterProcessMutex(
109111

110112
if (settings.isWaitConnection()) {
111113
try {
112-
logger.debug("Waiting for session connection to complete...");
114+
logger.debug("Waiting for session connection to complete for lock '{}'", lockName);
113115
sessionConnectionTask.get();
114-
logger.debug("Session connection completed");
115116
} catch (InterruptedException e) {
116117
Thread.currentThread().interrupt();
117118
logger.error("Interrupted while waiting for session connection for lock '{}'", lockName, e);
@@ -128,36 +129,38 @@ public InterProcessMutex(
128129
@Override
129130
public void acquire() throws Exception {
130131
checkState();
131-
logger.debug("Attempting to acquire lock...");
132+
logger.debug("Attempting to acquire lock '{}'", lockName);
132133
lockInternals.tryAcquire(
133134
null,
134135
true,
135136
null
136137
);
137-
logger.debug("Lock acquired successfully");
138+
logger.debug("Lock '{}' acquired successfully", lockName);
138139
}
139140

140141
@Override
141142
public boolean acquire(Duration waitDuration) throws Exception {
143+
Objects.requireNonNull(waitDuration, "wait duration must not be null");
144+
142145
checkState();
143-
logger.debug("Attempting to acquire lock with timeout {}...", waitDuration);
146+
logger.debug("Attempting to acquire lock '{}' with timeout {}", lockName, waitDuration);
144147
Instant deadline = Instant.now().plus(waitDuration);
145148
boolean acquired = lockInternals.tryAcquire(
146149
deadline,
147150
true,
148151
null
149152
) != null;
150-
logger.debug("Lock acquisition {}successful", acquired ? "" : "un");
153+
logger.debug("Lock '{}' acquisition {}successful", lockName, acquired ? "" : "un");
151154
return acquired;
152155
}
153156

154157
@Override
155158
public boolean release() throws InterruptedException {
156159
checkState();
157-
logger.debug("Attempting to release lock...");
160+
logger.debug("Attempting to release lock '{}'", lockName);
158161
boolean released = lockInternals.release();
159162
if (released) {
160-
logger.debug("Lock released successfully");
163+
logger.debug("Lock '{}' released successfully", lockName);
161164
} else {
162165
logger.debug("No lock to release");
163166
}
@@ -184,29 +187,27 @@ public Listenable<CoordinationSession.State> getSessionListenable() {
184187

185188
@Override
186189
public void close() {
187-
logger.debug("Closing InterProcessMutex...");
190+
logger.debug("Closing mutex '{}'", lockName);
188191
state.set(State.CLOSED);
189192
try {
190193
lockInternals.close();
191194
} catch (Exception e) {
192-
logger.warn("Error while closing lock internals", e);
195+
logger.warn("Error while closing lock internals '{}'", lockName, e);
193196
}
194-
logger.info("InterProcessMutex closed");
197+
logger.info("Mutex '{}' closed", lockName);
195198
}
196199

197200
private void checkState() throws LockStateException {
198201
State currentState = state.get();
199202
if (currentState == State.FAILED) {
200-
throw new LockStateException("Lock '" + lockInternals.getLockName() + "' is in FAILED state",
201-
lockInternals.getLockName());
203+
throw new LockStateException("Lock '" + lockName + "' is in FAILED state", lockName);
202204
}
203205
if (currentState == State.CLOSED) {
204-
throw new LockStateException("Lock '" + lockInternals.getLockName() + "' is already closed",
205-
lockInternals.getLockName());
206+
throw new LockStateException("Lock '" + lockName + "' is already closed", lockName);
206207
}
207208
if (currentState != State.STARTED) {
208-
throw new LockStateException("Lock '" + lockInternals.getLockName() + "' is not ready (current state: "
209-
+ currentState + ")", lockInternals.getLockName());
209+
throw new LockStateException("Lock '" + lockName + "' is not ready (current state: " + currentState + ")",
210+
lockName);
210211
}
211212
}
212213
}

0 commit comments

Comments
 (0)