Search in sources :

Example 1 with StoreBeingTakenOverException

use of com.sun.messaging.jmq.jmsserver.util.StoreBeingTakenOverException 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 2 with StoreBeingTakenOverException

use of com.sun.messaging.jmq.jmsserver.util.StoreBeingTakenOverException in project openmq by eclipse-ee4j.

the class HAMonitorServiceImpl method init.

@Override
public void init(String clusterid, MQAddress brokerURL, boolean resetTakeoverThenExit) throws Exception {
    DEBUG = (DEBUG || (logger.getLevel() <= Logger.DEBUG));
    fi = FaultInjection.getInjection();
    clusterconfig = Globals.getClusterManager();
    clusterconfig.addEventListener(this);
    mybrokerid = clusterconfig.getLocalBroker().getBrokerName();
    logger.logToAll(logger.INFO, BrokerResources.I_MONITOR_INFO, mybrokerid, brokerURL);
    logger.logToAll(logger.INFO, MONITOR_INTERVAL_PROP + "=" + MONITOR_TIMEOUT / 1000);
    logger.logToAll(logger.INFO, MONITOR_THRESHOLD_PROP + "=" + MAX_MONITOR);
    // validate id is valid
    if (!clusterid.equals(clusterconfig.getClusterId())) {
        logger.log(Logger.ERROR, BrokerResources.E_ERROR_MONITOR_BAD_CID, clusterid, clusterconfig.getClusterId());
        throw new IllegalStateException("Bad Cluster ID " + clusterid);
    }
    // retrieve broker state info
    HAClusteredBroker cb = (HAClusteredBroker) clusterconfig.getLocalBroker();
    // make sure we are valid
    MQAddress mqa = cb.getBrokerURL();
    if (!mqa.equals(brokerURL)) {
        logger.logToAll(Logger.INFO, BrokerResources.I_UPD_STORED_PORT, mybrokerid, brokerURL);
        // we need to validate that there isnt a misconfiguration
        String remotebrokerid = getRemoteBrokerIDFromPortMapper(mqa.getHostName(), mqa.getPort(), mybrokerid);
        if (mybrokerid.equals(remotebrokerid)) {
            logger.log(Logger.ERROR, BrokerResources.E_BID_CONFLICT, mybrokerid);
            Broker.getBroker().exit(1, Globals.getBrokerResources().getKString(BrokerResources.E_BID_CONFLICT, mybrokerid), BrokerEvent.Type.FATAL_ERROR, null, false, false, true);
        }
        // OK, if we made it here, we know that we dont have a previous
        // broker running on the same system with a different brokerid
        // XXX-HA replace with real core
        cb.setBrokerURL(brokerURL);
    }
    cb.updateHeartbeat(true);
    // make sure we werent taking over other stores
    Iterator itr = clusterconfig.getConfigBrokers();
    while (itr.hasNext()) {
        HAClusteredBroker nextb = (HAClusteredBroker) itr.next();
        String bkr = nextb.getTakeoverBroker();
        if (bkr != null && bkr.equals(mybrokerid) && (nextb.getState() == BrokerState.FAILOVER_PENDING || nextb.getState() == BrokerState.FAILOVER_STARTED)) {
            logger.logToAll(logger.INFO, BrokerResources.I_TAKEOVER_RESET, nextb.getBrokerName());
            nextb.setState(BrokerState.FAILOVER_COMPLETE);
        }
    }
    if (resetTakeoverThenExit) {
        logger.logToAll(logger.INFO, Globals.getBrokerResources().getKString(BrokerResources.I_RESET_TAKEOVER_EXIT));
        return;
    }
    // OK now check the state
    BrokerState state = cb.getState();
    String tkovrbkr = cb.getTakeoverBroker();
    if (state == BrokerState.FAILOVER_STARTED || state == BrokerState.FAILOVER_PENDING) {
        logger.log(Logger.WARNING, BrokerResources.W_TAKEOVER_IN_PROGRESS, mybrokerid, tkovrbkr);
        long maxwaitsec = Globals.getConfig().getLongProperty(Globals.IMQ + ".cluster.ha.takeoverWaitTimeout", 300);
        long maxwait = maxwaitsec * 1000L;
        if (maxwait == 0) {
            throw new BrokerException(Globals.getBrokerResources().getKString(BrokerResources.W_WAIT_FOR_BEEN_TAKENOVER_TIMEOUT, String.valueOf(maxwaitsec)));
        }
        long waitinterval = 10 * 1000L;
        long currtime = System.currentTimeMillis();
        long totalwaited = 0L;
        long waittime = ((maxwait < 0 || maxwait > waitinterval) ? waitinterval : maxwait);
        do {
            logger.logToAll(Logger.INFO, BrokerResources.I_STARTUP_PAUSE);
            try {
                Thread.currentThread().sleep(waittime);
                long precurrtime = currtime;
                currtime = System.currentTimeMillis();
                totalwaited += ((currtime - precurrtime) > waittime ? (currtime - precurrtime) : waittime);
            } catch (InterruptedException ex) {
                throw new BrokerException("Waiting for taking over of this broker to complete is interrupted: " + ex.getMessage());
            }
            cb.updateHeartbeat(true);
            state = cb.getState();
            waittime = (maxwait < 0 ? waitinterval : (maxwait - totalwaited));
            if (waittime > waitinterval) {
                waittime = waitinterval;
            }
        } while ((state == BrokerState.FAILOVER_STARTED || state == BrokerState.FAILOVER_PENDING) && waittime > 0);
        if (waittime <= 0 && (state == BrokerState.FAILOVER_STARTED || state == BrokerState.FAILOVER_PENDING)) {
            throw new BrokerException(Globals.getBrokerResources().getKString(BrokerResources.W_WAIT_FOR_BEEN_TAKENOVER_TIMEOUT, String.valueOf(maxwaitsec)));
        }
    }
    try {
        cb.resetTakeoverBrokerReadyOperating();
    } catch (Exception e) {
        state = cb.getState();
        if (state == BrokerState.FAILOVER_STARTED || state == BrokerState.FAILOVER_PENDING) {
            String msg = Globals.getBrokerResources().getKString(BrokerResources.E_CLUSTER_TAKINGOVER_RESTART);
            logger.logStack(logger.ERROR, msg, e);
            throw new StoreBeingTakenOverException(msg, Status.CONFLICT);
        }
        throw e;
    }
    Globals.setStoreSession(cb.getStoreSessionUID());
    logger.logToAll(Logger.INFO, BrokerResources.I_HA_INFO_STRING, String.valueOf(cb.getStoreSessionUID().longValue()), String.valueOf(cb.getBrokerSessionUID().longValue()));
    haMonitor = new HAMonitorTask();
    try {
        new HATimerThread("HAMonitor", haMonitor, MONITOR_TIMEOUT, MONITOR_TIMEOUT);
    } catch (Exception ex) {
        String emsg = "Unable to start HA monitor thread";
        logger.logStack(Logger.ERROR, emsg, ex);
        throw new BrokerException(emsg, ex);
    }
}
Also used : BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) TakeoverLockException(com.sun.messaging.jmq.jmsserver.persist.api.TakeoverLockException) StoreBeingTakenOverException(com.sun.messaging.jmq.jmsserver.util.StoreBeingTakenOverException) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) MQAddress(com.sun.messaging.jmq.io.MQAddress) BrokerMQAddress(com.sun.messaging.jmq.jmsserver.core.BrokerMQAddress) StoreBeingTakenOverException(com.sun.messaging.jmq.jmsserver.util.StoreBeingTakenOverException)

