Search in sources :

Example 6 with TransactionAcknowledgement

use of com.sun.messaging.jmq.jmsserver.data.TransactionAcknowledgement in project openmq by eclipse-ee4j.

the class ConsumerStateDAOImpl method getAllTransactionAcks.

/**
 * Retrieve all transaction acknowledgements for all transactions.
 *
 * @param conn database connection
 * @return HashMap of containing all acknowledgement
 */
@Override
public HashMap getAllTransactionAcks(Connection conn) throws BrokerException {
    HashMap data = new HashMap(100);
    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, selectAllTransactionAcksSQL);
        rs = pstmt.executeQuery();
        while (rs.next()) {
            TransactionUID txnUID = new TransactionUID(rs.getLong(1));
            ConsumerUID conID = new ConsumerUID(rs.getLong(2));
            try {
                SysMessageID msgID = SysMessageID.get(rs.getString(3));
                List ackList = (List) data.get(txnUID);
                if (ackList == null) {
                    // Create a new list of acks for this txn
                    ackList = new ArrayList(25);
                    data.put(txnUID, ackList);
                }
                // Added ack to the list of acks
                ackList.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("[" + selectAllTransactionAcksSQL + "]", (SQLException) e);
        } else {
            ex = e;
        }
        throw new BrokerException(br.getKString(BrokerResources.X_LOAD_TXNACK_FAILED), ex);
    } finally {
        if (myConn) {
            Util.close(rs, pstmt, conn, myex);
        } else {
            Util.close(rs, pstmt, null, myex);
        }
    }
    // Transforms HashMap value to TransactionAcknowledgement[] instead of List
    Set keySet = data.keySet();
    if (!keySet.isEmpty()) {
        Iterator itr = keySet.iterator();
        while (itr.hasNext()) {
            TransactionUID txnUID = (TransactionUID) itr.next();
            List ackList = (List) data.get(txnUID);
            data.put(txnUID, ackList.toArray(new TransactionAcknowledgement[ackList.size()]));
        }
    }
    return data;
}
Also used : TransactionAcknowledgement(com.sun.messaging.jmq.jmsserver.data.TransactionAcknowledgement) ConsumerUID(com.sun.messaging.jmq.jmsserver.core.ConsumerUID) TransactionUID(com.sun.messaging.jmq.jmsserver.data.TransactionUID) SysMessageID(com.sun.messaging.jmq.io.SysMessageID)

Example 7 with TransactionAcknowledgement

use of com.sun.messaging.jmq.jmsserver.data.TransactionAcknowledgement in project openmq by eclipse-ee4j.

the class JDBCStore method storeRemoteTransactionInternal.

public void storeRemoteTransactionInternal(TransactionUID tid, TransactionState ts, TransactionAcknowledgement[] txnAcks, BrokerAddress txnHomeBroker, boolean sync, long storeSession) throws BrokerException {
    if (tid == null) {
        throw new NullPointerException();
    }
    if (Globals.getHAEnabled()) {
        throw new UnsupportedOperationException("Operation not supported by the " + getStoreType() + " store in HA mode");
    }
    if (DEBUG) {
        logger.log(Logger.INFO, "JDBCStore.storeRemoteTransactionInternal(" + tid + ", " + ts + ", " + Arrays.toString(txnAcks) + ", " + txnHomeBroker + ", " + sync + ")");
    }
    Connection conn = null;
    Exception myex = null;
    try {
        conn = dbmgr.getConnection(false);
        Util.RetryStrategy retry = null;
        do {
            try {
                // First, store the txn
                daoFactory.getTransactionDAO().insert(conn, tid, ts, txnHomeBroker, null, TransactionInfo.TXN_REMOTE, storeSession);
                // Now, store the acks if any
                if (txnAcks != null && txnAcks.length > 0) {
                    for (int i = 0, len = txnAcks.length; i < len; i++) {
                        TransactionAcknowledgement ack = txnAcks[i];
                        if (ack.shouldStore()) {
                            daoFactory.getConsumerStateDAO().updateTransaction(conn, ack.getSysMessageID(), ack.getStoredConsumerUID(), tid);
                        }
                    }
                }
                conn.commit();
                return;
            } catch (Exception e) {
                // Exception will be log & re-throw if operation cannot be retry
                if (retry == null) {
                    retry = new Util.RetryStrategy();
                }
                retry.assertShouldRetry(e);
            }
        } while (true);
    } catch (BrokerException e) {
        myex = e;
        throw e;
    } finally {
        Util.close(null, null, conn, myex);
    }
}
Also used : TransactionAcknowledgement(com.sun.messaging.jmq.jmsserver.data.TransactionAcknowledgement) DupKeyException(com.sun.messaging.bridge.api.DupKeyException) KeyNotFoundException(com.sun.messaging.bridge.api.KeyNotFoundException)

