use of com.sun.messaging.jmq.jmsserver.persist.api.HABrokerInfo in project openmq by eclipse-ee4j.
the class BrokerDAOImpl method update.
/**
* Update an existing entry.
*
* @param conn database connection
* @param id Broker ID
* @param updateType (as defined in HABrokerInfo)
* @param oldValue (depending on updateType)
* @param newValue (depending on updateType)
* @return current active store session UID only if reseted takeover broker
*/
@Override
public UID update(Connection conn, String id, int updateType, Object oldValue, Object newValue) throws BrokerException {
String _updatesql = "";
UID currentID = null;
boolean myConn = false;
PreparedStatement pstmt = null;
Exception myex = null;
try {
// Get a connection
DBManager dbMgr = DBManager.getDBManager();
if (conn == null) {
conn = dbMgr.getConnection(false);
myConn = true;
}
switch(updateType) {
case HABrokerInfo.UPDATE_VERSION:
_updatesql = updateVersionSQL;
pstmt = dbMgr.createPreparedStatement(conn, updateVersionSQL);
pstmt.setInt(1, ((Integer) newValue).intValue());
pstmt.setString(2, id);
pstmt.executeUpdate();
pstmt.close();
break;
case HABrokerInfo.UPDATE_URL:
_updatesql = updateURLSQL;
pstmt = dbMgr.createPreparedStatement(conn, updateURLSQL);
pstmt.setString(1, (String) newValue);
pstmt.setString(2, id);
pstmt.executeUpdate();
pstmt.close();
break;
case HABrokerInfo.RESET_TAKEOVER_BROKER_READY_OPERATING:
_updatesql = resetTakeoverBrokerSQL;
pstmt = dbMgr.createPreparedStatement(conn, resetTakeoverBrokerSQL);
pstmt.setInt(1, (BrokerState.OPERATING).intValue());
pstmt.setString(2, id);
int updateCnt = pstmt.executeUpdate();
pstmt.close();
if (updateCnt == 1) {
logger.log(logger.INFO, br.getKString(BrokerResources.I_THIS_BROKER_RESETED_TAKEOVER_BROKER, TAKEOVER_BROKER_COLUMN, id));
long ssid = dbMgr.getDAOFactory().getStoreSessionDAO().insert(conn, id, ((UID) newValue).longValue(), false);
logger.log(logger.INFO, br.getKString(BrokerResources.I_THIS_BROKER_CURRENT_STORE_SESSION, id, String.valueOf(ssid)));
currentID = new UID(ssid);
break;
} else if (updateCnt == 0) {
_updatesql = updateStateThisBrokerSQL;
if (!updateState(conn, id, BrokerState.OPERATING, (BrokerState) oldValue, true)) {
HABrokerInfo info = getBrokerInfo(conn, id);
throw new BrokerException("IllegalStateException for updating state " + oldValue + " to " + BrokerState.OPERATING.toString() + ": " + info);
}
break;
} else {
throw new BrokerException("Unexpected affected row count " + updateCnt + " for updating broker info " + id);
}
case HABrokerInfo.RESTORE_ON_TAKEOVER_FAIL:
if (fi.FAULT_INJECTION) {
fi.checkFaultAndThrowBrokerException(FaultInjection.FAULT_HA_TAKEOVER_RESTORE_EXCEPTION, null);
fi.checkFaultAndExit(FaultInjection.FAULT_HA_TAKEOVER_RESTORE_HALT, null, 2, false);
}
_updatesql = restoreOnTakeoverFailSQL;
pstmt = dbMgr.createPreparedStatement(conn, restoreOnTakeoverFailSQL);
pstmt.setInt(1, ((HABrokerInfo) newValue).getState());
pstmt.setString(2, id);
pstmt.setString(3, (String) oldValue);
updateCnt = pstmt.executeUpdate();
pstmt.close();
if (updateCnt != 1) {
throw new BrokerException("Unexpected affected row count " + updateCnt + " for restoring broker info " + id);
}
break;
case HABrokerInfo.RESTORE_HEARTBEAT_ON_TAKEOVER_FAIL:
_updatesql = restoreHeartbeatOnTakeoverFailSQL;
pstmt = dbMgr.createPreparedStatement(conn, restoreHeartbeatOnTakeoverFailSQL);
pstmt.setLong(1, ((HABrokerInfo) newValue).getHeartbeat());
pstmt.setString(2, id);
pstmt.setLong(3, ((HABrokerInfo) newValue).getTakeoverTimestamp());
pstmt.setString(4, (String) oldValue);
pstmt.executeUpdate();
pstmt.close();
break;
default:
String emsg = Globals.getBrokerResources().getKString(BrokerResources.E_INTERNAL_BROKER_ERROR, "Unknown update type " + updateType + " for updating broker info " + id);
BrokerException ex = new BrokerException(emsg);
logger.logStack(logger.ERROR, emsg, ex);
throw ex;
}
if (myConn) {
conn.commit();
}
return currentID;
} 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 SQLException) {
ex = DBManager.wrapSQLException("[" + _updatesql + "]", (SQLException) e);
} else {
ex = e;
}
throw new BrokerException(br.getKString(BrokerResources.X_PERSIST_BROKERINFO_FAILED, id), ex);
} 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 BrokerDAOImpl method getAllBrokerInfosByState.
/**
* Get broker information for all brokers in an HA cluster by state.
*
* @param conn database connection
* @param state the state of the broker
* @return a HashMap object containing HABrokerInfo for all brokers
*/
@Override
public HashMap getAllBrokerInfosByState(Connection conn, BrokerState state) throws BrokerException {
HashMap data = new HashMap();
boolean myConn = false;
PreparedStatement pstmt = null;
ResultSet rs = 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, selectAllByStateSQL);
pstmt.setInt(1, state.intValue());
rs = pstmt.executeQuery();
while (rs.next()) {
HABrokerInfo bkrInfo = loadData(rs);
data.put(bkrInfo.getId(), bkrInfo);
}
} 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 + "[" + selectAllByStateSQL + "]", rbe);
}
Exception ex;
if (e instanceof BrokerException) {
throw (BrokerException) e;
} else if (e instanceof SQLException) {
ex = DBManager.wrapSQLException("[" + selectAllByStateSQL + "]", (SQLException) e);
} else {
ex = e;
}
throw new BrokerException(br.getKString(BrokerResources.X_LOAD_ALL_BROKERINFO_FAILED), ex);
} finally {
if (myConn) {
Util.close(rs, pstmt, conn, myex);
} else {
Util.close(rs, pstmt, null, myex);
}
}
return data;
}
use of com.sun.messaging.jmq.jmsserver.persist.api.HABrokerInfo in project openmq by eclipse-ee4j.
the class MessageDAOImpl method delete.
/**
* Delete an existing entry.
*
* @param conn Database Connection
* @param dstUID the destination
* @param id the SysMessageID
*/
@Override
public void delete(Connection conn, DestinationUID dstUID, String id, 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(false);
// Set to true since this is our connection
myConn = true;
}
if (fi.FAULT_INJECTION) {
HashMap fips = new HashMap();
fips.put(FaultInjection.DST_NAME_PROP, DestinationUID.getUniqueString(dstUID.getName(), dstUID.isQueue()));
fi.checkFaultAndExit(FaultInjection.FAULT_TXN_COMMIT_1_8, fips, 2, false);
}
// Now delete the message
boolean deleteFailed = false;
pstmt = dbMgr.createPreparedStatement(conn, deleteSQL);
pstmt.setString(1, id);
if (pstmt.executeUpdate() == 0) {
deleteFailed = true;
} else {
// For HA mode, make sure 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;
}
}
}
// Delete states
dbMgr.getDAOFactory().getConsumerStateDAO().deleteByMessageID(conn, id);
if (deleteFailed && replaycheck) {
logger.log(Logger.INFO, BrokerResources.I_CANCEL_SQL_REPLAY, id + "[" + dstUID + "]delete");
return;
}
if (deleteFailed) {
// We'll assume the msg does not exist
throw new BrokerException(br.getKString(BrokerResources.E_MSG_NOT_FOUND_IN_STORE, id, dstUID), Status.NOT_FOUND);
}
// Check whether to commit or not
if (myConn) {
conn.commit();
}
} 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) {
if (!(e instanceof StoreBeingTakenOverException) && ((BrokerException) e).getStatusCode() != Status.NOT_FOUND) {
((BrokerException) e).setSQLRecoverable(true);
((BrokerException) e).setSQLReplayCheck(replayck);
}
throw (BrokerException) e;
} else if (e instanceof SQLException) {
ex = DBManager.wrapSQLException("[" + deleteSQL + "]", (SQLException) e);
} else {
ex = e;
}
BrokerException be = new BrokerException(br.getKString(BrokerResources.X_REMOVE_MESSAGE_FAILED, id), ex);
be.setSQLRecoverable(true);
be.setSQLReplayCheck(replayck);
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 DBTool method doBackup.
/**
* Backup the JDBC store to filebased backup files.
*/
private void doBackup() throws BrokerException {
if (!Globals.getHAEnabled()) {
throw new BrokerException(br.getKString(BrokerResources.I_HA_NOT_ENABLE, dbmgr.getBrokerID()));
}
// instantiate the file store.
Properties props = System.getProperties();
String clusterID = Globals.getClusterID();
String backupDir = (String) props.get(Globals.IMQ + ".backupdir") + File.separator + clusterID;
logger.logToAll(Logger.INFO, "Backup persistent store for HA cluster " + clusterID);
final FileStore fileStore = new FileStore(backupDir, false);
// for backup, need to clear the store before storing anything.
fileStore.clearAll(false);
JDBCStore jdbcStore = null;
try {
jdbcStore = (JDBCStore) StoreManager.getStore();
/*
* For data that are not broker specific, i.e. properties, change records, consumers, brokers, and sessions, we will
* store those data on the top level directory (a.k.a cluster filestore).
*/
// Properties table
Properties properties = jdbcStore.getAllProperties();
Iterator propItr = properties.entrySet().iterator();
while (propItr.hasNext()) {
Map.Entry entry = (Map.Entry) propItr.next();
fileStore.updateProperty((String) entry.getKey(), entry.getValue(), false);
}
propItr = null;
properties = null;
// Configuration Change Record table
List<ChangeRecordInfo> records = jdbcStore.getAllConfigRecords();
for (int i = 0, len = records.size(); i < len; i++) {
fileStore.storeConfigChangeRecord(records.get(i).getTimestamp(), records.get(i).getRecord(), false);
}
records = null;
// Consumer table
Consumer[] consumerArray = jdbcStore.getAllInterests();
for (int i = 0; i < consumerArray.length; i++) {
fileStore.storeInterest(consumerArray[i], false);
}
consumerArray = null;
// Broker & Session table - store all HABrokerInfo as a property
HashMap bkrMap = jdbcStore.getAllBrokerInfos(true);
List haBrokers = new ArrayList(bkrMap.values());
fileStore.updateProperty(STORE_PROPERTY_HABROKERS, haBrokers, true);
/*
* For each broker in the cluster, we will store broker specific data, destinations, messages, transactions,
* acknowledgements in their own filestore (a.k.a broker filestore).
*/
Iterator bkrItr = haBrokers.iterator();
while (bkrItr.hasNext()) {
// Backup data for each broker
HABrokerInfo bkrInfo = (HABrokerInfo) bkrItr.next();
String brokerID = bkrInfo.getId();
logger.logToAll(Logger.INFO, "Backup persistent data for broker " + brokerID);
FileStore bkrFS = null;
JMSBridgeStore jmsbridgeStore = null;
try {
String instanceRootDir = backupDir + File.separator + brokerID;
bkrFS = new FileStore(instanceRootDir, false);
bkrFS.clearAll(false);
// Destination table.
Destination[] dstArray = jdbcStore.getAllDestinations(brokerID);
for (int i = 0, len = dstArray.length; i < len; i++) {
DestinationUID did = dstArray[i].getDestinationUID();
Destination dst = bkrFS.getDestination(did);
if (dst == null) {
// Store the destination if not found
bkrFS.storeDestination(dstArray[i], false);
}
}
// Storing messages for each destination.
for (int i = 0, len = dstArray.length; i < len; i++) {
Enumeration e = jdbcStore.messageEnumeration(dstArray[i]);
try {
for (; e.hasMoreElements(); ) {
DestinationUID did = dstArray[i].getDestinationUID();
Packet message = (Packet) e.nextElement();
SysMessageID mid = message.getSysMessageID();
// Get interest states for the message; Consumer State table
HashMap stateMap = jdbcStore.getInterestStates(did, mid);
if (stateMap == null || stateMap.isEmpty()) {
bkrFS.storeMessage(did, message, false);
} else {
int size = stateMap.size();
ConsumerUID[] iids = new ConsumerUID[size];
int[] states = new int[size];
Iterator stateItr = stateMap.entrySet().iterator();
int j = 0;
while (stateItr.hasNext()) {
Map.Entry entry = (Map.Entry) stateItr.next();
iids[j] = (ConsumerUID) entry.getKey();
states[j] = ((Integer) entry.getValue()).intValue();
j++;
}
bkrFS.storeMessage(did, message, iids, states, false);
}
}
} finally {
jdbcStore.closeEnumeration(e);
}
}
// Transaction table
Collection txnList = jdbcStore.getTransactions(brokerID);
Iterator txnItr = txnList.iterator();
while (txnItr.hasNext()) {
TransactionUID tid = (TransactionUID) txnItr.next();
TransactionInfo txnInfo = jdbcStore.getTransactionInfo(tid);
TransactionAcknowledgement[] txnAck = jdbcStore.getTransactionAcks(tid);
bkrFS.storeTransaction(tid, txnInfo, false);
for (int i = 0, len = txnAck.length; i < len; i++) {
bkrFS.storeTransactionAck(tid, txnAck[i], false);
}
}
/**
************************************************
* JMSBridge
*************************************************
*/
Properties bp = new Properties();
bp.setProperty("instanceRootDir", instanceRootDir);
bp.setProperty("reset", "true");
bp.setProperty("logdomain", "imqdbmgr");
bp.setProperty("identityName", Globals.getIdentityName());
BridgeServiceManager.getExportedService(JMSBridgeStore.class, "JMS", bp);
List bnames = jdbcStore.getJMSBridgesByBroker(brokerID, null);
Collections.sort(bnames);
String bname = null;
String currbname = null;
Iterator itr = bnames.iterator();
while (itr.hasNext()) {
bname = (String) itr.next();
if (currbname == null || !currbname.equals(bname)) {
currbname = bname;
bp.setProperty("jmsbridge", bname);
if (jmsbridgeStore != null) {
jmsbridgeStore.closeJMSBridgeStore();
jmsbridgeStore = null;
}
logger.logToAll(logger.INFO, "Backup JMS bridge " + bname);
jmsbridgeStore = (JMSBridgeStore) BridgeServiceManager.getExportedService(JMSBridgeStore.class, "JMS", bp);
List lrs = jdbcStore.getLogRecordsByNameByBroker(bname, brokerID, null);
logger.logToAll(logger.INFO, "\tBackup JMS bridge " + bname + " with " + lrs.size() + " TM log records");
byte[] lr = null;
Iterator itr1 = lrs.iterator();
while (itr1.hasNext()) {
lr = (byte[]) itr1.next();
jmsbridgeStore.storeTMLogRecord(null, lr, currbname, true, null);
}
}
}
} finally {
bkrFS.close();
if (jmsbridgeStore != null) {
jmsbridgeStore.closeJMSBridgeStore();
}
}
}
logger.logToAll(Logger.INFO, "Backup persistent store complete.");
} catch (Throwable ex) {
ex.printStackTrace();
throw new BrokerException(ex.getMessage());
} finally {
assert fileStore != null;
try {
fileStore.close();
} catch (Exception e) {
e.printStackTrace();
}
StoreManager.releaseStore(true);
}
}
use of com.sun.messaging.jmq.jmsserver.persist.api.HABrokerInfo in project openmq by eclipse-ee4j.
the class TransactionDAOImpl method updateTransactionHomeBroker.
/**
* Update the transaction home broker for the specified transaction.
*
* @param conn database connection
* @param txnUID the transaction ID
* @param txnHomeBroker the home broker for a REMOTE txn
* @throws BrokerException if transaction does not exists in the store
*/
@Override
public void updateTransactionHomeBroker(Connection conn, TransactionUID txnUID, BrokerAddress txnHomeBroker) 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, updateTxnHomeBrokerSQL);
Util.setObject(pstmt, 1, txnHomeBroker);
pstmt.setLong(2, txnUID.longValue());
if (Globals.getHAEnabled()) {
pstmt.setString(3, 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("[" + updateTxnHomeBrokerSQL + "]", (IOException) e);
} else if (e instanceof SQLException) {
ex = DBManager.wrapSQLException("[" + updateTxnHomeBrokerSQL + "]", (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);
}
}
}
Aggregations