Example 3 with StoreBeingTakenOverException

use of com.sun.messaging.jmq.jmsserver.util.StoreBeingTakenOverException in project openmq by eclipse-ee4j.

the class JMSBGDAOImpl method updateBrokerId.

/**
 * @param conn database connection
 * @param name jmsbridge name
 * @param logger_ can be null
 * @throws KeyNotFoundException if not found StoreBeingTakenOverException if being takeover Exception on any other error
 */
@Override
public void updateBrokerId(Connection conn, String name, String newBrokerId, String expectedBrokerId, java.util.logging.Logger logger_) throws KeyNotFoundException, StoreBeingTakenOverException, Exception {
    Connection myconn = null;
    PreparedStatement pstmt = null;
    Exception myex = null;
    try {
        DBManager dbMgr = DBManager.getDBManager();
        if (conn == null) {
            conn = dbMgr.getConnection(true);
            myconn = conn;
        }
        pstmt = dbMgr.createPreparedStatement(conn, updateBrokerIdSQL);
        pstmt.setString(1, newBrokerId);
        pstmt.setLong(2, System.currentTimeMillis());
        pstmt.setString(3, name);
        pstmt.setString(4, expectedBrokerId);
        if (Globals.getHAEnabled()) {
            pstmt.setString(5, dbMgr.getBrokerID());
        }
        if (pstmt.executeUpdate() == 0) {
            Util.checkBeingTakenOver(conn, dbMgr, logger, logger_);
            throw new KeyNotFoundException("Name " + name + " not found in store");
        }
    } catch (Exception e) {
        myex = e;
        try {
            if ((conn != null) && !conn.getAutoCommit()) {
                conn.rollback();
            }
        } catch (SQLException e1) {
            String emsg = BrokerResources.X_DB_ROLLBACK_FAILED;
            logger.log(Logger.ERROR, emsg, e1);
            Util.logExt(logger_, java.util.logging.Level.SEVERE, emsg, e1);
        }
        throw e;
    } finally {
        closeSQL(null, pstmt, myconn, myex, logger_);
    }
}
Also used : DBManager(com.sun.messaging.jmq.jmsserver.persist.jdbc.DBManager) KeyNotFoundException(com.sun.messaging.bridge.api.KeyNotFoundException) StoreBeingTakenOverException(com.sun.messaging.jmq.jmsserver.util.StoreBeingTakenOverException) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) DupKeyException(com.sun.messaging.bridge.api.DupKeyException) KeyNotFoundException(com.sun.messaging.bridge.api.KeyNotFoundException)

