Skip to content

Commit ec4a46e

Browse files
author
Alex Huang
committed
use in memory counter for locks so we don't do a delete everytime
1 parent 3f18192 commit ec4a46e

2 files changed

Lines changed: 54 additions & 13 deletions

File tree

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

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public class Merovingian2 extends StandardMBean implements MerovingianMBean {
5959

6060
private static Merovingian2 s_instance = null;
6161
private ConnectionConcierge _concierge = null;
62+
private static ThreadLocal<Count> s_tls = new ThreadLocal<Count>();
6263

6364
private Merovingian2(long msId) {
6465
super(MerovingianMBean.class, false);
@@ -91,6 +92,25 @@ public static Merovingian2 getLockMaster() {
9192
return s_instance;
9293
}
9394

95+
protected void incrCount() {
96+
Count count = s_tls.get();
97+
if (count == null) {
98+
count = new Count();
99+
s_tls.set(count);
100+
}
101+
102+
count.count++;
103+
}
104+
105+
protected void decrCount() {
106+
Count count = s_tls.get();
107+
if (count == null) {
108+
return;
109+
}
110+
111+
count.count--;
112+
}
113+
94114
public boolean acquire(String key, int timeInSeconds) {
95115
Thread th = Thread.currentThread();
96116
String threadName = th.getName();
@@ -103,21 +123,23 @@ public boolean acquire(String key, int timeInSeconds) {
103123

104124
while ((InaccurateClock.getTime() - startTime) < (timeInSeconds * 1000)) {
105125
int count = owns(key);
106-
if (count == -1) {
107-
try {
108-
Thread.sleep(1000);
109-
} catch (InterruptedException e) {
110-
}
111-
} else if (count >= 1) {
126+
if (count >= 1) {
112127
return increment(key, threadName, threadId);
113-
} else {
128+
} else if (count == 0) {
114129
if (doAcquire(key, threadName, threadId)) {
115130
return true;
116131
}
117132
}
133+
try {
134+
if (s_logger.isTraceEnabled()) {
135+
s_logger.trace("Sleeping more time while waiting for lck-" + key);
136+
}
137+
Thread.sleep(5000);
138+
} catch (InterruptedException e) {
139+
}
118140
}
119141
if (s_logger.isTraceEnabled()) {
120-
s_logger.trace("Timed out on acquiring lock " + key);
142+
s_logger.trace("Timed out on acquiring lock " + key + ". Waited for " + (InaccurateClock.getTime() - startTime));
121143
}
122144
return false;
123145
}
@@ -133,9 +155,13 @@ protected boolean increment(String key, String threadName, int threadId) {
133155
int rows = pstmt.executeUpdate();
134156
assert (rows <= 1) : "hmm...non unique key? " + pstmt;
135157
if (s_logger.isTraceEnabled()) {
136-
s_logger.trace("lck-" + key + (rows == 1 ? " acquired for a second time" : " failed to acquire"));
158+
s_logger.trace("lck-" + key + (rows == 1 ? " acquired again" : " failed to acquire again"));
137159
}
138-
return rows == 1;
160+
if (rows == 1) {
161+
incrCount();
162+
return true;
163+
}
164+
return false;
139165
} catch (SQLException e) {
140166
throw new CloudRuntimeException("Unable to increment " + key, e);
141167
} finally {
@@ -165,6 +191,7 @@ protected boolean doAcquire(String key, String threadName, int threadId) {
165191
if (s_logger.isTraceEnabled()) {
166192
s_logger.trace("Acquired for lck-" + key);
167193
}
194+
incrCount();
168195
return true;
169196
}
170197
} catch(SQLException e) {
@@ -265,6 +292,7 @@ public boolean release(String key) {
265292
if (result == 1 && s_logger.isTraceEnabled()) {
266293
s_logger.trace("lck-" + key + " removed");
267294
}
295+
decrCount();
268296
}
269297
return rows == 1;
270298
} catch (SQLException e) {
@@ -372,6 +400,15 @@ public List<Map<String, String>> getLocksAcquiredBy(long msId, String threadName
372400
}
373401

374402
public void cleanupThread() {
403+
Count count = s_tls.get();
404+
if (count == null || count.count == 0) {
405+
return;
406+
}
407+
408+
int c = count.count;
409+
410+
count.count = 0;
411+
375412
Thread th = Thread.currentThread();
376413
String threadName = th.getName();
377414
int threadId = System.identityHashCode(th);
@@ -383,7 +420,7 @@ public void cleanupThread() {
383420
pstmt.setString(2, threadName);
384421
pstmt.setInt(3, threadId);
385422
int rows = pstmt.executeUpdate();
386-
assert (rows == 0) : "Abandon hope, all ye who enter here....There were still " + rows + " locks not released when the transaction ended!";
423+
assert (false) : "Abandon hope, all ye who enter here....There were still " + rows + ":" + c + " locks not released when the transaction ended!";
387424
} catch (SQLException e) {
388425
throw new CloudRuntimeException("Can't clear locks " + pstmt, e);
389426
} finally {
@@ -398,7 +435,7 @@ public void cleanupThread() {
398435

399436
@Override
400437
public boolean releaseLockAsLastResortAndIReallyKnowWhatIAmDoing(String key) {
401-
s_logger.info("Releasing a lock from jMX lck-" + key);
438+
s_logger.info("Releasing a lock from JMX lck-" + key);
402439
PreparedStatement pstmt = null;
403440
try {
404441
pstmt = _concierge.conn().prepareStatement(RELEASE_LOCK_SQL);
@@ -410,4 +447,8 @@ public boolean releaseLockAsLastResortAndIReallyKnowWhatIAmDoing(String key) {
410447
return false;
411448
}
412449
}
450+
451+
protected static class Count {
452+
public int count = 0;
453+
}
413454
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public static Transaction open(final short databaseId) {
126126
// the existing DAO features
127127
//
128128
public void transitToUserManagedConnection(Connection conn) {
129-
assert(_conn == null && _stack.size() <= 1) : "Can't change to a user managed connection unless the stack is empty and the db connection is null: " + toString();
129+
assert (_conn == null) : "Can't change to a user managed connection unless the stack is empty and the db connection is null: " + toString();
130130
_conn = conn;
131131
_dbId = CONNECTED_DB;
132132
}

0 commit comments

Comments
 (0)