Search in sources :

Example 1 with HABrokerInfo

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);
        }
    }
}
Also used : HABrokerInfo(com.sun.messaging.jmq.jmsserver.persist.api.HABrokerInfo) PacketReadEOFException(com.sun.messaging.jmq.io.PacketReadEOFException) InvalidPacketException(com.sun.messaging.jmq.io.InvalidPacketException)

Example 2 with HABrokerInfo

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);
        }
    }
}
Also used : HABrokerInfo(com.sun.messaging.jmq.jmsserver.persist.api.HABrokerInfo) IOException(java.io.IOException) IOException(java.io.IOException)

Example 3 with HABrokerInfo

use of com.sun.messaging.jmq.jmsserver.persist.api.HABrokerInfo in project openmq by eclipse-ee4j.

the class UpgradeHAStore method upgradeStore.

void upgradeStore(Connection conn) throws BrokerException {
    // log informational messages
    logger.logToAll(Logger.INFO, br.getString(BrokerResources.I_UPGRADE_HASTORE_IN_PROGRESS, String.valueOf(JDBCStore.STORE_VERSION), brokerID));
    DAOFactory daoFactory = dbMgr.getDAOFactory();
    // Check if HA store exists
    int version = -1;
    try {
        VersionDAO verDAO = daoFactory.getVersionDAO();
        version = verDAO.getStoreVersion(conn);
    } catch (BrokerException e) {
    // Assume store doesn't exist
    }
    boolean createStore = false;
    if (version == JDBCStore.STORE_VERSION) {
        // Check if store has been upgraded
        BrokerDAO bkrDAO = daoFactory.getBrokerDAO();
        HABrokerInfo bkrInfo = bkrDAO.getBrokerInfo(conn, brokerID);
        if (bkrInfo != null) {
            String reason = br.getString(BrokerResources.I_HASTORE_ALREADY_UPGRADED, brokerID);
            throw new BrokerException(br.getKString(BrokerResources.E_UPGRADE_HASTORE_FAILED, reason));
        }
    } else if (version == -1) {
        createStore = true;
    } else {
        // Bad version
        String reason = br.getString(BrokerResources.E_BAD_STORE_VERSION, String.valueOf(version), String.valueOf(JDBCStore.STORE_VERSION));
        throw new BrokerException(br.getKString(BrokerResources.E_UPGRADE_HASTORE_FAILED, reason));
    }
    try {
        // create the tables first
        if (createStore) {
            DBTool.createTables(conn);
        }
    } catch (Throwable e) {
        String url = dbMgr.getCreateDBURL();
        if (url == null || url.length() == 0) {
            url = dbMgr.getOpenDBURL();
        }
        String errorMsg = br.getKString(BrokerResources.E_CREATE_DATABASE_TABLE_FAILED, url);
        logger.logToAll(Logger.ERROR, errorMsg, e);
        throw new BrokerException(errorMsg, e);
    }
    try {
        conn.setAutoCommit(false);
        upgradeStoreSessions(conn);
        upgradeDestinations(conn);
        upgradeInterests(conn);
        upgradeMessages(conn);
        upgradeTxns(conn);
        logger.logToAll(Logger.INFO, br.getString(BrokerResources.I_UPGRADE_STORE_DONE));
    } catch (Exception e) {
        // upgrade failed; log message
        logger.logToAll(Logger.ERROR, BrokerResources.I_REMOVE_UPGRADE_HASTORE_DATA, brokerID);
        try {
            // remove all entries associated w/ the broker and return
            DestinationDAO dstDAO = daoFactory.getDestinationDAO();
            dstDAO.deleteAll(conn);
            ConsumerDAO conDAO = daoFactory.getConsumerDAO();
            conDAO.deleteAll(conn);
            MessageDAO msgDAO = daoFactory.getMessageDAO();
            msgDAO.deleteAll(conn);
            TransactionDAO txnDAO = daoFactory.getTransactionDAO();
            txnDAO.deleteAll(conn);
        } catch (Exception ex) {
            logger.logStack(Logger.ERROR, BrokerResources.X_INTERNAL_EXCEPTION, "Failed to clean up after upgrade failed", ex);
        }
        if (e instanceof BrokerException) {
            throw (BrokerException) e;
        } else {
            throw new BrokerException(br.getKString(BrokerResources.E_UPGRADE_HASTORE_FAILED, e.getMessage()), e);
        }
    }
}
Also used : HABrokerInfo(com.sun.messaging.jmq.jmsserver.persist.api.HABrokerInfo)

Example 4 with HABrokerInfo

use of com.sun.messaging.jmq.jmsserver.persist.api.HABrokerInfo in project openmq by eclipse-ee4j.

