Search in sources :

Example 6 with TransactionLogRecord

use of com.sun.messaging.jmq.io.txnlog.TransactionLogRecord in project openmq by eclipse-ee4j.

the class FileStore method initTxnLogger.

// Initialize txn logging class
@Override
public boolean initTxnLogger() throws BrokerException {
    boolean storeNeedsRestart = false;
    if (removeStore || !Globals.txnLogEnabled()) {
        return storeNeedsRestart;
    }
    logger.log(logger.INFO, BrokerResources.I_TXNLOG_ENABLED);
    // create txn log writers
    String filename = null;
    try {
        SizeString filesize = config.getSizeProperty(TXNLOG_FILE_SIZE_PROP, DEFAULT_TXNLOG_FILE_SIZE);
        filename = MSG_LOG_FILENAME;
        msgLogWriter = new FileTransactionLogWriter(rootDir, filename, filesize.getBytes());
        msgLogWriter.setCheckPointListener(this);
        filename = ACK_LOG_FILENAME;
        ackLogWriter = new FileTransactionLogWriter(rootDir, filename, filesize.getBytes());
        ackLogWriter.setCheckPointListener(this);
        if (resetMessage || resetStore) {
            msgLogWriter.reset();
            ackLogWriter.reset();
            txnLoggerInited = true;
            return storeNeedsRestart;
        }
    } catch (IOException ex) {
        logger.logStack(Logger.ERROR, BrokerResources.E_CREATE_TXNLOG_FILE_FAILED, filename, ex);
        throw new BrokerException(br.getString(BrokerResources.E_CREATE_TXNLOG_FILE_FAILED, filename), ex);
    }
    // reconstruct persistence store if needed
    try {
        TransactionLogRecord rec;
        byte[] data;
        ByteArrayInputStream bis;
        DataInputStream dis;
        // Keep track of loaded dst
        HashSet dstLoadedSet = new HashSet();
        // Check to see if we need to process log files
        if (msgLogWriter.playBackRequired()) {
            storeNeedsRestart = true;
            logger.log(logger.FORCE, BrokerResources.I_PROCESS_MSG_TXNLOG);
            // All destinations need to be loaded
            Globals.getCoreLifecycle().initDestinations();
            Globals.getCoreLifecycle().initSubscriptions();
            int count = 0;
            Iterator itr = msgLogWriter.iterator();
            while (itr.hasNext()) {
                // Keep track the number of records processed
                count++;
                // Read in the messages
                rec = (TransactionLogRecord) itr.next();
                int recType = rec.getType();
                if (recType != TransactionLogType.PRODUCE_TRANSACTION) {
                    // Shouldn't happens
                    logger.log(logger.ERROR, BrokerResources.E_PROCESS_TXNLOG_RECORD_FAILED, String.valueOf(rec.getSequence()), "record type " + recType + " is invalid");
                    continue;
                }
                data = rec.getBody();
                bis = new ByteArrayInputStream(data);
                dis = new DataInputStream(bis);
                // Transaction ID
                long tid = dis.readLong();
                String tidStr = String.valueOf(tid);
                logger.log(logger.FORCE, BrokerResources.I_PROCESS_TXNLOG_RECORD, tidStr, String.valueOf(recType));
                // Process all msgs in the txn
                processTxnRecMsgPart(dis, dstLoadedSet);
                // Check to see if we need to commit the txn
                if (tid > 0) {
                    TransactionUID tuid = new TransactionUID(tid);
                    TransactionState state = tidList.getTransactionState(tuid);
                    if (state.getState() != TransactionState.NULL && state.getState() != TransactionState.COMMITTED) {
                        logger.log(logger.FORCE, BrokerResources.I_COMMIT_TXNLOG_RECORD, tidStr);
                        tidList.updateTransactionState(tuid, state, false);
                    }
                }
                dis.close();
                bis.close();
            }
            logger.log(logger.FORCE, BrokerResources.I_LOAD_MSG_TXNLOG, String.valueOf(count));
            logger.flush();
        }
        // of record, we'll just store it the same file for simplicity.
        if (ackLogWriter.playBackRequired()) {
            storeNeedsRestart = true;
            logger.log(logger.FORCE, BrokerResources.I_PROCESS_ACK_TXNLOG);
            // All destinations need to be loaded
            Globals.getCoreLifecycle().initDestinations();
            Globals.getCoreLifecycle().initSubscriptions();
            int count = 0;
            Iterator itr = ackLogWriter.iterator();
            while (itr.hasNext()) {
                // Keep track the number of records processed
                count++;
                // Read in the acks or msgs & acks
                rec = (TransactionLogRecord) itr.next();
                int recType = rec.getType();
                if (!(recType == TransactionLogType.CONSUME_TRANSACTION || recType == TransactionLogType.PRODUCE_AND_CONSUME_TRANSACTION)) {
                    // shouldn't happens
                    logger.log(logger.ERROR, BrokerResources.E_PROCESS_TXNLOG_RECORD_FAILED, String.valueOf(rec.getSequence()), "record type " + recType + " is invalid");
                    continue;
                }
                data = rec.getBody();
                bis = new ByteArrayInputStream(data);
                dis = new DataInputStream(bis);
                // Transaction ID
                long tid = dis.readLong();
                String tidStr = String.valueOf(tid);
                logger.log(logger.FORCE, BrokerResources.I_PROCESS_TXNLOG_RECORD, tidStr, String.valueOf(recType));
                if (recType == TransactionLogType.PRODUCE_AND_CONSUME_TRANSACTION) {
                    // Process all msgs in the txn first!
                    processTxnRecMsgPart(dis, dstLoadedSet);
                }
                // Process all acks in the txn
                processTxnRecAckPart(dis, dstLoadedSet);
                // Check to see if we need to commit the txn
                TransactionUID tuid = new TransactionUID(tid);
                TransactionState state = tidList.getTransactionState(tuid);
                if (state.getState() != TransactionState.NULL && state.getState() != TransactionState.COMMITTED) {
                    logger.log(logger.FORCE, BrokerResources.I_COMMIT_TXNLOG_RECORD, tidStr);
                    tidList.updateTransactionState(tuid, state, false);
                }
                dis.close();
                bis.close();
            }
            logger.log(logger.FORCE, BrokerResources.I_LOAD_ACK_TXNLOG, String.valueOf(count));
            logger.flush();
        }
        if (storeNeedsRestart) {
            // Now unload all the destinations that we've loaded so msgs can be routed correctly later on by the broker
            Iterator itr = dstLoadedSet.iterator();
            while (itr.hasNext()) {
                Destination d = (Destination) itr.next();
                // Sync changes to disk
                syncDestination(d);
                d.unload(true);
            }
            dstLoadedSet = null;
            // Sync changes to txn tables
            tidList.sync(null);
            tidList.syncTransactionAck(null);
            // Reset the txn log after the store is updated & synced
            msgLogWriter.reset();
            ackLogWriter.reset();
            logger.log(logger.FORCE, BrokerResources.I_RECONSTRUCT_STORE_DONE);
            logger.flush();
        }
    } catch (Throwable t) {
        logger.logStack(Logger.ERROR, BrokerResources.E_RECONSTRUCT_STORE_FAILED, t);
        throw new BrokerException(br.getString(BrokerResources.E_RECONSTRUCT_STORE_FAILED), t);
    }
    txnLoggerInited = true;
    return storeNeedsRestart;
}
Also used : TransactionState(com.sun.messaging.jmq.jmsserver.data.TransactionState) Destination(com.sun.messaging.jmq.jmsserver.core.Destination) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) SizeString(com.sun.messaging.jmq.util.SizeString) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) TransactionUID(com.sun.messaging.jmq.jmsserver.data.TransactionUID) SizeString(com.sun.messaging.jmq.util.SizeString) ByteArrayInputStream(java.io.ByteArrayInputStream) TransactionLogRecord(com.sun.messaging.jmq.io.txnlog.TransactionLogRecord) Iterator(java.util.Iterator) FileTransactionLogWriter(com.sun.messaging.jmq.io.txnlog.file.FileTransactionLogWriter) HashSet(java.util.HashSet)

