Skip to content

Commit f28a2b4

Browse files
author
Alex Huang
committed
bug 10501: This is really Kelven's bug but I'll fix it for him anyways. DAO code already have a way to extract the DB connection from a transaction that is stored in the TLS. There's no real reason for the DAO code to add special semantics to use a different DB connection. That can be done by simply switching the transaction before it even reached the dao code. Think about it. Why would anyone want to call one dao function, switch transaction, and then switch back. The right thing is for the caller to switch transaction, call a series of dao codes, and switch it back. That's the semantics I changed to. By doing this, it also eliminates the number of debug messages in this bug.
1 parent fd6900a commit f28a2b4

8 files changed

Lines changed: 82 additions & 169 deletions

File tree

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public class ClusterManagerImpl implements ClusterManager {
9494
private final Map<String, Listener> asyncCalls;
9595
private final Gson gson;
9696

97+
@Inject
9798
private AgentManager _agentMgr;
9899
@Inject
99100
private ClusteredAgentRebalanceService _rebalanceService;
@@ -563,24 +564,25 @@ private Runnable getHeartbeatTask() {
563564
return new Runnable() {
564565
@Override
565566
public void run() {
567+
Transaction txn = Transaction.open("ClusterHeartBeat");
566568
try {
569+
txn.transitToUserManagedConnection(getHeartbeatConnection());
567570
if(s_logger.isTraceEnabled()) {
568571
s_logger.trace("Cluster manager heartbeat update, id:" + _mshostId);
569572
}
570573

571-
Connection conn = getHeartbeatConnection();
572-
_mshostDao.update(conn, _mshostId, getCurrentRunId(), DateUtil.currentGMTTime());
574+
_mshostDao.update(_mshostId, getCurrentRunId(), DateUtil.currentGMTTime());
573575

574576
if (s_logger.isTraceEnabled()) {
575577
s_logger.trace("Cluster manager peer-scan, id:" + _mshostId);
576578
}
577579

578580
if (!_peerScanInited) {
579581
_peerScanInited = true;
580-
initPeerScan(conn);
582+
initPeerScan();
581583
}
582584

583-
peerScan(conn);
585+
peerScan();
584586
} catch(CloudRuntimeException e) {
585587
s_logger.error("Runtime DB exception ", e.getCause());
586588

@@ -602,6 +604,8 @@ public void run() {
602604
}
603605

604606
s_logger.error("Problem with the cluster heartbeat!", e);
607+
} finally {
608+
txn.close("ClusterHeartBeat");
605609
}
606610
}
607611
};
@@ -706,7 +710,7 @@ public void run() {
706710
}
707711
}
708712

709-
try { Thread.currentThread().sleep(1000); } catch (InterruptedException e) {}
713+
try { Thread.sleep(1000); } catch (InterruptedException e) {}
710714
}
711715
}
712716
};
@@ -729,20 +733,20 @@ private ClusterManagerMessage getNextNotificationMessage() {
729733
return null;
730734
}
731735

