steveloughran commented on a change in pull request #1208: HADOOP-16423. 
S3Guard fsck: Check metadata consistency between S3 and metadatastore (log)
URL: https://github.com/apache/hadoop/pull/1208#discussion_r321804874
 
 

 ##########
 File path: 
hadoop-tools/hadoop-aws/src/main/java/org/apache/hadoop/fs/s3a/s3guard/S3GuardFsckViolationHandler.java
 ##########
 @@ -0,0 +1,315 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.fs.s3a.s3guard;
+
+import org.apache.hadoop.fs.FileStatus;
+import org.apache.hadoop.fs.s3a.S3AFileStatus;
+import org.apache.hadoop.fs.s3a.S3AFileSystem;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.lang.reflect.InvocationTargetException;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * Violation handler for the S3Guard's fsck.
+ */
+public class S3GuardFsckViolationHandler {
+  private static final Logger LOG = LoggerFactory.getLogger(
+      S3GuardFsckViolationHandler.class);
+
+  // The rawFS and metadataStore are here to prepare when the ViolationHandlers
+  // will not just log, but fix the violations, so they will have access.
+  private S3AFileSystem rawFs;
+  private DynamoDBMetadataStore metadataStore;
+
+  private static String newLine = System.getProperty("line.separator");
+
+  public S3GuardFsckViolationHandler(S3AFileSystem fs,
+      DynamoDBMetadataStore ddbms) {
+
+    this.metadataStore = ddbms;
+    this.rawFs = fs;
+  }
+
+  public void handle(S3GuardFsck.ComparePair comparePair) {
+    if (!comparePair.containsViolation()) {
+      LOG.debug("There is no violation in the compare pair: " + toString());
+      return;
+    }
+
+    StringBuilder sB = new StringBuilder();
+    sB.append(newLine)
+        .append("On path: ").append(comparePair.getPath()).append(newLine);
+
+    // Create a new instance of the handler and use it.
+    for (S3GuardFsck.Violation violation : comparePair.getViolations()) {
+      try {
+        ViolationHandler handler = violation.getHandler()
+            .getDeclaredConstructor(S3GuardFsck.ComparePair.class)
+            .newInstance(comparePair);
+        final String errorStr = handler.getError();
+        sB.append(errorStr);
+      } catch (NoSuchMethodException e) {
+        LOG.error("Can not find declared constructor for handler: {}",
+            violation.getHandler());
+      } catch (IllegalAccessException | InstantiationException | 
InvocationTargetException e) {
+        LOG.error("Can not instantiate handler: {}",
+            violation.getHandler());
+      }
+      sB.append(newLine);
+    }
+    LOG.error(sB.toString());
+  }
+
+  /**
+   * Violation handler abstract class.
+   * This class should be extended for violation handlers.
+   */
+  public static abstract class ViolationHandler {
+    private final PathMetadata pathMetadata;
+    private final S3AFileStatus s3FileStatus;
+    private final S3AFileStatus msFileStatus;
+    private final List<FileStatus> s3DirListing;
+    private final DirListingMetadata msDirListing;
+
+    public ViolationHandler(S3GuardFsck.ComparePair comparePair) {
+      pathMetadata = comparePair.getMsPathMetadata();
+      s3FileStatus = comparePair.getS3FileStatus();
+      if (pathMetadata != null) {
+        msFileStatus = pathMetadata.getFileStatus();
+      } else {
+        msFileStatus = null;
+      }
+      s3DirListing = comparePair.getS3DirListing();
+      msDirListing = comparePair.getMsDirListing();
+    }
+
+    abstract String getError();
+
+    public PathMetadata getPathMetadata() {
+      return pathMetadata;
+    }
+
+    public S3AFileStatus getS3FileStatus() {
+      return s3FileStatus;
+    }
+
+    public S3AFileStatus getMsFileStatus() {
+      return msFileStatus;
+    }
+
+    public List<FileStatus> getS3DirListing() {
+      return s3DirListing;
+    }
+
+    public DirListingMetadata getMsDirListing() {
+      return msDirListing;
+    }
+  }
+
+  /**
+   * The violation handler when there's no matching metadata entry in the MS.
+   */
+  public static class NoMetadataEntry extends ViolationHandler {
+
+    public NoMetadataEntry(S3GuardFsck.ComparePair comparePair) {
+      super(comparePair);
+    }
+
+    @Override
+    public String getError() {
+      return "No PathMetadata for this path in the MS.";
+    }
+  }
+
+  /**
+   * The violation handler when there's no parent entry.
+   */
+  public static class NoParentEntry extends ViolationHandler {
+
+    public NoParentEntry(S3GuardFsck.ComparePair comparePair) {
+      super(comparePair);
+    }
+
+    @Override
+    public String getError() {
+      return "Entry does not have a parent entry (not root)";
+    }
+  }
+
+  /**
+   * The violation handler when the parent of an entry is a file.
+   */
+  public static class ParentIsAFile extends ViolationHandler {
+
+    public ParentIsAFile(S3GuardFsck.ComparePair comparePair) {
+      super(comparePair);
+    }
+
+    @Override
+    public String getError() {
+      return "An entry’s parent is a file";
 
 Review comment:
   I think we could try to explain this better. Does it get raised if there 
really is a file above a file in S3?

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: common-issues-unsubscr...@hadoop.apache.org
For additional commands, e-mail: common-issues-h...@hadoop.apache.org

Reply via email to