Search in sources :

Example 1 with TransactionInfo

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

the class TransactionDAOImpl method getTransactionInfo.

/**
 * Get the TransactionInfo object.
 *
 * @param conn database connection
 * @param txnUID the transaction ID
 * @return TransactionInfo object
 */
@Override
public TransactionInfo getTransactionInfo(Connection conn, TransactionUID txnUID) throws BrokerException {
    TransactionInfo txnInfo = null;
    long id = txnUID.longValue();
    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, selectTxnInfoSQL);
        pstmt.setLong(1, id);
        rs = pstmt.executeQuery();
        if (rs.next()) {
            int type = rs.getInt(1);
            int state = rs.getInt(2);
            TransactionState txnState = (TransactionState) Util.readObject(rs, 3);
            // update state in TransactionState object
            txnState.setState(state);
            BrokerAddress txnHomeBroker = (BrokerAddress) Util.readObject(rs, 4);
            TransactionBroker[] txnBrokers = (TransactionBroker[]) Util.readObject(rs, 5);
            txnInfo = new TransactionInfo(txnState, txnHomeBroker, txnBrokers, type);
        } else {
            throw new BrokerException(br.getKString(BrokerResources.E_TRANSACTIONID_NOT_FOUND_IN_STORE, String.valueOf(id)), 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 SQLException) {
            ex = DBManager.wrapSQLException("[" + selectTxnInfoSQL + "]", (SQLException) e);
        } else {
            ex = e;
        }
        BrokerException be = new BrokerException(br.getKString(BrokerResources.X_LOAD_TRANSACTION_FAILED, txnUID), ex);
        be.setSQLRecoverable(true);
        throw be;
    } finally {
        if (myConn) {
            Util.close(rs, pstmt, conn, myex);
        } else {
            Util.close(rs, pstmt, null, myex);
        }
    }
    return txnInfo;
}
Also used : TransactionState(com.sun.messaging.jmq.jmsserver.data.TransactionState) IOException(java.io.IOException) BrokerAddress(com.sun.messaging.jmq.jmsserver.core.BrokerAddress) TransactionBroker(com.sun.messaging.jmq.jmsserver.data.TransactionBroker) TransactionInfo(com.sun.messaging.jmq.jmsserver.persist.api.TransactionInfo)

Example 2 with TransactionInfo

use of com.sun.messaging.jmq.jmsserver.persist.api.TransactionInfo 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);
    }
}
Also used : Destination(com.sun.messaging.jmq.jmsserver.core.Destination) HABrokerInfo(com.sun.messaging.jmq.jmsserver.persist.api.HABrokerInfo) Consumer(com.sun.messaging.jmq.jmsserver.core.Consumer) TransactionInfo(com.sun.messaging.jmq.jmsserver.persist.api.TransactionInfo) JMSBridgeStore(com.sun.messaging.bridge.api.JMSBridgeStore) Packet(com.sun.messaging.jmq.io.Packet) TransactionAcknowledgement(com.sun.messaging.jmq.jmsserver.data.TransactionAcknowledgement) ConsumerUID(com.sun.messaging.jmq.jmsserver.core.ConsumerUID) TransactionUID(com.sun.messaging.jmq.jmsserver.data.TransactionUID) FileStore(com.sun.messaging.jmq.jmsserver.persist.file.FileStore) DestinationUID(com.sun.messaging.jmq.jmsserver.core.DestinationUID) SysMessageID(com.sun.messaging.jmq.io.SysMessageID) ChangeRecordInfo(com.sun.messaging.jmq.jmsserver.persist.api.ChangeRecordInfo)

Example 3 with TransactionInfo

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

the class TidList method updateTransactionState.

/**
 * Update the state of a transaction
 *
 * @param id the transaction id to be updated
 * @param tstate the new transaction state
 * @exception BrokerException if an error occurs while persisting or the same transaction id does NOT exists the store
 * already
 * @exception NullPointerException if <code>id</code> is <code>null</code>
 */
void updateTransactionState(TransactionUID id, TransactionState tstate, boolean sync) throws IOException, BrokerException {
    try {
        TransactionInfo txnInfo = (TransactionInfo) tidMap.get(id);
        if (txnInfo == null) {
            logger.log(logger.ERROR, br.E_TRANSACTIONID_NOT_FOUND_IN_STORE, id);
            throw new BrokerException(br.getString(br.E_TRANSACTIONID_NOT_FOUND_IN_STORE, id), Status.NOT_FOUND);
        }
        TransactionState txnState = txnInfo.getTransactionState();
        int ts = tstate.getState();
        if (txnState.getState() != ts) {
            txnState.setState(ts);
            if (tstate.getOnephasePrepare()) {
                txnState.setOnephasePrepare(true);
            }
            if (updateOptimization) {
                // To improve I/O performance, just persist the new
                // state as client data and not the whole record
                byte[] cd = generateClientData(id, txnInfo);
                PHashMapMMF tidMapMMF = (PHashMapMMF) tidMap;
                tidMapMMF.putClientData(id, cd);
            } else {
                tidMap.put(id, txnInfo);
            }
        } else {
            // No need to sync
            sync = false;
        }
        if (sync) {
            sync(id);
        }
    } catch (Exception e) {
        logger.log(logger.ERROR, br.X_UPDATE_TXNSTATE_FAILED, id, e);
        throw new BrokerException(br.getString(br.X_UPDATE_TXNSTATE_FAILED, id), e);
    }
}
Also used : TransactionState(com.sun.messaging.jmq.jmsserver.data.TransactionState) PHashMapMMF(com.sun.messaging.jmq.io.disk.PHashMapMMF) TransactionInfo(com.sun.messaging.jmq.jmsserver.persist.api.TransactionInfo) LoadException(com.sun.messaging.jmq.jmsserver.persist.api.LoadException) PHashMapLoadException(com.sun.messaging.jmq.io.disk.PHashMapLoadException)