Example 8 with TransactionAcknowledgement

use of com.sun.messaging.jmq.jmsserver.data.TransactionAcknowledgement in project openmq by eclipse-ee4j.

the class JDBCStore method updateTransactionStateWithWorkInternal.

public void updateTransactionStateWithWorkInternal(TransactionUID tid, TransactionState ts, TransactionWork txnwork, long storeSessionID, boolean sync) throws BrokerException {
    if (tid == null || ts == null || txnwork == null) {
        throw new NullPointerException();
    }
    if (DEBUG) {
        logger.log(Logger.INFO, "JDBCStore.updateTransactionStateInternal(" + tid + ", " + ts + ", " + ", " + txnwork + ", " + sync + ")");
    }
    Connection conn = null;
    Exception myex = null;
    try {
        conn = dbmgr.getConnection(false);
        Util.RetryStrategy retry = null;
        do {
            boolean inside = false;
            try {
                Iterator<TransactionWorkMessage> itr1 = txnwork.getSentMessages().iterator();
                while (itr1.hasNext()) {
                    TransactionWorkMessage txnmsg = itr1.next();
                    Packet m = txnmsg.getMessage();
                    if (m == null) {
                        continue;
                    }
                    inside = true;
                    daoFactory.getMessageDAO().insert(conn, txnmsg.getDestUID(), m, null, null, storeSessionID, m.getTimestamp(), true, false);
                    inside = false;
                }
                if (FI.FAULT_INJECTION) {
                    try {
                        FI.checkFaultAndThrowBrokerException(FaultInjection.FAULT_TXN_PERSIST_WORK_1_5, null);
                    } catch (BrokerException e) {
                        FI.unsetFault(FI.FAULT_TXN_PERSIST_WORK_1_5);
                        throw e;
                    }
                }
                List<TransactionWorkMessageAck> txnacks = txnwork.getMessageAcknowledgments();
                if (txnacks != null) {
                    Iterator<TransactionWorkMessageAck> itr2 = txnacks.iterator();
                    while (itr2.hasNext()) {
                        TransactionWorkMessageAck txnack = itr2.next();
                        TransactionAcknowledgement ta = txnack.getTransactionAcknowledgement();
                        if (ta != null) {
                            inside = true;
                            daoFactory.getConsumerStateDAO().updateTransaction(conn, ta.getSysMessageID(), ta.getStoredConsumerUID(), tid);
                            inside = false;
                        }
                    }
                }
                inside = true;
                daoFactory.getTransactionDAO().updateTransactionState(conn, tid, ts, false);
                inside = false;
                conn.commit();
                return;
            } catch (Exception e) {
                if (!inside) {
                    try {
                        conn.rollback();
                    } catch (SQLException rbe) {
                        logger.logStack(Logger.WARNING, BrokerResources.X_DB_ROLLBACK_FAILED, rbe);
                    }
                }
                if (retry == null) {
                    retry = new Util.RetryStrategy();
                }
                try {
                    retry.assertShouldRetry(e, conn);
                } catch (RetrySQLRecoverableException ee) {
                    try {
                        Util.close(null, null, conn, ee);
                        conn = dbmgr.getConnection(false);
                    } catch (Exception eee) {
                        logger.logStack(Logger.WARNING, eee.getMessage(), eee);
                        conn = null;
                        if (e instanceof BrokerException) {
                            throw (BrokerException) e;
                        }
                        throw new BrokerException(e.getMessage(), e);
                    }
                }
            }
        } while (true);
    } catch (BrokerException e) {
        myex = e;
        throw e;
    } finally {
        Util.close(null, null, conn, myex);
    }
}
Also used : Packet(com.sun.messaging.jmq.io.Packet) TransactionWorkMessageAck(com.sun.messaging.jmq.jmsserver.data.TransactionWorkMessageAck) TransactionAcknowledgement(com.sun.messaging.jmq.jmsserver.data.TransactionAcknowledgement) DupKeyException(com.sun.messaging.bridge.api.DupKeyException) KeyNotFoundException(com.sun.messaging.bridge.api.KeyNotFoundException) TransactionWorkMessage(com.sun.messaging.jmq.jmsserver.data.TransactionWorkMessage)

