/*
 * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
 * (the "License"). You may not use this work except in compliance with the License, which is
 * available at www.apache.org/licenses/LICENSE-2.0
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied, as more fully set forth in the License.
 *
 * See the NOTICE file distributed with this work for information regarding copyright ownership.
 */

package alluxio.master.journal;

import alluxio.Constants;
import alluxio.proto.journal.Journal.JournalEntry;

import com.google.protobuf.CodedInputStream;
import com.google.protobuf.InvalidProtocolBufferException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.annotation.concurrent.ThreadSafe;

/**
 * Reads and writes protocol buffer journal entries. The entries contain headers describing their
 * length. This framing is handled entirely by {@link JournalEntry#writeDelimitedTo(OutputStream)}
 * and {@link JournalEntry#parseDelimitedFrom(InputStream)}. This class is thread-safe.
 */
@ThreadSafe
public final class ProtoBufJournalFormatter implements JournalFormatter {
  private static final Logger LOG = LoggerFactory.getLogger(Constants.LOGGER_TYPE);

  /**
   * Constructs a new {@link ProtoBufJournalFormatter}.
   */
  public ProtoBufJournalFormatter() {}

  @Override
  public void serialize(JournalEntry entry, OutputStream outputStream) throws IOException {
    entry.writeDelimitedTo(outputStream);
  }

  @Override
  public JournalInputStream deserialize(final InputStream inputStream) throws IOException {
    return new JournalInputStream() {
      private final byte[] mBuffer = new byte[1024];
      private long mLatestSequenceNumber;

      @Override
      public JournalEntry getNextEntry() throws IOException {
        try {
          int firstByte = inputStream.read();
          if (firstByte == -1) {
            return null;
          }
          int size = CodedInputStream.readRawVarint32(firstByte, inputStream);
          byte[] buffer = size <= mBuffer.length ? mBuffer : new byte[size];
          int bytes = inputStream.read(buffer, 0, size);
          if (bytes < size) {
            throw new InvalidProtocolBufferException(
                "Journal entry was truncated. Expected to read " + size + " bytes but only got "
                    + bytes);
          }
          JournalEntry entry = JournalEntry.parseFrom(new ByteArrayInputStream(buffer, 0, size));
          if (entry != null) {
            mLatestSequenceNumber = entry.getSequenceNumber();
          }
          return entry;
        } catch (InvalidProtocolBufferException e) {
          LOG.warn("Failed to read journal entry", e);
          // Barring IO corruption, this means that the master crashed while writing the last
          // journal entry, so we can ignore this last entry.
          return null;
        }
      }

      @Override
      public void close() throws IOException {
        inputStream.close();
      }

      @Override
      public long getLatestSequenceNumber() {
        return mLatestSequenceNumber;
      }
    };
  }
}

@@ -667,67 +667,8 @@
 am;%0A
-import com.google.protobuf.InvalidProtocolBufferException;%0A
 impo
@@ -1935,24 +1935,8 @@
 n %7B%0A
-        try %7B%0A  
     
@@ -1971,26 +1971,24 @@
 eam.read();%0A
-  
         if (
@@ -2008,34 +2008,32 @@
 -1) %7B%0A          
-  
 return null;%0A   
@@ -2029,38 +2029,34 @@
 n null;%0A        
- 
- 
 %7D%0A
-  
         int size
@@ -2124,18 +2124,16 @@
         
-  
 byte%5B%5D b
@@ -2195,26 +2195,24 @@
 e%5D;%0A        
- 
- 
 int bytes = 
@@ -2250,26 +2250,24 @@
 e);%0A        
-  
 if (bytes %3C 
@@ -2288,68 +2288,17 @@
     
-  throw new InvalidProtocolBufferException(%0A                
+LOG.warn(
 %22Jou
@@ -2388,22 +2388,16 @@
         
-      
 + bytes)
@@ -2400,36 +2400,56 @@
 tes);%0A          
-%7D%0A  
+return null;%0A        %7D%0A%0A
         JournalE
@@ -2532,26 +2532,24 @@
 ));%0A        
-  
 if (entry !=
@@ -2567,18 +2567,16 @@
         
- 
- 
 mLatestS
@@ -2622,30 +2622,26 @@
 ();%0A        
-  %7D%0A  
+%7D%0A
         retu
@@ -2654,305 +2654,8 @@
 ry;%0A
-        %7D catch (InvalidProtocolBufferException e) %7B%0A          LOG.warn(%22Failed to read journal entry%22, e);%0A          // Barring IO corruption, this means that the master crashed while writing the last%0A          // journal entry, so we can ignore this last entry.%0A          return null;%0A        %7D%0A
     

Allow corrupted journal exceptions to propagate