Reidddddd commented on a change in pull request #633: HBASE-22890 Verify the 
file integrity in persistent IOEngine
URL: https://github.com/apache/hbase/pull/633#discussion_r326013377
 
 

 ##########
 File path: 
hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/bucket/BucketCache.java
 ##########
 @@ -1021,41 +1037,61 @@ void doDrain(final List<RAMQueueEntry> entries) throws 
InterruptedException {
 
   private void persistToFile() throws IOException {
     assert !cacheEnabled;
-    FileOutputStream fos = null;
-    ObjectOutputStream oos = null;
-    try {
+    try (ObjectOutputStream oos = new ObjectOutputStream(
+      new FileOutputStream(persistencePath, false))){
       if (!ioEngine.isPersistent()) {
         throw new IOException("Attempt to persist non-persistent cache 
mappings!");
       }
-      fos = new FileOutputStream(persistencePath, false);
-      oos = new ObjectOutputStream(fos);
+      byte[] checksum = ((PersistentIOEngine) 
ioEngine).calculateChecksum(algorithm);
+      if (checksum != null) {
+        oos.write(ProtobufUtil.PB_MAGIC);
+        oos.writeInt(checksum.length);
+        oos.write(checksum);
+      }
       oos.writeLong(cacheCapacity);
       oos.writeUTF(ioEngine.getClass().getName());
       oos.writeUTF(backingMap.getClass().getName());
       oos.writeObject(deserialiserMap);
       oos.writeObject(backingMap);
-    } finally {
-      if (oos != null) oos.close();
-      if (fos != null) fos.close();
     }
   }
 
   @SuppressWarnings("unchecked")
-  private void retrieveFromFile(int[] bucketSizes) throws IOException, 
BucketAllocatorException,
+  private void retrieveFromFile(int[] bucketSizes) throws IOException,
       ClassNotFoundException {
     File persistenceFile = new File(persistencePath);
     if (!persistenceFile.exists()) {
       return;
     }
     assert !cacheEnabled;
-    FileInputStream fis = null;
     ObjectInputStream ois = null;
     try {
       if (!ioEngine.isPersistent())
         throw new IOException(
             "Attempt to restore non-persistent cache mappings!");
-      fis = new FileInputStream(persistencePath);
-      ois = new ObjectInputStream(fis);
+      ois = new ObjectInputStream(new FileInputStream(persistencePath));
+      int pblen = ProtobufUtil.lengthOfPBMagic();
+      byte[] pbuf = new byte[pblen];
+      int read = ois.read(pbuf);
+      if (read != pblen) {
+        throw new IOException("Incorrect number of bytes read while checking 
for protobuf magic "
+          + "number. Requested=" + pblen + ", Received= " + read + ", File=" + 
persistencePath);
+      }
+      if (Bytes.equals(ProtobufUtil.PB_MAGIC, pbuf)) {
+        int length = ois.readInt();
+        byte[] persistentChecksum = new byte[length];
+        int readLen = ois.read(persistentChecksum);
+        if (readLen != length || !((PersistentIOEngine) 
ioEngine).verifyFileIntegrity(
+            persistentChecksum, algorithm)) {
+          LOG.warn("Can't restore from file because of verification failed.");
 
 Review comment:
   Separate the if branch? `readLen != length` doesn't mean `Can't restore from 
file because of verification failed`

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to