the class Util method checkBeingTakenOver.

public static void checkBeingTakenOver(Connection conn, DBManager dbMgr, Logger logger, java.util.logging.Logger logger_) throws BrokerException {
    if (!Globals.getHAEnabled()) {
        return;
    }
    String brokerID = dbMgr.getBrokerID();
    BrokerDAO dao = dbMgr.getDAOFactory().getBrokerDAO();
    if (dao.isBeingTakenOver(conn, brokerID)) {
        BrokerException be = new StoreBeingTakenOverException(Globals.getBrokerResources().getKString(BrokerResources.E_STORE_BEING_TAKEN_OVER));
        try {
            HABrokerInfo bkrInfo = dao.getBrokerInfo(conn, brokerID);
            String emsg = be.getMessage() + "[" + (bkrInfo == null ? "" + brokerID : bkrInfo.toString()) + "]";
            logger.logStack(Logger.ERROR, emsg, be);
            logExt(logger_, java.util.logging.Level.SEVERE, emsg, be);
        } catch (Throwable t) {
        /* Ignore error */
        }
        throw be;
    }
}
Also used : HABrokerInfo(com.sun.messaging.jmq.jmsserver.persist.api.HABrokerInfo) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) StoreBeingTakenOverException(com.sun.messaging.jmq.jmsserver.util.StoreBeingTakenOverException)

Example 5 with HABrokerInfo

use of com.sun.messaging.jmq.jmsserver.persist.api.HABrokerInfo in project openmq by eclipse-ee4j.

the class SFSHABrokerInfoMap method get.

/**
 * Retrieves the HAClusteredBroker associated with the passed in broker id. If the id is not found in the hashtable, the
 * store will be checked.
 *
 * @param key the brokerid to lookup
 * @param update update against store
 * @return the HAClusteredBroker object (or null if one can't be found)
 */
@Override
public Object get(Object key, boolean update) {
    // always check against the backing store
    Object o = super.get(key);
    if (o == null || update) {
        try {
            HABrokerInfo m = Globals.getStore().getBrokerInfo((String) key);
            if (m != null && o == null) {
                HAClusteredBroker cb = new SFSHAClusteredBrokerImpl((String) key, m, parent);
                put(key, cb);
                parent.brokerChanged(ClusterReason.ADDED, cb.getBrokerName(), null, cb, cb.getBrokerSessionUID(), null);
                o = cb;
            }
            if (m != null && update) {
                ((HAClusteredBrokerImpl) o).update(m);
            }
        } catch (BrokerException ex) {
            Globals.getLogger().logStack(Logger.WARNING, "Exception while creating broker entry " + key, ex);
        }
    }
    return o;
}
Also used : HABrokerInfo(com.sun.messaging.jmq.jmsserver.persist.api.HABrokerInfo) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) HAClusteredBroker(com.sun.messaging.jmq.jmsserver.cluster.api.ha.HAClusteredBroker)

Aggregations

HABrokerInfo (com.sun.messaging.jmq.jmsserver.persist.api.HABrokerInfo)28 HAClusteredBroker (com.sun.messaging.jmq.jmsserver.cluster.api.ha.HAClusteredBroker)6 TakeoverLockException (com.sun.messaging.jmq.jmsserver.persist.api.TakeoverLockException)6 HashMap (java.util.HashMap)5 AutoClusterBrokerMap (com.sun.messaging.jmq.jmsserver.cluster.manager.AutoClusterBrokerMap)4 TransactionUID (com.sun.messaging.jmq.jmsserver.data.TransactionUID)4 Iterator (java.util.Iterator)4 Map (java.util.Map)4 TransactionState (com.sun.messaging.jmq.jmsserver.data.TransactionState)3 BrokerException (com.sun.messaging.jmq.jmsserver.util.BrokerException)3 IOException (java.io.IOException)3 JMSBridgeStore (com.sun.messaging.bridge.api.JMSBridgeStore)2 InvalidPacketException (com.sun.messaging.jmq.io.InvalidPacketException)2 Packet (com.sun.messaging.jmq.io.Packet)2 PacketReadEOFException (com.sun.messaging.jmq.io.PacketReadEOFException)2 SysMessageID (com.sun.messaging.jmq.io.SysMessageID)2 Consumer (com.sun.messaging.jmq.jmsserver.core.Consumer)2 ConsumerUID (com.sun.messaging.jmq.jmsserver.core.ConsumerUID)2 Destination (com.sun.messaging.jmq.jmsserver.core.Destination)2 DestinationUID (com.sun.messaging.jmq.jmsserver.core.DestinationUID)2