Example 9 with TransactionAcknowledgement

use of com.sun.messaging.jmq.jmsserver.data.TransactionAcknowledgement in project openmq by eclipse-ee4j.

the class JDBCStore method updateRemoteTransactionInternal.

public void updateRemoteTransactionInternal(TransactionUID tid, TransactionAcknowledgement[] txnAcks, BrokerAddress txnHomeBroker, boolean sync) throws BrokerException {
    if (tid == null) {
        throw new NullPointerException();
    }
    if (DEBUG) {
        logger.log(Logger.INFO, "JDBCStore.updateRemoteTransactionInternal(" + tid + ", " + Arrays.toString(txnAcks) + ", " + txnHomeBroker + ", " + sync + ")");
    }
    if (!Globals.getHAEnabled()) {
        throw new UnsupportedOperationException("Operation not supported by the " + getStoreType() + " store in non-HA mode");
    }
    Connection conn = null;
    Exception myex = null;
    try {
        conn = dbmgr.getConnection(false);
        Util.RetryStrategy retry = null;
        do {
            try {
                // store the acks if any
                if (txnAcks != null && txnAcks.length > 0) {
                    for (int i = 0, len = txnAcks.length; i < len; i++) {
                        TransactionAcknowledgement ack = txnAcks[i];
                        if (ack.shouldStore()) {
                            daoFactory.getConsumerStateDAO().updateTransaction(conn, ack.getSysMessageID(), ack.getStoredConsumerUID(), tid);
                        }
                    }
                }
                // In HA mode, the txn is owned by another broker
                // so we'll only update the txn home broker
                daoFactory.getTransactionDAO().updateTransactionHomeBroker(conn, tid, txnHomeBroker);
                conn.commit();
                return;
            } catch (Exception e) {
                // Exception will be log & re-throw if operation cannot be retry
                if (retry == null) {
                    retry = new Util.RetryStrategy();
                }
                retry.assertShouldRetry(e);
            }
        } while (true);
    } catch (BrokerException e) {
        myex = e;
        throw e;
    } finally {
        Util.close(null, null, conn, myex);
    }
}
Also used : TransactionAcknowledgement(com.sun.messaging.jmq.jmsserver.data.TransactionAcknowledgement) DupKeyException(com.sun.messaging.bridge.api.DupKeyException) KeyNotFoundException(com.sun.messaging.bridge.api.KeyNotFoundException)

Example 10 with TransactionAcknowledgement

use of com.sun.messaging.jmq.jmsserver.data.TransactionAcknowledgement 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)

Aggregations

TransactionAcknowledgement (com.sun.messaging.jmq.jmsserver.data.TransactionAcknowledgement)17 SysMessageID (com.sun.messaging.jmq.io.SysMessageID)10 ConsumerUID (com.sun.messaging.jmq.jmsserver.core.ConsumerUID)10 TransactionUID (com.sun.messaging.jmq.jmsserver.data.TransactionUID)8 DestinationUID (com.sun.messaging.jmq.jmsserver.core.DestinationUID)6 BrokerException (com.sun.messaging.jmq.jmsserver.util.BrokerException)6 PacketReference (com.sun.messaging.jmq.jmsserver.core.PacketReference)5 Packet (com.sun.messaging.jmq.io.Packet)4 BrokerAddress (com.sun.messaging.jmq.jmsserver.core.BrokerAddress)4 Destination (com.sun.messaging.jmq.jmsserver.core.Destination)4 TransactionBroker (com.sun.messaging.jmq.jmsserver.data.TransactionBroker)4 TransactionList (com.sun.messaging.jmq.jmsserver.data.TransactionList)4 DupKeyException (com.sun.messaging.bridge.api.DupKeyException)3 KeyNotFoundException (com.sun.messaging.bridge.api.KeyNotFoundException)3 Consumer (com.sun.messaging.jmq.jmsserver.core.Consumer)3 TransactionState (com.sun.messaging.jmq.jmsserver.data.TransactionState)3 AckEntryNotFoundException (com.sun.messaging.jmq.jmsserver.util.AckEntryNotFoundException)3 SelectorFormatException (com.sun.messaging.jmq.util.selector.SelectorFormatException)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3