Package org.activeio.journal

Examples of org.activeio.journal.RecordLocation


                        catch (InterruptedException e1) {
                            return;
                        }
   
                        log.debug("Checkpoint started.");
                        RecordLocation newMark = null;
   
                        Iterator iterator = messageStores.values().iterator();
                        while (iterator.hasNext()) {
                            try {
                                JournalMessageStore ms = (JournalMessageStore) iterator.next();
                                RecordLocation mark = ms.checkpoint();
                                if (mark != null && (newMark == null || newMark.compareTo(mark) < 0)) {
                                    newMark = mark;
                                }
                            }
                            catch (Exception e) {
                                log.error("Failed to checkpoint a message store: " + e, e);
                            }
                        }
                       
                        iterator = topicMessageStores.values().iterator();
                        while (iterator.hasNext()) {
                            try {
                                JournalTopicMessageStore ms = (JournalTopicMessageStore) iterator.next();
                                RecordLocation mark = ms.checkpoint();
                                if (mark != null && (newMark == null || newMark.compareTo(mark) < 0)) {
                                    newMark = mark;
                                }
                            }
                            catch (Exception e) {
View Full Code Here


     * @throws InvalidRecordLocationException
     * @throws IllegalStateException
     */
    private void recover() throws IllegalStateException, InvalidRecordLocationException, IOException, JMSException {

        RecordLocation pos = null;
        int transactionCounter = 0;

        log.info("Journal Recovery Started.");

        // While we have records in the journal.
        while ((pos = journal.getNextRecordLocation(pos)) != null) {
            org.activeio.Packet data = journal.read(pos);
            DataInputStream is = new DataInputStream(new PacketInputStream(data));

            // Read the destination and packate from the record.
            String destination = null;
            Packet packet = null;
            try {
                byte type = is.readByte();
                switch (type) {
                    case PACKET_RECORD_TYPE:

                        // Is the current packet part of the destination?
                        destination = is.readUTF();
                        packet = wireFormat.readPacket(data);

                        // Try to replay the packet.
                        if (packet instanceof ActiveMQMessage) {
                            ActiveMQMessage msg = (ActiveMQMessage) packet;
                           
                            JournalMessageStore store = (JournalMessageStore) createMessageStore(destination, msg.getJMSActiveMQDestination().isQueue());
                            if( msg.getTransactionId()!=null ) {
                                transactionStore.addMessage(store, msg, pos);
                            } else {
                                store.replayAddMessage(msg);
                                transactionCounter++;
                            }
                        }
                        else if (packet instanceof MessageAck) {
                            MessageAck ack = (MessageAck) packet;
                            JournalMessageStore store = (JournalMessageStore) createMessageStore(destination, ack.getDestination().isQueue());
                            if( ack.getTransactionId()!=null ) {
                                transactionStore.removeMessage(store, ack, pos);
                            } else {
                                store.replayRemoveMessage(ack);
                                transactionCounter++;
                            }
                        }
                        else {
                            log.error("Unknown type of packet in transaction log which will be discarded: " + packet);
                        }

                        break;
                    case TX_COMMAND_RECORD_TYPE:
                       
                        TxCommand command = new TxCommand();
                        command.setType(is.readByte());
                        command.setWasPrepared(is.readBoolean());
                        switch(command.getType()) {
                          case TxCommand.LOCAL_COMMIT:
                          case TxCommand.LOCAL_ROLLBACK:
                              command.setTransactionId(is.readUTF());
                              break;
                          default:
                              command.setTransactionId(ActiveMQXid.read(is));
                            break;
                        }
                       
                        // Try to replay the packet.
                        switch(command.getType()) {
                          case TxCommand.XA_PREPARE:
                              transactionStore.replayPrepare(command.getTransactionId());
                              break;
                          case TxCommand.XA_COMMIT:
                          case TxCommand.LOCAL_COMMIT:
                              Tx tx = transactionStore.replayCommit(command.getTransactionId(), command.getWasPrepared());
                              // Replay the committed operations.
                              if( tx!=null) {
                                  tx.getOperations();
                                  for (Iterator iter = tx.getOperations().iterator(); iter.hasNext();) {
                                      TxOperation op = (TxOperation) iter.next();
                                      if( op.operationType == TxOperation.ADD_OPERATION_TYPE ) {
                                          op.store.replayAddMessage((ActiveMQMessage) op.data);
                                      }
                                      if( op.operationType == TxOperation.REMOVE_OPERATION_TYPE) {
                                          op.store.replayRemoveMessage((MessageAck) op.data);
                                      }
                                      if( op.operationType == TxOperation.ACK_OPERATION_TYPE) {
                                          JournalAck ack = (JournalAck) op.data;
                                          ((JournalTopicMessageStore)op.store).replayAcknowledge(ack.getSubscription(), new MessageIdentity(ack.getMessageId()));
                                      }
                                  }
                                  transactionCounter++;
                              }
                              break;
                          case TxCommand.LOCAL_ROLLBACK:
                          case TxCommand.XA_ROLLBACK:
                              transactionStore.replayRollback(command.getTransactionId());
                              break;
                        }
                       
                        break;
                       
                    case ACK_RECORD_TYPE:
                       
                        destination = is.readUTF();
                        String subscription = is.readUTF();
                        String messageId = is.readUTF();
                        Object transactionId=null;
                       
                        JournalTopicMessageStore store = (JournalTopicMessageStore) createMessageStore(destination, false);
                        if( transactionId!=null ) {
                            JournalAck ack = new JournalAck(destination, subscription, messageId, transactionId);
                            transactionStore.acknowledge(store, ack, pos);
                        } else {
                            store.replayAcknowledge(subscription, new MessageIdentity(messageId));
                            transactionCounter++;
                        }
                       
                    case COMMAND_RECORD_TYPE:

                        break;
                    default:
                        log.error("Unknown type of record in transaction log which will be discarded: " + type);
                        break;
                }
            }
            finally {
                is.close();
            }
        }

        RecordLocation location = writeCommand("RECOVERED", true);
        journal.setMark(location, true);

        log.info("Journal Recovered: " + transactionCounter + " message(s) in transactions recovered.");
    }
View Full Code Here

     * Not synchronized since the Journal has better throughput if you increase
     * the number of conncurrent writes that it is doing.
     */
    public void addMessage(final ActiveMQMessage message) throws JMSException {
        final boolean debug = log.isDebugEnabled();
        final RecordLocation location = peristenceAdapter.writePacket(destinationName, message, message.isReceiptRequired());
        if( !TransactionManager.isCurrentTransaction() ) {
            if( debug )
                log.debug("Journalled message add: "+message.getJMSMessageID()+" at "+location);
             addMessage(message, location);
        } else {
View Full Code Here

    /**
     */
    public void removeMessage(final MessageAck ack) throws JMSException {

        final boolean debug = log.isDebugEnabled();
        final RecordLocation location = peristenceAdapter.writePacket(destinationName, ack, ack.isReceiptRequired());
        if( !TransactionManager.isCurrentTransaction() ) {
            if( debug )
                log.debug("Journalled message remove: "+ack.getMessageID()+" at "+location);           
            removeMessage(ack, location);
        } else {
View Full Code Here

     */
    private void removeMessage(final MessageAck ack, final RecordLocation location) {
        synchronized (this) {
            lastLocation=location;
            MessageIdentity id = ack.getMessageIdentity();
            RecordLocation msgLocation = (RecordLocation) addedMessageIds.remove(id);
            if (msgLocation == null) {
                removedMessageLocations.add(ack);
            } else {
                removedFromJournal++;
            }
View Full Code Here

     * @return
     * @throws JMSException
     */
    public RecordLocation checkpoint() throws JMSException {

        RecordLocation rc;
        final ArrayList cpRemovedMessageLocations;
        final ArrayList cpActiveJournalLocations;

        // swap out the message hash maps..
        synchronized (this) {
            cpAddedMessageIds = this.addedMessageIds;
            cpRemovedMessageLocations = this.removedMessageLocations;

            this.inFlightTxLocations.removeAll(this.removedMessageLocations);
            this.inFlightTxLocations.removeAll(this.addedMessageIds.values());           
            cpActiveJournalLocations=new ArrayList(inFlightTxLocations);
           
            this.addedMessageIds = new LinkedHashMap();
            this.removedMessageLocations = new ArrayList();           
            log.debug("removedFromJournal="+removedFromJournal);
            removedFromJournal=0;
        }
       
        final boolean debug = log.isDebugEnabled();
        if( debug )
            log.debug("Checkpoint: "+destinationName);
       
       
        final int messagesAdded[]=new int[]{0};
        final int messagesRemoved[]=new int[]{0};

        transactionTemplate.run(new Callback() {
            public void execute() throws Throwable {

                // Checkpoint the added messages.
                Iterator iterator = cpAddedMessageIds.keySet().iterator();
                while (iterator.hasNext()) {
                    MessageIdentity identity = (MessageIdentity) iterator.next();
                    if( debug )
                        log.debug("Adding: "+identity.getMessageID());
                    ActiveMQMessage msg = getCacheMessage(identity);
                    // Pull it out of the journal if we have to.
                    if (msg == null) {
                        RecordLocation location = (RecordLocation) cpAddedMessageIds.get(identity);
                        msg = (ActiveMQMessage) peristenceAdapter.readPacket((RecordLocation) location);
                    }
                    if( msg != null ) {
                        try {
                            longTermStore.addMessage(msg);
View Full Code Here

        // checkpoint tx operations in to long term store until they are committed.

        // But we keep track of the first location of an operation
        // that was associated with an active tx. The journal can not
        // roll over active tx records.       
        RecordLocation rc = null;
        for (Iterator iter = inflightTransactions.values().iterator(); iter.hasNext();) {
            Tx tx = (Tx) iter.next();
            RecordLocation location = tx.location;
            if (rc == null || rc.compareTo(location) < 0) {
                rc = location;
            }
        }
        for (Iterator iter = preparedTransactions.values().iterator(); iter.hasNext();) {
            Tx tx = (Tx) iter.next();
            RecordLocation location = tx.location;
            if (rc == null || rc.compareTo(location) < 0) {
                rc = location;
            }
        }
        return rc;
View Full Code Here

                        catch (InterruptedException e1) {
                            return;
                        }
   
                        log.info("Checkpoint started.");
                        RecordLocation newMark = null;
   
                        Iterator iterator = messageStores.values().iterator();
                        while (iterator.hasNext()) {
                            try {
                                JournalMessageStore ms = (JournalMessageStore) iterator.next();
                                RecordLocation mark = ms.checkpoint();
                                if (mark != null && (newMark == null || newMark.compareTo(mark) < 0)) {
                                    newMark = mark;
                                }
                            }
                            catch (Exception e) {
                                log.error("Failed to checkpoint a message store: " + e, e);
                            }
                        }
                       
                        iterator = topicMessageStores.values().iterator();
                        while (iterator.hasNext()) {
                            try {
                                JournalTopicMessageStore ms = (JournalTopicMessageStore) iterator.next();
                                RecordLocation mark = ms.checkpoint();
                                if (mark != null && (newMark == null || newMark.compareTo(mark) < 0)) {
                                    newMark = mark;
                                }
                            }
                            catch (Exception e) {
View Full Code Here

     * @throws InvalidRecordLocationException
     * @throws IllegalStateException
     */
    private void recover() throws IllegalStateException, InvalidRecordLocationException, IOException, JMSException {

        RecordLocation pos = null;
        int transactionCounter = 0;

        log.info("Journal Recovery Started.");

        // While we have records in the journal.
        while ((pos = journal.getNextRecordLocation(pos)) != null) {
            org.activeio.Packet data = journal.read(pos);
            DataInputStream is = new DataInputStream(new PacketInputStream(data));

            // Read the destination and packate from the record.
            String destination = null;
            Packet packet = null;
            try {
                byte type = is.readByte();
                switch (type) {
                    case PACKET_RECORD_TYPE:

                        // Is the current packet part of the destination?
                        destination = is.readUTF();
                        packet = wireFormat.readPacket(is);

                        // Try to replay the packet.
                        if (packet instanceof ActiveMQMessage) {
                            ActiveMQMessage msg = (ActiveMQMessage) packet;
                           
                            JournalMessageStore store = (JournalMessageStore) createMessageStore(destination, msg.getJMSActiveMQDestination().isQueue());
                            try {
                                store.getLongTermMessageStore().addMessage(msg);
                                transactionCounter++;
                            }
                            catch (Throwable e) {
                                log.error("Recovery Failure: Could not add message: " + msg.getJMSMessageIdentity().getMessageID() + ", reason: " + e, e);
                            }
                        }
                        else if (packet instanceof MessageAck) {
                            MessageAck ack = (MessageAck) packet;
                            JournalMessageStore store = (JournalMessageStore) createMessageStore(destination, ack.getDestination().isQueue());
                            try {
                                store.getLongTermMessageStore().removeMessage(ack);
                                transactionCounter++;
                            }
                            catch (Throwable e) {
                                log.error("Recovery Failure: Could not remove message: " + ack.getMessageIdentity().getMessageID() + ", reason: " + e, e);
                            }
                        }
                        else {
                            log.error("Unknown type of packet in transaction log which will be discarded: " + packet);
                        }

                        break;
                    case TX_COMMAND_RECORD_TYPE:
                       
                        TxCommand command = new TxCommand();
                        command.setType(is.readByte());
                        command.setWasPrepared(is.readBoolean());
                        switch(command.getType()) {
                          case TxCommand.LOCAL_COMMIT:
                          case TxCommand.LOCAL_ROLLBACK:
                              command.setTransactionId(is.readUTF());
                              break;
                          default:
                              command.setTransactionId(ActiveMQXid.read(is));
                            break;
                        }
                       
                        try {
                            // Try to replay the packet.
                            switch(command.getType()) {
                            case TxCommand.XA_PREPARE:
                                transactionStore.checkpointStore.prepare(command.getTransactionId());
                                break;
                              case TxCommand.XA_COMMIT:
                              case TxCommand.LOCAL_COMMIT:
                                  transactionStore.checkpointStore.commit(command.getTransactionId(), command.getWasPrepared());
                                  break;
                              case TxCommand.LOCAL_ROLLBACK:
                              case TxCommand.XA_ROLLBACK:
                                  transactionStore.checkpointStore.rollback(command.getTransactionId());
                                  break;
                            }
                        } catch (XAException e) {
                            log.error("Recovery Failure: Could not replay: " + command + ", reason: " + e, e);
                        }
                       
                        break;
                       
                    case ACK_RECORD_TYPE:
                       
                        String destinationName = is.readUTF();
                        String subscription = is.readUTF();
                        String messageId = is.readUTF();
                       
                        JournalTopicMessageStore store = (JournalTopicMessageStore) createMessageStore(destination, false);
                        try {                           
                            store.getLongTermTopicMessageStore().setLastAcknowledgedMessageIdentity(subscription, new MessageIdentity(messageId));
                        }
                        catch (Throwable e) {
                            log.error("Recovery Failure: Could not ack message: " + messageId + ", reason: " + e, e);
                        }
                       
                    case COMMAND_RECORD_TYPE:

                        break;
                    default:
                        log.error("Unknown type of record in transaction log which will be discarded: " + type);
                        break;
                }
            }
            finally {
                is.close();
            }
        }

        RecordLocation location = writeCommand("RECOVERED", true);
        journal.setMark(location, true);

        log.info("Journal Recovered: " + transactionCounter + " message(s) in transactions recovered.");
    }
View Full Code Here

    /**
     * Not synchronized since the Journal has better throughput if you increase
     * the number of conncurrent writes that it is doing.
     */
    public void addMessage(ActiveMQMessage message) throws JMSException {
        RecordLocation location = peristenceAdapter.writePacket(destinationName, message, message.isReceiptRequired());
        synchronized (this) {
            nextMark = location;
            MessageIdentity id = message.getJMSMessageIdentity();
            addedMessageIds.put(id, location);
        }
View Full Code Here

TOP

Related Classes of org.activeio.journal.RecordLocation

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.