use of com.sun.messaging.jmq.jmsserver.persist.api.HABrokerInfo in project openmq by eclipse-ee4j.
the class DBManager method isHAClusterActive.
public boolean isHAClusterActive(Connection conn) throws BrokerException {
boolean isActive = false;
BrokerDAO bkrDAO = getDBManager().getDAOFactory().getBrokerDAO();
HashMap bkrMap = bkrDAO.getAllBrokerInfos(conn, false);
Iterator itr = bkrMap.values().iterator();
long currentTime = System.currentTimeMillis();
while (itr.hasNext()) {
HABrokerInfo bkrInfo = (HABrokerInfo) itr.next();
int state = bkrInfo.getState();
if (!BrokerState.getState(state).isActiveState()) {
// broker is not active
continue;
}
// We've a broker in active state, re-verify w/ last heartbeat;
// If heartbeat is older than 3 minutes then consider it not active
long lastHeartBeat = bkrInfo.getHeartbeat();
if (lastHeartBeat + 180000 > currentTime) {
isActive = true;
break;
}
}
return isActive;
}
use of com.sun.messaging.jmq.jmsserver.persist.api.HABrokerInfo in project openmq by eclipse-ee4j.
the class TransactionDAOImpl method updateTransactionState.
/**
* Update the TransactionState for the specified transaction.
*
* @param conn database connection
* @param txnUID the transaction ID
* @param txnState the new state
* @throws BrokerException if transaction does not exists in the store
*/
@Override
public void updateTransactionState(Connection conn, TransactionUID txnUID, TransactionState txnState, boolean replaycheck) throws BrokerException {
boolean myConn = false;
PreparedStatement pstmt = null;
Exception myex = null;
try {
// Get a connection
DBManager dbMgr = DBManager.getDBManager();
if (conn == null) {
conn = dbMgr.getConnection(true);
myConn = true;
}
if (replaycheck) {
TransactionState st = null;
try {
st = getTransactionState(conn, txnUID);
if (st.getState() == txnState.getState()) {
logger.log(Logger.INFO, BrokerResources.I_CANCEL_SQL_REPLAY, "TID:" + txnUID + "[" + txnState + "]");
return;
}
} catch (BrokerException e) {
if (e.getStatusCode() != Status.NOT_FOUND) {
e.setSQLRecoverable(true);
e.setSQLReplayCheck(true);
throw e;
}
}
}
pstmt = dbMgr.createPreparedStatement(conn, updateTxnStateSQL);
pstmt.setInt(1, txnState.getState());
Util.setObject(pstmt, 2, txnState);
pstmt.setLong(3, txnUID.longValue());
if (Globals.getHAEnabled()) {
pstmt.setString(4, dbMgr.getBrokerID());
}
if (pstmt.executeUpdate() == 0) {
// For HA mode, check if this broker still owns the store
if (Globals.getHAEnabled()) {
String brokerID = dbMgr.getBrokerID();
BrokerDAO dao = dbMgr.getDAOFactory().getBrokerDAO();
if (dao.isBeingTakenOver(conn, brokerID)) {
BrokerException be = new StoreBeingTakenOverException(br.getKString(BrokerResources.E_STORE_BEING_TAKEN_OVER));
try {
HABrokerInfo bkrInfo = dao.getBrokerInfo(conn, brokerID);
logger.logStack(Logger.ERROR, be.getMessage() + "[" + (bkrInfo == null ? "" + brokerID : bkrInfo.toString()) + "]", be);
} catch (Throwable t) {
/* Ignore error */
}
throw be;
}
}
throw new BrokerException(br.getKString(BrokerResources.E_TRANSACTIONID_NOT_FOUND_IN_STORE, txnUID), Status.NOT_FOUND);
}
} catch (Exception e) {
myex = e;
boolean replayck = false;
try {
if ((conn != null) && !conn.getAutoCommit()) {
conn.rollback();
}
} catch (SQLException rbe) {
replayck = true;
logger.log(Logger.ERROR, BrokerResources.X_DB_ROLLBACK_FAILED, rbe);
}
Exception ex;
if (e instanceof BrokerException) {
throw (BrokerException) e;
} else if (e instanceof IOException) {
ex = DBManager.wrapIOException("[" + updateTxnStateSQL + "]", (IOException) e);
} else if (e instanceof SQLException) {
ex = DBManager.wrapSQLException("[" + updateTxnStateSQL + "]", (SQLException) e);
} else {
ex = e;
}
BrokerException be = new BrokerException(br.getKString(BrokerResources.X_UPDATE_TXNSTATE_FAILED, txnUID), ex);
be.setSQLRecoverable(true);
if (replayck) {
be.setSQLReplayCheck(true);
}
throw be;
} finally {
if (myConn) {
Util.close(null, pstmt, conn, myex);
} else {
Util.close(null, pstmt, null, myex);
}
}
}
use of com.sun.messaging.jmq.jmsserver.persist.api.HABrokerInfo in project openmq by eclipse-ee4j.
the class TransactionDAOImpl method updateTransactionBrokers.
/**
* Update transaction's participant brokers for the specified transaction.
*
* @param conn database connection
* @param txnUID the transaction ID
* @param txnBrokers the transaction's participant brokers
* @throws BrokerException if transaction does not exists in the store
*/
@Override
public void updateTransactionBrokers(Connection conn, TransactionUID txnUID, TransactionBroker[] txnBrokers) throws BrokerException {
boolean myConn = false;
PreparedStatement pstmt = null;
Exception myex = null;
try {
// Get a connection
DBManager dbMgr = DBManager.getDBManager();
if (conn == null) {
conn = dbMgr.getConnection(true);
myConn = true;
}
pstmt = dbMgr.createPreparedStatement(conn, updateTxnBrokersSQL);
pstmt.setInt(1, TransactionInfo.TXN_CLUSTER);
Util.setObject(pstmt, 2, txnBrokers);
pstmt.setLong(3, txnUID.longValue());
if (Globals.getHAEnabled()) {
pstmt.setString(4, dbMgr.getBrokerID());
}
if (pstmt.executeUpdate() == 0) {
// For HA mode, check if this broker still owns the store
if (Globals.getHAEnabled()) {
String brokerID = dbMgr.getBrokerID();
BrokerDAO dao = dbMgr.getDAOFactory().getBrokerDAO();
if (dao.isBeingTakenOver(conn, brokerID)) {
BrokerException be = new StoreBeingTakenOverException(br.getKString(BrokerResources.E_STORE_BEING_TAKEN_OVER));
try {
HABrokerInfo bkrInfo = dao.getBrokerInfo(conn, brokerID);
logger.logStack(Logger.ERROR, be.getMessage() + "[" + (bkrInfo == null ? "" + brokerID : bkrInfo.toString()) + "]", be);
} catch (Throwable t) {
/* Ignore error */
}
throw be;
}
}
throw new BrokerException(br.getKString(BrokerResources.E_TRANSACTIONID_NOT_FOUND_IN_STORE, txnUID), Status.NOT_FOUND);
}
} catch (Exception e) {
myex = e;
try {
if ((conn != null) && !conn.getAutoCommit()) {
conn.rollback();
}
} catch (SQLException rbe) {
logger.log(Logger.ERROR, BrokerResources.X_DB_ROLLBACK_FAILED, rbe);
}
Exception ex;
if (e instanceof BrokerException) {
throw (BrokerException) e;
} else if (e instanceof IOException) {
ex = DBManager.wrapIOException("[" + updateTxnBrokersSQL + "]", (IOException) e);
} else if (e instanceof SQLException) {
ex = DBManager.wrapSQLException("[" + updateTxnBrokersSQL + "]", (SQLException) e);
} else {
ex = e;
}
BrokerException be = new BrokerException(br.getKString(BrokerResources.X_PERSIST_TRANSACTION_FAILED, txnUID), ex);
be.setSQLRecoverable(true);
throw be;
} finally {
if (myConn) {
Util.close(null, pstmt, conn, myex);
} else {
Util.close(null, pstmt, null, myex);
}
}
}
use of com.sun.messaging.jmq.jmsserver.persist.api.HABrokerInfo in project openmq by eclipse-ee4j.
the class SFSHABrokerInfoMap method updateHAMapForState.
private void updateHAMapForState(BrokerState state) throws BrokerException {
Map map = null;
if (state == null) {
map = Globals.getStore().getAllBrokerInfos();
} else {
map = Globals.getStore().getAllBrokerInfoByState(state);
}
Iterator itr = map.entrySet().iterator();
while (itr.hasNext()) {
Map.Entry entry = (Map.Entry) itr.next();
String key = (String) entry.getKey();
HABrokerInfo bi = (HABrokerInfo) entry.getValue();
HAClusteredBrokerImpl impl = (HAClusteredBrokerImpl) get(key);
if (impl == null) {
HAClusteredBroker cb = new SFSHAClusteredBrokerImpl(bi.getId(), bi, parent);
put(key, cb);
parent.brokerChanged(ClusterReason.ADDED, cb.getBrokerName(), null, cb, cb.getBrokerSessionUID(), null);
} else {
impl.update(bi);
}
}
if (state == null) {
itr = entrySet().iterator();
while (itr.hasNext()) {
Map.Entry entry = (Map.Entry) itr.next();
String key = (String) entry.getKey();
if (!map.containsKey(key)) {
itr.remove();
HAClusteredBrokerImpl impl = (HAClusteredBrokerImpl) entry.getValue();
parent.brokerChanged(ClusterReason.REMOVED, impl.getBrokerName(), impl, null, impl.getBrokerSessionUID(), null);
}
}
}
}
use of com.sun.messaging.jmq.jmsserver.persist.api.HABrokerInfo in project openmq by eclipse-ee4j.
the class HAClusteredBrokerImpl method getTakeoverBroker.
/**
* Retrieves the id of the broker who has taken over this broker's store.
*
* @return the broker id of the takeover broker (or null if there is not a takeover broker).
*/
@Override
public synchronized String getTakeoverBroker() throws BrokerException {
HABrokerInfo bkrInfo = Globals.getStore().getBrokerInfo(brokerid);
if (bkrInfo == null) {
logger.log(Logger.ERROR, BrokerResources.E_BROKERINFO_NOT_FOUND_IN_STORE, brokerid);
return null;
}
takeoverBroker = bkrInfo.getTakeoverBrokerID();
return takeoverBroker;
}
Aggregations