Skip to content

Commit ea48d40

Browse files
author
Alex Huang
committed
connection conierge to deal with connections that are not returned to pools
1 parent c679fc0 commit ea48d40

4 files changed

Lines changed: 28 additions & 90 deletions

File tree

‎server/src/com/cloud/cluster/ClusterManagerImpl.java‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import com.cloud.utils.component.ComponentLocator;
6969
import com.cloud.utils.component.Inject;
7070
import com.cloud.utils.concurrency.NamedThreadFactory;
71+
import com.cloud.utils.db.ConnectionConcierge;
7172
import com.cloud.utils.db.DB;
7273
import com.cloud.utils.db.Transaction;
7374
import com.cloud.utils.events.SubscriptionMgr;
@@ -102,7 +103,7 @@ public class ClusterManagerImpl implements ClusterManager {
102103
private final ScheduledExecutorService _heartbeatScheduler = Executors.newScheduledThreadPool(1, new NamedThreadFactory("Cluster-Heartbeat"));
103104
private final ExecutorService _notificationExecutor = Executors.newFixedThreadPool(1, new NamedThreadFactory("Cluster-Notification"));
104105
private final List<ClusterManagerMessage> _notificationMsgs = new ArrayList<ClusterManagerMessage>();
105-
private Connection _heartbeatConnection = null;
106+
private ConnectionConcierge _heartbeatConnection = null;
106107

107108
private final ExecutorService _executor;
108109

@@ -624,23 +625,20 @@ private boolean isRootCauseConnectionRelated(Throwable e) {
624625
}
625626

626627
private Connection getHeartbeatConnection() throws SQLException {
627-
if(_heartbeatConnection != null) {
628-
return _heartbeatConnection;
628+
if(_heartbeatConnection == null) {
629+
Connection conn = Transaction.getStandaloneConnectionWithException();
630+
_heartbeatConnection = new ConnectionConcierge("ClusterManagerHeartBeat", conn, false, false);
629631
}
630632

631-
_heartbeatConnection = Transaction.getStandaloneConnectionWithException();
632-
return _heartbeatConnection;
633+
return _heartbeatConnection.conn();
633634
}
634635

635636
private void invalidHeartbeatConnection() {
636637
if(_heartbeatConnection != null) {
637-
try {
638-
_heartbeatConnection.close();
639-
} catch (SQLException e) {
640-
s_logger.warn("Unable to close hearbeat DB connection. ", e);
638+
Connection conn = Transaction.getStandaloneConnection();
639+
if (conn != null) {
640+
_heartbeatConnection.reset(Transaction.getStandaloneConnection());
641641
}
642-
643-
_heartbeatConnection = null;
644642
}
645643
}
646644

‎server/src/com/cloud/storage/StorageManagerImpl.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2626,7 +2626,7 @@ public void prepare(VirtualMachineProfile<? extends VirtualMachine> vm, DeployDe
26262626
}
26272627
if (assignedPool != null) {
26282628
Volume.State state = vol.getState();
2629-
if (state == Volume.State.Allocated && state == Volume.State.Creating) {
2629+
if (state == Volume.State.Allocated || state == Volume.State.Creating) {
26302630
recreateVols.add(vol);
26312631
} else {
26322632
if (vol.isRecreatable()) {

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

Lines changed: 18 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,12 @@
2727
import java.util.List;
2828
import java.util.Map;
2929
import java.util.TimeZone;
30-
import java.util.concurrent.Executors;
31-
import java.util.concurrent.ScheduledExecutorService;
32-
import java.util.concurrent.TimeUnit;
3330

3431
import javax.management.StandardMBean;
3532

3633
import org.apache.log4j.Logger;
3734

3835
import com.cloud.utils.DateUtil;
39-
import com.cloud.utils.concurrency.NamedThreadFactory;
4036
import com.cloud.utils.exception.CloudRuntimeException;
4137
import com.cloud.utils.mgmt.JmxUtil;
4238
import com.cloud.utils.time.InaccurateClock;
@@ -62,15 +58,18 @@ public class Merovingian2 extends StandardMBean implements MerovingianMBean {
6258
private long _msId;
6359

6460
private static Merovingian2 s_instance = null;
65-
ScheduledExecutorService _executor = null;
66-
Connection _conn = null;
61+
ConnectionConcierge _concierge = null;
6762

6863
private Merovingian2(long msId) {
6964
super(MerovingianMBean.class, false);
7065
_msId = msId;
71-
String result = resetDbConnection();
72-
if (!result.equalsIgnoreCase("Success")) {
73-
throw new CloudRuntimeException("Unable to initialize a connection to the database for locking purposes due to " + result);
66+
Connection conn = null;
67+
try {
68+
conn = Transaction.getStandaloneConnectionWithException();
69+
_concierge = new ConnectionConcierge("LockMaster", conn, true, true);
70+
} catch (SQLException e) {
71+
s_logger.error("Unable to get a new db connection", e);
72+
throw new CloudRuntimeException("Unable to initialize a connection to the database for locking purposes: ", e);
7473
}
7574
}
7675

@@ -90,42 +89,6 @@ public static Merovingian2 getLockMaster() {
9089
return s_instance;
9190
}
9291

93-
@Override
94-
public String resetDbConnection() {
95-
if (_conn != null) {
96-
try {
97-
_conn.close();
98-
} catch (Throwable th) {
99-
s_logger.error("Unable to close connection", th);
100-
}
101-
}
102-
103-
try {
104-
_conn = Transaction.getStandaloneConnectionWithException();
105-
_conn.setAutoCommit(true);
106-
} catch (SQLException e) {
107-
s_logger.error("Unable to get a new db connection", e);
108-
return "Unable to initialize a connection to the database for locking purposes: " + e;
109-
}
110-
111-
if (_conn == null) {
112-
return "Unable to initialize a connection to the database for locking purposes, shutdown this server!";
113-
}
114-
115-
if (_executor != null) {
116-
try {
117-
_executor.shutdown();
118-
} catch (Throwable th) {
119-
s_logger.error("Unable to shutdown the executor", th);
120-
}
121-
}
122-
123-
_executor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("LockMasterConnectionKeepAlive"));
124-
_executor.schedule(new KeepAliveTask(), 10, TimeUnit.SECONDS);
125-
126-
return "Success";
127-
}
128-
12992
public boolean acquire(String key, int timeInSeconds) {
13093
Thread th = Thread.currentThread();
13194
String threadName = th.getName();
@@ -160,7 +123,7 @@ public boolean acquire(String key, int timeInSeconds) {
160123
protected boolean increment(String key, String threadName, int threadId) {
161124
PreparedStatement pstmt = null;
162125
try {
163-
pstmt = _conn.prepareStatement(INCREMENT_SQL);
126+
pstmt = _concierge.conn().prepareStatement(INCREMENT_SQL);
164127
pstmt.setString(1, key);
165128
pstmt.setLong(2, _msId);
166129
pstmt.setString(3, threadName);
@@ -188,7 +151,7 @@ protected boolean doAcquire(String key, String threadName, int threadId) {
188151

189152
long startTime = InaccurateClock.getTime();
190153
try {
191-
pstmt = _conn.prepareStatement(ACQUIRE_SQL);
154+
pstmt = _concierge.conn().prepareStatement(ACQUIRE_SQL);
192155
pstmt.setString(1, key);
193156
pstmt.setLong(2, _msId);
194157
pstmt.setString(3, threadName);
@@ -226,7 +189,7 @@ protected Map<String, String> isLocked(String key) {
226189
PreparedStatement pstmt = null;
227190
ResultSet rs = null;
228191
try {
229-
pstmt = _conn.prepareStatement(INQUIRE_SQL);
192+
pstmt = _concierge.conn().prepareStatement(INQUIRE_SQL);
230193
pstmt.setString(1, key);
231194
rs = pstmt.executeQuery();
232195
if (!rs.next()) {
@@ -258,7 +221,7 @@ public void cleanupThisServer() {
258221
public void cleanupForServer(long msId) {
259222
PreparedStatement pstmt = null;
260223
try {
261-
pstmt = _conn.prepareStatement(CLEANUP_MGMT_LOCKS_SQL);
224+
pstmt = _concierge.conn().prepareStatement(CLEANUP_MGMT_LOCKS_SQL);
262225
pstmt.setLong(1, _msId);
263226
pstmt.executeUpdate();
264227
} catch (SQLException e) {
@@ -279,7 +242,7 @@ public boolean release(String key) {
279242
String threadName = th.getName();
280243
int threadId = System.identityHashCode(th);
281244
try {
282-
pstmt = _conn.prepareStatement(DECREMENT_SQL);
245+
pstmt = _concierge.conn().prepareStatement(DECREMENT_SQL);
283246
pstmt.setString(1, key);
284247
pstmt.setLong(2, _msId);
285248
pstmt.setString(3, threadName);
@@ -291,7 +254,7 @@ public boolean release(String key) {
291254
}
292255
if (rows == 1) {
293256
pstmt.close();
294-
pstmt = _conn.prepareStatement(RELEASE_SQL);
257+
pstmt = _concierge.conn().prepareStatement(RELEASE_SQL);
295258
pstmt.setString(1, key);
296259
pstmt.setLong(2, _msId);
297260
int result = pstmt.executeUpdate();
@@ -336,7 +299,7 @@ protected List<Map<String, String>> getLocks(String sql, Long msId) {
336299
PreparedStatement pstmt = null;
337300
ResultSet rs = null;
338301
try {
339-
pstmt = _conn.prepareStatement(sql);
302+
pstmt = _concierge.conn().prepareStatement(sql);
340303
if (msId != null) {
341304
pstmt.setLong(1, msId);
342305
}
@@ -384,7 +347,7 @@ public List<Map<String, String>> getLocksAcquiredBy(long msId, String threadName
384347
PreparedStatement pstmt = null;
385348
ResultSet rs = null;
386349
try {
387-
pstmt = _conn.prepareStatement(SELECT_THREAD_LOCKS_SQL);
350+
pstmt = _concierge.conn().prepareStatement(SELECT_THREAD_LOCKS_SQL);
388351
pstmt.setLong(1, msId);
389352
pstmt.setString(2, threadName);
390353
rs = pstmt.executeQuery();
@@ -411,7 +374,7 @@ public void cleanupThread() {
411374

412375
PreparedStatement pstmt = null;
413376
try {
414-
pstmt = _conn.prepareStatement(CLEANUP_THREAD_LOCKS_SQL);
377+
pstmt = _concierge.conn().prepareStatement(CLEANUP_THREAD_LOCKS_SQL);
415378
pstmt.setLong(1, _msId);
416379
pstmt.setString(2, threadName);
417380
pstmt.setInt(3, threadId);
@@ -433,7 +396,7 @@ public void cleanupThread() {
433396
public boolean releaseLockAsLastResortAndIReallyKnowWhatIAmDoing(String key) {
434397
PreparedStatement pstmt = null;
435398
try {
436-
pstmt = _conn.prepareStatement(RELEASE_LOCK_SQL);
399+
pstmt = _concierge.conn().prepareStatement(RELEASE_LOCK_SQL);
437400
pstmt.setString(1, key);
438401
int rows = pstmt.executeUpdate();
439402
return rows > 0;
@@ -442,25 +405,4 @@ public boolean releaseLockAsLastResortAndIReallyKnowWhatIAmDoing(String key) {
442405
return false;
443406
}
444407
}
445-
446-
protected class KeepAliveTask implements Runnable {
447-
@Override
448-
public void run() {
449-
PreparedStatement pstmt = null; // Should this even be prepared everytime?
450-
try {
451-
pstmt = _conn.prepareStatement("SELECT 1");
452-
pstmt.executeQuery();
453-
} catch (Throwable th) {
454-
s_logger.error("Unable to keep the db connection alive for locking purposes!", th);
455-
} finally {
456-
if (pstmt != null) {
457-
try {
458-
pstmt.close();
459-
} catch (SQLException e) {
460-
}
461-
}
462-
}
463-
}
464-
}
465-
466408
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,5 @@ public interface MerovingianMBean {
2929

3030
boolean releaseLockAsLastResortAndIReallyKnowWhatIAmDoing(String key);
3131

32-
String resetDbConnection();
33-
3432
void cleanupForServer(long msId);
3533
}

0 commit comments

Comments
 (0)