use of com.sun.messaging.jmq.jmsserver.data.TransactionAcknowledgement in project openmq by eclipse-ee4j.
the class UpgradeStore method upgradeTxnAcks.
/**
* Upgrade transaction acknowledgements from version 350/370 table to current format (410)
*/
private void upgradeTxnAcks(Connection conn) throws BrokerException {
if (oldStoreVersion == JDBCStore.OLD_STORE_VERSION_400) {
// In 400, ack table has been folded into consumer state table
return;
}
ConsumerStateDAO stateDAO = dbMgr.getDAOFactory().getConsumerStateDAO();
// SQL to select all acknowledgements from version 350/370 table
String getAllTxnAcksFromOldSQL = "SELECT atbl." + TTXNACK_CTUID + ", " + TTXNACK_CACK + " FROM " + oldTxnTable + " ttbl, " + oldAckTable + " atbl" + " WHERE ttbl." + TTXN_CTUID + " = atbl." + TTXNACK_CTUID;
// SQL to insert acknowledgements to consumer state table;
// for 400, txn ack is represent as a column in consumer state table.
String insertTxnAckSQL = new StringBuilder(128).append("UPDATE ").append(stateDAO.getTableName()).append(" SET ").append(ConsumerStateDAO.TRANSACTION_ID_COLUMN).append(" = ? ").append(" WHERE ").append(ConsumerStateDAO.MESSAGE_ID_COLUMN).append(" = ?").append(" AND ").append(ConsumerStateDAO.CONSUMER_ID_COLUMN).append(" = ?").toString();
boolean dobatch = dbMgr.supportsBatchUpdates();
PreparedStatement pstmt = null;
Statement stmt = null;
ResultSet rs = null;
TransactionUID tid = null;
Exception myex = null;
try {
pstmt = dbMgr.createPreparedStatement(conn, insertTxnAckSQL);
stmt = conn.createStatement();
rs = dbMgr.executeQueryStatement(stmt, getAllTxnAcksFromOldSQL);
while (rs.next()) {
long id = rs.getLong(1);
tid = new TransactionUID(id);
TransactionAcknowledgement ack = (TransactionAcknowledgement) Util.readObject(rs, 2);
// insert in new table
try {
pstmt.setLong(1, id);
pstmt.setString(2, ack.getSysMessageID().toString());
pstmt.setLong(3, ack.getStoredConsumerUID().longValue());
if (dobatch) {
pstmt.addBatch();
} else {
pstmt.executeUpdate();
}
} catch (SQLException e) {
SQLException ex = DBManager.wrapSQLException("[" + insertTxnAckSQL + "]", e);
throw ex;
}
}
if (dobatch) {
pstmt.executeBatch();
}
conn.commit();
if (store.upgradeNoBackup()) {
dropTable(conn, oldTxnTable);
dropTable(conn, oldAckTable);
}
} catch (Exception e) {
myex = e;
String errorMsg = br.getKString(BrokerResources.X_JDBC_UPGRADE_TXNACK_FAILED, (tid == null ? "loading" : tid.toString()));
logger.logStack(Logger.ERROR, errorMsg, e);
throw new BrokerException(errorMsg, e);
} finally {
Util.close(rs, stmt, null, myex);
Util.close(null, pstmt, null, myex);
}
}
use of com.sun.messaging.jmq.jmsserver.data.TransactionAcknowledgement in project openmq by eclipse-ee4j.
the class DBTool method doRestore.
/*
* Restore the JDBC store from filebased backup files.
*/
private void doRestore() 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, "Restore persistent store for HA cluster " + clusterID + " from backup dir: " + backupDir);
final FileStore fileStore = new FileStore(backupDir, false);
// Brokers table.
JDBCStore jdbcStore = null;
try {
// Re-create the jdbc store
doRecreate();
jdbcStore = (JDBCStore) StoreManager.getStore();
/*
* For data that are not broker specific, i.e. properties, change records, consumers, brokers, and sessions, we will
* retrieve those data from the top level directory (a.k.a cluster filestore).
*/
// Properties table
List haBrokers = null;
Properties properties = fileStore.getAllProperties();
Iterator propItr = properties.entrySet().iterator();
while (propItr.hasNext()) {
Map.Entry entry = (Map.Entry) propItr.next();
String name = (String) entry.getKey();
if (name.equals(STORE_PROPERTY_HABROKERS)) {
// Retrieve all HABrokerInfo from a property
haBrokers = (List) entry.getValue();
} else {
jdbcStore.updateProperty(name, entry.getValue(), false);
}
}
propItr = null;
properties = null;
if (haBrokers == null || haBrokers.isEmpty()) {
throw new BrokerException(br.getKString(BrokerResources.X_LOAD_ALL_BROKERINFO_FAILED));
}
// Configuration Change Record table
List<ChangeRecordInfo> records = fileStore.getAllConfigRecords();
for (int i = 0, len = records.size(); i < len; i++) {
jdbcStore.storeConfigChangeRecord(records.get(i).getTimestamp(), records.get(i).getRecord(), false);
}
records = null;
// Consumer table
Consumer[] consumerArray = fileStore.getAllInterests();
for (int i = 0; i < consumerArray.length; i++) {
jdbcStore.storeInterest(consumerArray[i], false);
}
consumerArray = null;
// Broker & Session table.
Iterator bkrItr = haBrokers.iterator();
while (bkrItr.hasNext()) {
HABrokerInfo bkrInfo = (HABrokerInfo) bkrItr.next();
jdbcStore.addBrokerInfo(bkrInfo, false);
}
/*
* For each broker in the cluster, we will retrieve broker specific data, destinations, messages, transactions,
* acknowledgements from their own filestore (a.k.a broker filestore).
*/
bkrItr = haBrokers.iterator();
while (bkrItr.hasNext()) {
// Backup data for each broker
HABrokerInfo bkrInfo = (HABrokerInfo) bkrItr.next();
String brokerID = bkrInfo.getId();
long sessionID = bkrInfo.getSessionID();
logger.logToAll(Logger.INFO, "Restore persistent data for broker " + brokerID);
FileStore bkrFS = null;
JMSBridgeStore jmsbridgeStore = null;
try {
String instanceRootDir = backupDir + File.separator + brokerID;
bkrFS = new FileStore(instanceRootDir, false);
// Destination table.
Destination[] dstArray = bkrFS.getAllDestinations();
for (int i = 0, len = dstArray.length; i < len; i++) {
DestinationUID did = dstArray[i].getDestinationUID();
Destination dst = jdbcStore.getDestination(did);
if (dst == null) {
// Store the destination if not found
jdbcStore.storeDestination(dstArray[i], sessionID);
}
}
// Retrieve messages for each destination.
for (int i = 0, len = dstArray.length; i < len; i++) {
for (Enumeration e = bkrFS.messageEnumeration(dstArray[i]); 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 = bkrFS.getInterestStates(did, mid);
if (stateMap == null || stateMap.isEmpty()) {
jdbcStore.storeMessage(did, message, null, null, sessionID, 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++;
}
jdbcStore.storeMessage(did, message, iids, states, sessionID, false);
}
}
}
// Transaction table
Collection txnList = bkrFS.getTransactions(brokerID);
Iterator txnItr = txnList.iterator();
while (txnItr.hasNext()) {
TransactionUID tid = (TransactionUID) txnItr.next();
TransactionInfo txnInfo = bkrFS.getTransactionInfo(tid);
TransactionAcknowledgement[] txnAck = bkrFS.getTransactionAcks(tid);
jdbcStore.storeTransaction(tid, txnInfo, sessionID);
for (int i = 0, len = txnAck.length; i < len; i++) {
jdbcStore.storeTransactionAck(tid, txnAck[i], false);
}
}
/**
************************************************
* JMSBridge
*************************************************
*/
Properties bp = new Properties();
bp.setProperty("instanceRootDir", instanceRootDir);
bp.setProperty("reset", "false");
bp.setProperty("logdomain", "imqdbmgr");
jmsbridgeStore = (JMSBridgeStore) BridgeServiceManager.getExportedService(JMSBridgeStore.class, "JMS", bp);
if (jmsbridgeStore != null) {
List bnames = jmsbridgeStore.getJMSBridges(null);
String bname = null;
Iterator itr = bnames.iterator();
while (itr.hasNext()) {
bname = (String) itr.next();
jdbcStore.addJMSBridge(bname, true, null);
}
jmsbridgeStore.closeJMSBridgeStore();
jmsbridgeStore = null;
bname = null;
itr = bnames.iterator();
while (itr.hasNext()) {
bname = (String) itr.next();
bp.setProperty("jmsbridge", bname);
if (jmsbridgeStore != null) {
jmsbridgeStore.closeJMSBridgeStore();
jmsbridgeStore = null;
}
logger.logToAll(logger.INFO, "Restore JMS bridge " + bname);
jmsbridgeStore = (JMSBridgeStore) BridgeServiceManager.getExportedService(JMSBridgeStore.class, "JMS", bp);
List xids = jmsbridgeStore.getTMLogRecordKeysByName(bname, null);
logger.logToAll(logger.INFO, "\tRestore JMS bridge " + bname + " with " + xids.size() + " TM log records");
String xid = null;
byte[] lr = null;
Iterator itr1 = xids.iterator();
while (itr1.hasNext()) {
xid = (String) itr1.next();
lr = jmsbridgeStore.getTMLogRecord(xid, bname, null);
if (lr == null) {
logger.logToAll(Logger.INFO, "JMSBridge TM log record not found for " + xid);
continue;
}
jdbcStore.storeTMLogRecord(xid, lr, bname, true, null);
}
}
}
} finally {
bkrFS.close();
if (jmsbridgeStore != null) {
jmsbridgeStore.closeJMSBridgeStore();
}
}
}
logger.logToAll(Logger.INFO, "Restore 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.data.TransactionAcknowledgement in project openmq by eclipse-ee4j.
the class ConsumerStateDAOImpl method getTransactionAcks.
/**
* Retrieve all transaction acknowledgements for the specified transaction ID.
*
* @param conn database connection
* @param txnUID the transaction ID
* @return List of transaction acks
*/
@Override
public List getTransactionAcks(Connection conn, TransactionUID txnUID) throws BrokerException {
List data = new ArrayList();
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, selectTransactionAcksSQL);
pstmt.setLong(1, txnUID.longValue());
rs = pstmt.executeQuery();
while (rs.next()) {
ConsumerUID conID = new ConsumerUID(rs.getLong(1));
try {
SysMessageID msgID = SysMessageID.get(rs.getString(2));
data.add(new TransactionAcknowledgement(msgID, conID, conID));
} catch (Exception e) {
// fail to parse one object; just log it
logger.logStack(Logger.ERROR, BrokerResources.X_PARSE_TXNACK_FAILED, txnUID, e);
}
}
} 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("[" + selectTransactionAcksSQL + "]", (SQLException) e);
} else {
ex = e;
}
throw new BrokerException(br.getKString(BrokerResources.X_LOAD_ACKS_FOR_TXN_FAILED, txnUID), 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.data.TransactionAcknowledgement in project openmq by eclipse-ee4j.
the class BrokerConsumers method commitRecoveryRemoteTransaction.
private boolean commitRecoveryRemoteTransaction(TransactionList translist, TransactionUID tid, com.sun.messaging.jmq.jmsserver.core.BrokerAddress from) throws BrokerException {
logger.log(logger.INFO, "Committing recovery remote transaction " + tid + " from " + from);
TransactionBroker ba = translist.getRemoteTransactionHomeBroker(tid);
BrokerAddress currba = (ba == null) ? null : ba.getCurrentBrokerAddress();
if (currba == null || !currba.equals(from)) {
logger.log(logger.WARNING, "Committed remote transaction " + tid + " home broker " + ba + " not " + from);
}
RemoteTransactionAckEntry[] tae = translist.getRecoveryRemoteTransactionAcks(tid);
if (tae == null) {
logger.log(logger.WARNING, "No recovery transaction acks to process for committing remote transaction " + tid);
return true;
}
boolean done = true;
for (int j = 0; j < tae.length; j++) {
if (tae[j].processed()) {
continue;
}
TransactionAcknowledgement[] tas = tae[j].getAcks();
for (int i = 0; i < tas.length; i++) {
SysMessageID sysid = tas[i].getSysMessageID();
com.sun.messaging.jmq.jmsserver.core.ConsumerUID uid = tas[i].getConsumerUID();
com.sun.messaging.jmq.jmsserver.core.ConsumerUID suid = tas[i].getStoredConsumerUID();
if (suid == null) {
suid = uid;
}
// PART
PacketReference ref = DL.get(null, sysid);
if (ref == null || ref.isDestroyed() || ref.isInvalid()) {
continue;
}
try {
if (ref.acknowledged(uid, suid, true, true)) {
ref.getDestination().removeMessage(ref.getSysMessageID(), RemoveReason.ACKNOWLEDGED);
}
} catch (Exception ex) {
done = false;
logger.logStack(Logger.ERROR, Globals.getBrokerResources().E_INTERNAL_BROKER_ERROR, ex.getMessage(), ex);
}
}
}
return done;
}
use of com.sun.messaging.jmq.jmsserver.data.TransactionAcknowledgement in project openmq by eclipse-ee4j.
the class BrokerConsumers method rollbackRecoveryRemoteTransaction.
private void rollbackRecoveryRemoteTransaction(TransactionList translist, TransactionUID tid, com.sun.messaging.jmq.jmsserver.core.BrokerAddress from) throws BrokerException {
logger.log(logger.INFO, "Rolling back recovery remote transaction " + tid + " from " + from);
TransactionState ts = translist.getRemoteTransactionState(tid);
if (ts == null || ts.getState() != TransactionState.ROLLEDBACK) {
throw new BrokerException(Globals.getBrokerResources().E_INTERNAL_BROKER_ERROR, "Unexpected broker state " + ts + " for processing Rolledback remote transaction " + tid);
}
TransactionBroker ba = translist.getRemoteTransactionHomeBroker(tid);
BrokerAddress currba = (ba == null) ? null : ba.getCurrentBrokerAddress();
if (currba == null || !currba.equals(from)) {
logger.log(logger.WARNING, "Rolledback remote transaction " + tid + " home broker " + ba + " not " + from);
}
RemoteTransactionAckEntry[] tae = translist.getRecoveryRemoteTransactionAcks(tid);
if (tae == null) {
logger.log(logger.WARNING, "No recovery transaction acks to process for rolling back remote transaction " + tid);
return;
}
for (int j = 0; j < tae.length; j++) {
if (tae[j].processed()) {
continue;
}
TransactionAcknowledgement[] tas = tae[j].getAcks();
for (int i = 0; i < tas.length; i++) {
SysMessageID sysid = tas[i].getSysMessageID();
com.sun.messaging.jmq.jmsserver.core.ConsumerUID cuid = tas[i].getConsumerUID();
com.sun.messaging.jmq.jmsserver.core.ConsumerUID suid = tas[i].getStoredConsumerUID();
if (suid == null) {
suid = cuid;
}
// PART
PacketReference ref = DL.get(null, sysid);
if (ref == null) {
if (getDEBUG()) {
logger.log(logger.INFO, "[" + sysid + ":" + cuid + "] reference not found in rolling back recovery remote transaction " + tid);
}
continue;
}
ref.getDestination().forwardOrphanMessage(ref, suid);
}
}
}
Aggregations