Skip to content

Commit abdaafd

Browse files
committed
Optimized code base on code reviews.
1 parent 032b7d7 commit abdaafd

File tree

2 files changed

+9
-16
lines changed
  • dolphinscheduler-registry/dolphinscheduler-registry-plugins
    • dolphinscheduler-registry-etcd/src/main/java/org/apache/dolphinscheduler/plugin/registry/etcd
    • dolphinscheduler-registry-jdbc/src/main/java/org/apache/dolphinscheduler/plugin/registry/jdbc/server

2 files changed

+9
-16
lines changed

dolphinscheduler-registry/dolphinscheduler-registry-plugins/dolphinscheduler-registry-etcd/src/main/java/org/apache/dolphinscheduler/plugin/registry/etcd/EtcdRegistry.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ public boolean acquireLock(String lockKey) {
315315
lockClient.lock(byteSequence(lockKey), leaseId).get();
316316

317317
// save the leaseId for release Lock
318-
lockMap.put(lockKey, new LockEntry(leaseId));
318+
threadHeldLocks.put(lockKey, new LockEntry(leaseId));
319319
return true;
320320
} catch (InterruptedException e) {
321321
Thread.currentThread().interrupt();
@@ -325,23 +325,16 @@ public boolean acquireLock(String lockKey) {
325325
}
326326
}
327327

328-
private static boolean currentThreadIsReentrant(String lockKey, Map<String, LockEntry> lockEntryMap) {
329-
LockEntry lockEntry = lockEntryMap.get(lockKey);
330-
if (lockEntry != null) {
331-
lockEntry.lockCount.incrementAndGet();
332-
return true;
333-
}
334-
return false;
335328
private static boolean acquireBasedOnThreadHeldLocks(String lockKey, Map<String, LockEntry> threadHeldLocks) {
336329
LockEntry lockEntry = threadHeldLocks.get(lockKey);
337330
if (lockEntry != null) {
338331
lockEntry.lockCount.incrementAndGet();
339332
return true;
340-
}
333+
}
341334
return false;
342335
}
343336

344-
private static Map<String, LockEntry> getThreadLocks() {
337+
private static Map<String, LockEntry> getThreadHeldLocks() {
345338
Map<String, LockEntry> lockEntryMap = threadLocalLockMap.get();
346339
if (null == lockEntryMap) {
347340
lockEntryMap = new HashMap<>();
@@ -352,8 +345,8 @@ private static Map<String, LockEntry> getThreadLocks() {
352345

353346
@Override
354347
public boolean acquireLock(String key, long timeout) {
355-
Map<String, LockEntry> lockEntryMap = getLockMapFromThreadLocal();
356-
if (currentThreadIsReentrant(key, lockEntryMap)) {
348+
Map<String, LockEntry> threadHeldLocks = getThreadHeldLocks();
349+
if (acquireBasedOnThreadHeldLocks(key, threadHeldLocks)) {
357350
return true;
358351
}
359352

@@ -368,7 +361,7 @@ public boolean acquireLock(String key, long timeout) {
368361
}));
369362

370363
// save the leaseId for release Lock
371-
lockEntryMap.put(key, new LockEntry(leaseId));
364+
threadHeldLocks.put(key, new LockEntry(leaseId));
372365
return true;
373366
} catch (TimeoutException timeoutException) {
374367
log.debug("Acquire lock: {} in {}/ms timeout", key, timeout);

dolphinscheduler-registry/dolphinscheduler-registry-plugins/dolphinscheduler-registry-jdbc/src/main/java/org/apache/dolphinscheduler/plugin/registry/jdbc/server/JdbcRegistryLockManager.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public JdbcRegistryLockManager(JdbcRegistryProperties jdbcRegistryProperties,
5555
public void acquireJdbcRegistryLock(Long clientId, String lockKey) {
5656
String lockOwner = LockUtils.getLockOwner();
5757
while (true) {
58-
if (currentThreadIsReentrant(lockKey, lockOwner)) {
58+
if (tryReenterLock(lockKey, lockOwner)) {
5959
return;
6060
}
6161
JdbcRegistryLockDTO jdbcRegistryLock = JdbcRegistryLockDTO.builder()
@@ -87,7 +87,7 @@ public void acquireJdbcRegistryLock(Long clientId, String lockKey) {
8787

8888
private boolean tryReenterLock(String lockKey, String lockAcquirer) {
8989
LockEntry lockEntry = jdbcRegistryLockHolderMap.get(lockKey);
90-
if (lockEntry != null && lockOwner.equals(lockEntry.getLockOwner())) {
90+
if (lockEntry != null && lockAcquirer.equals(lockEntry.getLockOwner())) {
9191
lockEntry.lockCount.incrementAndGet();
9292
return true;
9393
}
@@ -99,7 +99,7 @@ public boolean acquireJdbcRegistryLock(Long clientId, String lockKey, long timeo
9999
String lockOwner = LockUtils.getLockOwner();
100100
long start = System.currentTimeMillis();
101101
while (System.currentTimeMillis() - start <= timeout) {
102-
if (currentThreadIsReentrant(lockKey, lockOwner)) {
102+
if (tryReenterLock(lockKey, lockOwner)) {
103103
return true;
104104
}
105105
JdbcRegistryLockDTO jdbcRegistryLock = JdbcRegistryLockDTO.builder()

0 commit comments

Comments
 (0)