Example 4 with StoreBeingTakenOverException

use of com.sun.messaging.jmq.jmsserver.util.StoreBeingTakenOverException in project openmq by eclipse-ee4j.

the class TMLogRecordDAOJMSBG method updateLogRecord.

/**
 * @param conn database connection
 * @param xid the global xid
 * @param logRecord log record data
 * @param name the jmsbridge name
 * @param callback to obtain updated data
 * @param logger_ can be null
 * @throws KeyNotFoundException if not found and addIfNotExist false StoreBeingTakenOverException if being takeover else
 * Exception on error
 */
@Override
public void updateLogRecord(Connection conn, String xid, byte[] logRecord, String name, UpdateOpaqueDataCallback callback, boolean addIfNotExist, java.util.logging.Logger logger_) throws KeyNotFoundException, StoreBeingTakenOverException, Exception {
    Connection myconn = null;
    PreparedStatement pstmt = null;
    Exception myex = null;
    try {
        DBManager dbMgr = DBManager.getDBManager();
        if (conn == null) {
            conn = dbMgr.getConnection(false);
            myconn = conn;
        }
        if (conn.getAutoCommit()) {
            throw new BrokerException("Broker Internal Error: Unexpected auto commit SQL connection for update TM log record: " + xid);
        }
        byte[] currLogRecord = null;
        currLogRecord = getLogRecord(conn, xid, name, logger_);
        if (currLogRecord == null) {
            if (addIfNotExist) {
                insert(conn, xid, logRecord, name, logger_);
                return;
            }
            throw new KeyNotFoundException("TM log record not found for " + xid);
        }
        byte[] newLogRecord = (byte[]) callback.update(currLogRecord);
        pstmt = dbMgr.createPreparedStatement(conn, updateLogRecordSQL);
        pstmt.setBytes(1, newLogRecord);
        pstmt.setLong(2, System.currentTimeMillis());
        pstmt.setString(3, xid);
        pstmt.setString(4, dbMgr.getBrokerID());
        if (pstmt.executeUpdate() == 0) {
            Util.checkBeingTakenOver(conn, dbMgr, logger, logger_);
            throw new BrokerException(br.getKString("TM Log record not found in store for " + xid), Status.NOT_FOUND);
        }
        if (myconn != null) {
            conn.commit();
        }
    } catch (Exception e) {
        myex = e;
        try {
            if (conn != null && !conn.getAutoCommit()) {
                conn.rollback();
            }
        } catch (SQLException e1) {
            String emsg = BrokerResources.X_DB_ROLLBACK_FAILED;
            logger.log(Logger.ERROR, emsg, e1);
            Util.logExt(logger_, java.util.logging.Level.SEVERE, emsg, e1);
        }
        throw e;
    } finally {
        closeSQL(null, pstmt, myconn, myex, logger_);
    }
}
Also used : DBManager(com.sun.messaging.jmq.jmsserver.persist.jdbc.DBManager) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) DupKeyException(com.sun.messaging.bridge.api.DupKeyException) KeyNotFoundException(com.sun.messaging.bridge.api.KeyNotFoundException) StoreBeingTakenOverException(com.sun.messaging.jmq.jmsserver.util.StoreBeingTakenOverException) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) KeyNotFoundException(com.sun.messaging.bridge.api.KeyNotFoundException)

Aggregations

BrokerException (com.sun.messaging.jmq.jmsserver.util.BrokerException)4 StoreBeingTakenOverException (com.sun.messaging.jmq.jmsserver.util.StoreBeingTakenOverException)4 DupKeyException (com.sun.messaging.bridge.api.DupKeyException)2 KeyNotFoundException (com.sun.messaging.bridge.api.KeyNotFoundException)2 DBManager (com.sun.messaging.jmq.jmsserver.persist.jdbc.DBManager)2 MQAddress (com.sun.messaging.jmq.io.MQAddress)1 BrokerMQAddress (com.sun.messaging.jmq.jmsserver.core.BrokerMQAddress)1 HABrokerInfo (com.sun.messaging.jmq.jmsserver.persist.api.HABrokerInfo)1 TakeoverLockException (com.sun.messaging.jmq.jmsserver.persist.api.TakeoverLockException)1