732-
private void initPeerScan(Connection conn) {
736+
private void initPeerScan() {
733737
// upon startup, for all inactive management server nodes that we see at startup time, we will send notification also to help upper layer perform
734738
// missed cleanup
735739
Date cutTime = DateUtil.currentGMTTime();
736-
List<ManagementServerHostVO> inactiveList = _mshostDao.getInactiveList(conn, new Date(cutTime.getTime() - heartbeatThreshold));
740+
List<ManagementServerHostVO> inactiveList = _mshostDao.getInactiveList(new Date(cutTime.getTime() - heartbeatThreshold));
737741
if(inactiveList.size() > 0) {
738742
this.queueNotification(new ClusterManagerMessage(ClusterManagerMessage.MessageType.nodeRemoved, inactiveList));
739743
}
740744
}
741745

742-
private void peerScan(Connection conn) {
746+
private void peerScan() {
743747
Date cutTime = DateUtil.currentGMTTime();
744748

745-
List<ManagementServerHostVO> currentList = _mshostDao.getActiveList(conn, new Date(cutTime.getTime() - heartbeatThreshold));
749+
List<ManagementServerHostVO> currentList = _mshostDao.getActiveList(new Date(cutTime.getTime() - heartbeatThreshold));
746750

747751
List<ManagementServerHostVO> removedNodeList = new ArrayList<ManagementServerHostVO>();
748752
List<ManagementServerHostVO> invalidatedNodeList = new ArrayList<ManagementServerHostVO>();
@@ -801,7 +805,7 @@ private void peerScan(Connection conn) {
801805
if(!pingManagementNode(mshost)) {
802806
s_logger.warn("Management node " + mshost.getId() + " is detected inactive by timestamp and also not pingable");
803807
activePeers.remove(mshost.getId());
804-
_mshostDao.invalidateRunSession(conn, mshost.getId(), mshost.getRunid());
808+
_mshostDao.invalidateRunSession(mshost.getId(), mshost.getRunid());
805809
try {
806810
JmxUtil.unregisterMBean("ClusterManager", "Node " + mshost.getId());
807811
} catch(Exception e) {

‎server/src/com/cloud/cluster/dao/ManagementServerHostDao.java‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@
1818

1919
package com.cloud.cluster.dao;
2020

21-
import java.sql.Connection;
2221
import java.util.Date;
2322
import java.util.List;
2423

25-
import com.cloud.cluster.ManagementServerHost.State;
2624
import com.cloud.cluster.ManagementServerHost;
25+
import com.cloud.cluster.ManagementServerHost.State;
2726
import com.cloud.cluster.ManagementServerHostVO;
28-
import com.cloud.host.Status;
2927
import com.cloud.utils.db.GenericDao;
3028

3129
public interface ManagementServerHostDao extends GenericDao<ManagementServerHostVO, Long> {
@@ -40,13 +38,9 @@ public interface ManagementServerHostDao extends GenericDao<ManagementServerHost
4038
List<ManagementServerHostVO> getActiveList(Date cutTime);
4139
List<ManagementServerHostVO> getInactiveList(Date cutTime);
4240

43-
void update(Connection conn, long id, long runid, String name, String version, String serviceIP, int servicePort, Date lastUpdate);
44-
void update(Connection conn, long id, long runid, Date lastUpdate);
45-
void invalidateRunSession(Connection conn, long id, long runid);
46-
List<ManagementServerHostVO> getActiveList(Connection conn, Date cutTime);
47-
List<ManagementServerHostVO> getInactiveList(Connection conn, Date cutTime);
41+
void invalidateRunSession(long id, long runid);
4842

49-
void update(Connection conn, long id, long runId, State state, Date lastUpdate);
43+
void update(long id, long runId, State state, Date lastUpdate);
5044

5145
List<ManagementServerHostVO> listBy(ManagementServerHost.State...states);
5246
}

‎server/src/com/cloud/cluster/dao/ManagementServerHostDaoImpl.java‎

Lines changed: 6 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
package com.cloud.cluster.dao;
2020

21-
import java.sql.Connection;
2221
import java.sql.PreparedStatement;
2322
import java.sql.SQLException;
2423
import java.util.Date;
@@ -51,110 +50,20 @@ public class ManagementServerHostDaoImpl extends GenericDaoBase<ManagementServer
5150
private final SearchBuilder<ManagementServerHostVO> StateSearch;
5251

5352
@Override
54-
public void update(Connection conn, long id, long runid, String name, String version, String serviceIP, int servicePort, Date lastUpdate) {
53+
public void invalidateRunSession(long id, long runid) {
54+
Transaction txn = Transaction.currentTxn();
5555
PreparedStatement pstmt = null;
5656
try {
57-
pstmt = conn.prepareStatement("update mshost set name=?, version=?, service_ip=?, service_port=?, last_update=?, removed=null, alert_count=0, runid=? where id=?");
58-
pstmt.setString(1, name);
59-
pstmt.setString(2, version);
60-
pstmt.setString(3, serviceIP);
61-
pstmt.setInt(4, servicePort);
62-
pstmt.setString(5, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), lastUpdate));
63-
pstmt.setLong(6, runid);
64-
pstmt.setLong(7, id);
65-
66-
pstmt.executeUpdate();
67-
conn.commit();
68-
} catch(SQLException e ) {
69-
throw new CloudRuntimeException("DB exception on " + pstmt.toString(), e);
70-
} finally {
71-
if(pstmt != null) {
72-
try {
73-
pstmt.close();
74-
} catch(Exception e) {
75-
s_logger.warn("Unable to close prepared statement due to exception ", e);
76-
}
77-
}
78-
}
79-
}
80-
81-
@Override
82-
public void update(Connection conn, long id, long runid, Date lastUpdate) {
83-
PreparedStatement pstmt = null;
84-
try {
85-
pstmt = conn.prepareStatement("update mshost set last_update=?, removed=null, alert_count=0 where id=? and runid=?");
86-
pstmt.setString(1, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), lastUpdate));
87-
pstmt.setLong(2, id);
88-
pstmt.setLong(3, runid);
89-
90-
int count = pstmt.executeUpdate();
91-
conn.commit();
92-
93-
if(count < 1) {
94-
throw new CloudRuntimeException("Invalid cluster session detected", new ClusterInvalidSessionException("runid " + runid + " is no longer valid"));
95-
}
96-
} catch (SQLException e) {
97-
throw new CloudRuntimeException("DB exception on " + pstmt.toString(), e);
98-
} finally {
99-
if(pstmt != null) {
100-
try {
101-
pstmt.close();
102-
} catch(Exception e) {
103-
s_logger.warn("Unable to close prepared statement due to exception ", e);
104-
}
105-
}
106-
}
107-
}
108-
109-
@Override
110-
public void invalidateRunSession(Connection conn, long id, long runid) {
111-
PreparedStatement pstmt = null;
112-
try {
113-
pstmt = conn.prepareStatement("update mshost set runid=0, state='Down' where id=? and runid=?");
57+
pstmt = txn.prepareAutoCloseStatement("update mshost set runid=0, state='Down' where id=? and runid=?");
11458
pstmt.setLong(1, id);
11559
pstmt.setLong(2, runid);
11660

11761
pstmt.executeUpdate();
118-
conn.commit();
11962
} catch (SQLException e) {
12063
throw new CloudRuntimeException("DB exception on " + pstmt.toString(), e);
121-
} finally {
122-
if(pstmt != null) {
123-
try {
124-
pstmt.close();
125-
} catch(Exception e) {
126-
s_logger.warn("Unable to close prepared statement due to exception ", e);
127-
}
128-
}
12964
}
13065
}
13166

132-
@Override
133-
public List<ManagementServerHostVO> getActiveList(Connection conn, Date cutTime) {
134-
Transaction txn = Transaction.openNew("getActiveList", conn);
135-
try {
136-
SearchCriteria<ManagementServerHostVO> sc = ActiveSearch.create();
137-
sc.setParameters("lastUpdateTime", cutTime);
138-
139-
return listIncludingRemovedBy(sc);
140-
} finally {
141-
txn.close("getActiveList");
142-
}
143-
}
144-
145-
@Override
146-
public List<ManagementServerHostVO> getInactiveList(Connection conn, Date cutTime) {
147-
Transaction txn = Transaction.openNew("getInactiveList", conn);
148-
try {
149-
SearchCriteria<ManagementServerHostVO> sc = InactiveSearch.create();
150-
sc.setParameters("lastUpdateTime", cutTime);
151-
152-
return listIncludingRemovedBy(sc);
153-
} finally {
154-
txn.close("getInactiveList");
155-
}
156-
}
157-
15867
@Override
15968
public ManagementServerHostVO findByMsid(long msid) {
16069
SearchCriteria<ManagementServerHostVO> sc = MsIdSearch.create();
@@ -190,7 +99,6 @@ public void update(long id, long runid, String name, String version, String serv
19099
txn.commit();
191100
} catch(Exception e) {
192101
s_logger.warn("Unexpected exception, ", e);
193-
txn.rollback();
194102
}
195103
}
196104

@@ -210,7 +118,6 @@ public boolean remove(Long id) {
210118
return true;
211119
} catch(Exception e) {
212120
s_logger.warn("Unexpected exception, ", e);
213-
txn.rollback();
214121
}
215122

216123
return false;
@@ -237,7 +144,6 @@ public void update(long id, long runid, Date lastUpdate) {
237144
}
238145
} catch(Exception e) {
239146
s_logger.warn("Unexpected exception, ", e);
240-
txn.rollback();
241147
}
242148
}
243149

@@ -298,31 +204,23 @@ protected ManagementServerHostDaoImpl() {
298204

299205

300206
@Override
301-
public void update(Connection conn, long id, long runId, State state, Date lastUpdate) {
207+
public void update(long id, long runId, State state, Date lastUpdate) {
208+
Transaction txn = Transaction.currentTxn();
302209
PreparedStatement pstmt = null;
303210
try {
304-
pstmt = conn.prepareStatement("update mshost set state=?, last_update=? where id=? and runid=?");
211+
pstmt = txn.prepareAutoCloseStatement("update mshost set state=?, last_update=? where id=? and runid=?");
305212
pstmt.setString(1, state.toString());
306213
pstmt.setString(2, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), lastUpdate));
307214
pstmt.setLong(3, id);
308215
pstmt.setLong(4, runId);
309216

310217
int count = pstmt.executeUpdate();
311-
conn.commit();
312218

313219
if(count < 1) {
314220
throw new CloudRuntimeException("Invalid cluster session detected", new ClusterInvalidSessionException("runid " + runId + " is no longer valid"));
315221
}
316222
} catch (SQLException e) {
317223
throw new CloudRuntimeException("DB exception on " + pstmt.toString(), e);
318-
} finally {
319-
if(pstmt != null) {
320-
try {
321-
pstmt.close();
322-
} catch(Exception e) {
323-
s_logger.warn("Unable to close prepared statement due to exception ", e);
324-
}
325-
}
326224
}
327225
}
328226

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@
3737
* txn.commit();
3838
*
3939
* 2. Annotate methods that uses a DAO's acquire method.
40-
* _dao.acquire(id);
40+
* _dao.acquireInLockTable(id);
4141
* ...
42-
* _dao.release(id);
42+
* _dao.releaseFromLockTable(id);
4343
*
4444
* 3. Annotate methods that are inside a DAO but doesn't use
4545
* the Transaction class. Generally, these are methods

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ public T createForUpdate() {
279279
return createForUpdate(null);
280280
}
281281

282-
@Override
282+
@Override @DB(txn=false)
283283
public <K> K getNextInSequence(final Class<K> clazz, final String name) {
284284
final TableGenerator tg = _tgs.get(name);
285285
assert (tg != null) : "Couldn't find Table generator using " + name;
@@ -377,7 +377,7 @@ public List<T> searchIncludingRemoved(SearchCriteria<T> sc, final Filter filter,
377377
}
378378
}
379379

380-
@Override @SuppressWarnings("unchecked") @DB
380+
@Override @SuppressWarnings("unchecked")
381381
public <M> List<M> customSearchIncludingRemoved(SearchCriteria<M> sc, final Filter filter) {
382382
String clause = sc != null ? sc.getWhereClause() : null;
383383
if (clause != null && clause.length() == 0) {
@@ -733,7 +733,6 @@ protected int update(ID id, UpdateBuilder ub, T entity) {
733733
return rowsUpdated;
734734
}
735735

736-
// @Override
737736
public int update(UpdateBuilder ub, final SearchCriteria<?> sc, Integer rows) {
738737
StringBuilder sql = null;
739738
PreparedStatement pstmt = null;

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public class Transaction {
9797
private String _name;
9898
private Connection _conn;
9999
private boolean _txn;
100-
private final short _dbId;
100+
private short _dbId;
101101
private long _txnTime;
102102
private Statement _stmt;
103103
private String _creator;
@@ -125,17 +125,16 @@ public static Transaction open(final short databaseId) {
125125
// transaction context in the stack. It is used in special use cases that we want to control DB connection explicitly and in the mean time utilize
126126
// the existing DAO features
127127
//
128-
public static Transaction openNew(final String name, Connection conn) {
129-
assert(conn != null);
130-
Transaction txn = new Transaction(name, false, CONNECTED_DB);
131-
txn._conn = conn;
132-
txn._prev = tls.get();
133-
tls.set(txn);
134-
135-
txn.takeOver(name, true);
136-
s_logger.debug("Registering txn" + txn.getId());
137-
s_mbean.addTransaction(txn);
138-
return txn;
128+
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();
130+
_conn = conn;
131+
_dbId = CONNECTED_DB;
132+
}
133+
134+
public void transitToAutoManagedConnection(short dbId) {
135+
assert(_stack.size() == 0) : "Can't change to auto managed connection unless your stack is empty";
136+
_dbId = dbId;
137+
_conn = null;
139138
}
140139

141140
public static Transaction open(final String name) {
@@ -166,7 +165,6 @@ public static Transaction open(final String name, final short databaseId, final
166165

167166
txn.takeOver(name, false);
168167
if (isNew) {
169-
s_logger.debug("Registering txn" + txn.getId());
170168
s_mbean.addTransaction(txn);
171169
}
172170
return txn;
@@ -611,7 +609,6 @@ public void close() {
611609
if(this._dbId == CONNECTED_DB) {
612610
tls.set(_prev);
613611
_prev = null;
614-
s_logger.debug("Unregistering txn" + getId());
615612
s_mbean.removeTransaction(this);
616613
}
617614
}

0 commit comments

Comments
 (0)