Skip to content

Commit 06b4894

Browse files
kelvenKelven Yang
authored andcommitted
Use double check pattern to avoid holding two locks in GlobalLock.java while trying to perserve intern semantic of global lock
1 parent 4e10a97 commit 06b4894

2 files changed

Lines changed: 42 additions & 31 deletions

File tree

‎utils/src/com/cloud/utils/db/DbUtil.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ public static boolean releaseGlobalLock(String name) {
249249
Connection conn = getConnectionForGlobalLocks(name, false);
250250
if(conn == null) {
251251
s_logger.error("Unable to acquire DB connection for global lock system");
252+
assert(false);
252253
return false;
253254
}
254255

‎utils/src/com/cloud/utils/db/GlobalLock.java‎

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,20 @@ public int addRef() {
6767
public int releaseRef() {
6868
int refCount;
6969

70-
synchronized(s_lockMap) { // // lock in sequence to prevent deadlock
71-
synchronized(this) {
72-
referenceCount--;
73-
refCount = referenceCount;
74-
75-
if(referenceCount < 0)
76-
s_logger.warn("Unmatched Global lock " + name + " reference usage detected, check your code!");
77-
78-
if(referenceCount == 0)
79-
releaseInternLock(name);
80-
}
81-
}
70+
boolean needToRemove = false;
71+
synchronized(this) {
72+
referenceCount--;
73+
refCount = referenceCount;
74+
75+
if(referenceCount < 0)
76+
s_logger.warn("Unmatched Global lock " + name + " reference usage detected, check your code!");
77+
78+
if(referenceCount == 0)
79+
needToRemove = true;
80+
}
81+
82+
if(needToRemove)
83+
releaseInternLock(name);
8284

8385
return refCount;
8486
}
@@ -99,8 +101,12 @@ public static GlobalLock getInternLock(String name) {
99101
}
100102

101103
private static void releaseInternLock(String name) {
102-
synchronized(s_lockMap) {
103-
s_lockMap.remove(name);
104+
synchronized(s_lockMap) {
105+
GlobalLock lock = s_lockMap.get(name);
106+
assert(lock != null);
107+
108+
if(lock.referenceCount == 0)
109+
s_lockMap.remove(name);
104110
}
105111
}
106112

@@ -135,25 +141,29 @@ public boolean lock(int timeoutSeconds) {
135141
return false;
136142

137143
continue;
138-
} else {
139-
// we will discount the time that has been spent in previous waiting
144+
} else {
145+
// take ownership temporarily to prevent others enter into stage of acquiring DB lock
140146
ownerThread = Thread.currentThread();
141-
if(DbUtil.getGlobalLock(name, remainingMilliSeconds / 1000)) {
142-
lockCount++;
143-
holdingStartTick = System.currentTimeMillis();
144-
145-
// keep the lock in the intern map when we got the lock from database
146-
addRef();
147-
148-
if(s_logger.isTraceEnabled())
149-
s_logger.trace("lock " + name + " is acquired, lock count :" + lockCount);
150-
return true;
151-
} else {
152-
ownerThread = null;
153-
}
154-
return false;
147+
addRef();
155148
}
156-
}
149+
}
150+
151+
if(DbUtil.getGlobalLock(name, remainingMilliSeconds / 1000)) {
152+
synchronized(this) {
153+
lockCount++;
154+
holdingStartTick = System.currentTimeMillis();
155+
156+
if(s_logger.isTraceEnabled())
157+
s_logger.trace("lock " + name + " is acquired, lock count :" + lockCount);
158+
return true;
159+
}
160+
} else {
161+
synchronized(this) {
162+
ownerThread = null;
163+
releaseRef();
164+
return false;
165+
}
166+
}
157167
}
158168
} finally {
159169
if(interrupted) {

0 commit comments

Comments
 (0)