Example 7 with TransactionLogRecord

use of com.sun.messaging.jmq.io.txnlog.TransactionLogRecord in project openmq by eclipse-ee4j.

the class TransactionLogManager method writeTransactionEvent.

public void writeTransactionEvent(TransactionEvent txnEvent) throws BrokerException {
    try {
        byte[] data = txnEvent.writeToBytes();
        TransactionLogRecord record = msgLogWriter.newTransactionLogRecord();
        record.setBody(data);
        msgLogWriter.write(record);
    } catch (IOException ioe) {
        throw new BrokerException("error logging transaction", ioe);
    }
}
Also used : BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) TransactionLogRecord(com.sun.messaging.jmq.io.txnlog.TransactionLogRecord) IOException(java.io.IOException)

Aggregations

TransactionLogRecord (com.sun.messaging.jmq.io.txnlog.TransactionLogRecord)7 IOException (java.io.IOException)5 BrokerException (com.sun.messaging.jmq.jmsserver.util.BrokerException)4 SizeString (com.sun.messaging.jmq.util.SizeString)3 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 FileTransactionLogWriter (com.sun.messaging.jmq.io.txnlog.file.FileTransactionLogWriter)1 Destination (com.sun.messaging.jmq.jmsserver.core.Destination)1 TransactionState (com.sun.messaging.jmq.jmsserver.data.TransactionState)1 TransactionUID (com.sun.messaging.jmq.jmsserver.data.TransactionUID)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInputStream (java.io.DataInputStream)1 ByteBuffer (java.nio.ByteBuffer)1