Package org.activeio.journal

Examples of org.activeio.journal.RecordLocation


     * @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());
                            try {
                                // Only add the message if it has not allready been added.
                                ActiveMQMessage t = store.getLongTermMessageStore().getMessage(msg.getJMSMessageIdentity());
                                if( t==null ) {
                                    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 {
                                // Only remove the message if it has not allready been removed.
                                ActiveMQMessage t = store.getLongTermMessageStore().getMessage(ack.getMessageIdentity());
                                if( t!=null ) {
                                    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:
                       
                        destination = 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


          deleteDir(logDirectory);
        //assertTrue( !logDirectory.exists() );
    }
   
    public void testLogFileCreation() throws IOException {
          RecordLocation mark = journal.getMark();
          assertNull(mark);
    }
View Full Code Here

    }
   
    public void testAppendAndRead() throws InvalidRecordLocationException, InterruptedException, IOException {
     
        Packet data1 = createPacket("Hello World 1");
      RecordLocation location1 = journal.write( data1, false);
      Packet data2 = createPacket("Hello World 2");
      RecordLocation location2 = journal.write( data2, false);
      Packet data3  = createPacket("Hello World 3");
      RecordLocation location3 = journal.write( data3, false);
     
      // Now see if we can read that data.
      Packet data;
      data = journal.read(location2);
      assertEquals( data2, data);
      data = journal.read(location1);
      assertEquals( data1, data);
      data = journal.read(location3);
      assertEquals( data3, data);
     
      // Can we cursor the data?
      RecordLocation l=journal.getNextRecordLocation(null);
      assertEquals(0, l.compareTo(location1));
      data = journal.read(l);
      assertEquals( data1, data);

      l=journal.getNextRecordLocation(l);
      assertEquals(0, l.compareTo(location2));
      data = journal.read(l);
      assertEquals( data2, data);

      l=journal.getNextRecordLocation(l);
      assertEquals(0, l.compareTo(location3));
      data = journal.read(l);
      assertEquals( data3, data);
     
      l=journal.getNextRecordLocation(l);
      assertNull(l);
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 {
    boolean sync = message.isReceiptRequired();
    RecordLocation location = peristenceAdapter.writePacket(destinationName, message, sync);
    synchronized(this) {
      nextMark = location;
      addedMessageIds.put(message.getJMSMessageIdentity(), location);
    }
  } 
View Full Code Here

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

    RecordLocation ackLocation = peristenceAdapter.writePacket(destinationName, ack, sync);

    synchronized(this) {
      nextMark = ackLocation;
      if( addedMessageIds.remove(ack.getMessageIdentity())==null ) {
        removedMessageLocations.add(ack);
View Full Code Here

   * @return
   * @throws JMSException
   */
  public RecordLocation checkpoint() throws JMSException {
     
    RecordLocation rc;   
    final HashMap addedMessageIds;
    final ArrayList removedMessageLocations;
   
    // swap out the message hashmaps..
    synchronized(this) {       
        rc = nextMark;
        addedMessageIds = this.addedMessageIds;
      removedMessageLocations = this.removedMessageLocations;
     
      this.nextMark = null;
      this.addedMessageIds = new HashMap();
      this.removedMessageLocations = new ArrayList();
    }
   
    transactionTemplate.run(new Callback() {
      public void execute() throws Throwable {
       
        // Checkpoint the added messages.
        Iterator iterator = addedMessageIds.keySet().iterator();
        while (iterator.hasNext()) {         
          MessageIdentity identity = (MessageIdentity) iterator.next();         
          ActiveMQMessage msg = getCacheMessage(identity);
          // Pull it out of the journal if we have to.
          if( msg==null ) {
              RecordLocation location = (RecordLocation) addedMessageIds.get(identity);
            msg = (ActiveMQMessage) peristenceAdapter.readPacket((RecordLocation)location);
          }         
          longTermStore.addMessage(msg);
        }       
       
View Full Code Here

    /**
     */
    public void setLastAcknowledgedMessageIdentity(String subscription, MessageIdentity messageIdentity) throws JMSException {

        RecordLocation location = peristenceAdapter.writePacket(destinationName, subscription, messageIdentity, false);       
    synchronized(this) {
        nextMark = location;
        ackedLastAckLocations.put(subscription,messageIdentity);
    }
       
View Full Code Here

       
    }
   
    public RecordLocation checkpoint() throws JMSException {
       
    RecordLocation rc = super.checkpoint();   
    final HashMap ackedLastAckLocations;

    // swap out the hashmaps..
    synchronized(this) {
      if( rc==null || rc.compareTo(this.nextMark)<0 ) {
        rc = this.nextMark;
      }         
        ackedLastAckLocations = this.ackedLastAckLocations;
        this.nextMark=null;
        this.ackedLastAckLocations = new HashMap();
View Full Code Here

      // apply to the checkpointStore.
     
      // 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();) {
            RecordLocation location = (RecordLocation) iter.next();
      if( rc==null || rc.compareTo(location)<0 ) {
          rc = location;
      }
        }
      for (Iterator iter = preparedTransactions.values().iterator(); iter.hasNext();) {
            RecordLocation location = (RecordLocation) iter.next();
      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

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.