Example 4 with TransactionInfo

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

the class TidList method getClusterTransactionBrokers.

/**
 * Return transaction's participant brokers for the specified transaction.
 *
 * @param id id of the transaction whose participant brokers are to be returned
 * @exception BrokerException if the transaction id is not in the store
 */
TransactionBroker[] getClusterTransactionBrokers(TransactionUID id) throws BrokerException {
    TransactionInfo txnInfo = (TransactionInfo) tidMap.get(id);
    if (txnInfo == null) {
        logger.log(Logger.ERROR, BrokerResources.E_TRANSACTIONID_NOT_FOUND_IN_STORE, id);
        throw new BrokerException(br.getString(BrokerResources.E_TRANSACTIONID_NOT_FOUND_IN_STORE, id), Status.NOT_FOUND);
    }
    TransactionBroker[] txnBrokers = txnInfo.getTransactionBrokers();
    if (txnBrokers != null) {
        // Make a copy
        txnBrokers = txnBrokers.clone();
    }
    return txnBrokers;
}
Also used : TransactionBroker(com.sun.messaging.jmq.jmsserver.data.TransactionBroker) TransactionInfo(com.sun.messaging.jmq.jmsserver.persist.api.TransactionInfo)

Example 5 with TransactionInfo

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

the class TidList method loadClientData.

/**
 * When update optimization is enabled, we'll only write out the modified transaction state as client data instead of
 * the whole record (key and value) to improve I/O performance. So when the broker start up and the persisted
 * transaction states are loaded from file, we need to update the TransactionState object with the modified value that
 * is stored in the client data section of the record.
 *
 * @throws PHashMapLoadException if an error occurs while loading data
 */
private void loadClientData() throws PHashMapLoadException {
    if (!updateOptimization) {
        // nothing to do
        return;
    }
    PHashMapLoadException loadException = null;
    Iterator itr = tidMap.entrySet().iterator();
    while (itr.hasNext()) {
        Throwable ex = null;
        Map.Entry entry = (Map.Entry) itr.next();
        Object key = entry.getKey();
        TransactionInfo value = (TransactionInfo) entry.getValue();
        byte[] cData = null;
        try {
            cData = ((PHashMapMMF) tidMap).getClientData(key);
            if (cData != null && cData.length > 0) {
                // 1st byte is the modified state
                int state = cData[0];
                // update txn state
                value.getTransactionState().setState(state);
                // Now read in modified txn broker states
                TransactionBroker[] bkrs = value.getTransactionBrokers();
                if (bkrs != null) {
                    for (int i = 0, len = bkrs.length; i < len; i++) {
                        TransactionBroker bkr = bkrs[i];
                        // update bkr's state
                        // 1 == true
                        boolean isComplete = (cData[i + 1] == 1);
                        bkr.setCompleted(isComplete);
                    }
                }
            }
        } catch (Throwable e) {
            ex = e;
        }
        if (ex != null) {
            PHashMapLoadException le = new PHashMapLoadException("Failed to load client data [cData=" + Arrays.toString(cData) + "]");
            le.setKey(key);
            le.setValue(value);
            le.setNextException(loadException);
            le.initCause(ex);
            loadException = le;
        }
    }
    if (loadException != null) {
        throw loadException;
    }
}
Also used : TransactionBroker(com.sun.messaging.jmq.jmsserver.data.TransactionBroker) PHashMapLoadException(com.sun.messaging.jmq.io.disk.PHashMapLoadException) TransactionInfo(com.sun.messaging.jmq.jmsserver.persist.api.TransactionInfo) PHashMap(com.sun.messaging.jmq.io.disk.PHashMap)

Aggregations

TransactionInfo (com.sun.messaging.jmq.jmsserver.persist.api.TransactionInfo)17 TransactionState (com.sun.messaging.jmq.jmsserver.data.TransactionState)8 LoadException (com.sun.messaging.jmq.jmsserver.persist.api.LoadException)6 PHashMapLoadException (com.sun.messaging.jmq.io.disk.PHashMapLoadException)5 PHashMap (com.sun.messaging.jmq.io.disk.PHashMap)4 TransactionBroker (com.sun.messaging.jmq.jmsserver.data.TransactionBroker)4 TransactionUID (com.sun.messaging.jmq.jmsserver.data.TransactionUID)4 IOException (java.io.IOException)4 SysMessageID (com.sun.messaging.jmq.io.SysMessageID)3 ConsumerUID (com.sun.messaging.jmq.jmsserver.core.ConsumerUID)3 Iterator (java.util.Iterator)3 JMSBridgeStore (com.sun.messaging.bridge.api.JMSBridgeStore)2 Packet (com.sun.messaging.jmq.io.Packet)2 PHashMapMMF (com.sun.messaging.jmq.io.disk.PHashMapMMF)2 Consumer (com.sun.messaging.jmq.jmsserver.core.Consumer)2 Destination (com.sun.messaging.jmq.jmsserver.core.Destination)2 DestinationUID (com.sun.messaging.jmq.jmsserver.core.DestinationUID)2 TransactionAcknowledgement (com.sun.messaging.jmq.jmsserver.data.TransactionAcknowledgement)2 ChangeRecordInfo (com.sun.messaging.jmq.jmsserver.persist.api.ChangeRecordInfo)2 HABrokerInfo (com.sun.messaging.jmq.jmsserver.persist.api.HABrokerInfo)2