Package org.activeio.journal

Examples of org.activeio.journal.RecordLocation


        oos.writeUTF(queue);
        oos.write(id.asByteArray());
        oos.writeObject(obj);
        oos.close();
        Packet packet = new ByteArrayPacket(baos.toByteArray());
        RecordLocation loc = journal.write(packet, false);
        synchronized (markLock) {
            marks.put(id, loc);
            pendingMarks.add(loc);
        }
        return id;
View Full Code Here


     * @see org.mule.transaction.xa.queue.QueuePersistenceStrategy#load(java.lang.Object)
     */
    public Object load(String queue, Object id) throws IOException
    {
        try {
            RecordLocation loc = (RecordLocation)marks.get(id);
            Packet packet = journal.read(loc);
            ObjectInputStream ois = new ObjectInputStream(
                    new ByteArrayInputStream(packet.sliceAsBytes()));
            ois.readByte();
            ois.readUTF();
View Full Code Here

    {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(256);
        baos.write(DELETE_BYTE);
        baos.write(((UUID)id).asByteArray());
        Packet packet = new ByteArrayPacket(baos.toByteArray());
        RecordLocation loc = journal.write(packet, false);
        synchronized (markLock) {
            unusedMarks.add(loc);
            loc = (RecordLocation)marks.remove(id);
            if (loc != null) {
                pendingMarks.remove(loc);
View Full Code Here

     * @see org.mule.transaction.xa.queue.QueuePersistenceStrategy#restore()
     */
    public List restore() throws IOException
    {
        Map results = new HashMap();
        RecordLocation pos = null;
        logger.info("Journal Recovery Started.");
        try {
            synchronized (markLock) {
                // While we have records in the journal.
                while ((pos = journal.getNextRecordLocation(pos)) != null) {
                    Packet packet = journal.read(pos);
                    byte[] buffer = packet.sliceAsBytes();
                    if (buffer[0] == DELETE_BYTE) {
                        UUID id = new UUID(buffer, 1);
                        results.remove(id);
                        unusedMarks.add(pos);
                        RecordLocation loc = (RecordLocation)marks.remove(id);
                        if (loc != null) {
                            pendingMarks.remove(loc);
                            unusedMarks.add(loc);
                        }
                        markLock.notify();
View Full Code Here

                synchronized (markLock) {
                    try {
                        SortedSet m;
                        if (overflowLocation != null) {
                            RecordLocation overflowLoc = overflowLocation;
                            overflowLocation = null;
                            m = pendingMarks.headSet(overflowLoc);
                            if (m != null && m.size() > 0) {
                                logger.trace("Relocating " + m.size() + " records");
                                int nbRecord = 0;
                                while (m.size() > 0) {
                                    RecordLocation loc = (RecordLocation)m.first();
                                    Packet packet = journal.read(loc);
                                    RecordLocation newLoc = journal.write(packet, false);
                                    ObjectInputStream ois = new ObjectInputStream(
                                            new ByteArrayInputStream(packet.sliceAsBytes()));
                                    ois.readByte();
                                    String queue = ois.readUTF();
                                    byte[] buf = new byte[UUID_LENGTH];
                                    ois.read(buf);
                                    UUID id = new UUID(buf);

                                    if (++nbRecord % 10 == 0) {
                                        RecordLocation n = journal.getNextRecordLocation(loc);
                                        journal.setMark(n != null ? n : loc, false);
                                        // unusedMarks.headSet(loc).clear();
                                    }
                                    unusedMarks.add(loc);
                                    pendingMarks.add(newLoc);
                                    marks.put(id, newLoc);
                                    m.remove(loc);
                                }
                            }
                        }
                        // No more pending marks
                        // so clear all unused marks and mark to last
                        if (pendingMarks.isEmpty()) {
                            m = unusedMarks;
                            // There are pending marks
                        }
                        else {
                            RecordLocation pendingMark = (RecordLocation)pendingMarks.first();
                            m = unusedMarks.headSet(pendingMark);
                        }
                        // Is there anything to do ?
                        if (!m.isEmpty()) {
                            RecordLocation last = (RecordLocation)m.last();
                            RecordLocation n = journal.getNextRecordLocation((RecordLocation)m.last());
                            if (n == null) {
                                n = last;
                            }
                            if (logger.isDebugEnabled()) {
                                logger.debug("Marking to " + n + " / "
                                        + (pendingMarks.isEmpty() ? "null" : pendingMarks.last()));
                            }
                            if (journal.getMark() == null || n.compareTo(journal.getMark()) > 0) {
                                journal.setMark(n, false);
                            }
                            m.clear();
                        }
                    }
View Full Code Here

                        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

                        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.
                              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

                        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

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.