bgaborg commented on a change in pull request #1003: HADOOP-16384: Avoid 
inconsistencies between DDB and S3
URL: https://github.com/apache/hadoop/pull/1003#discussion_r302422633
 
 

 ##########
 File path: 
hadoop-tools/hadoop-aws/src/main/java/org/apache/hadoop/fs/s3a/s3guard/DumpS3GuardTable.java
 ##########
 @@ -0,0 +1,691 @@
+/*
+ * 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
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 javax.annotation.Nullable;
+import java.io.Closeable;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Date;
+import java.util.List;
+
+import com.amazonaws.services.dynamodbv2.xspec.ExpressionSpecBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.hadoop.classification.InterfaceAudience;
+import org.apache.hadoop.classification.InterfaceStability;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FileStatus;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.fs.RemoteIterator;
+import org.apache.hadoop.fs.s3a.Listing;
+import org.apache.hadoop.fs.s3a.S3AFileStatus;
+import org.apache.hadoop.fs.s3a.S3AFileSystem;
+import org.apache.hadoop.fs.s3a.S3ALocatedFileStatus;
+import org.apache.hadoop.fs.s3a.S3ListRequest;
+import org.apache.hadoop.service.Service;
+import org.apache.hadoop.service.launcher.LauncherExitCodes;
+import org.apache.hadoop.service.launcher.ServiceLaunchException;
+import org.apache.hadoop.service.launcher.ServiceLauncher;
+import org.apache.hadoop.util.DurationInfo;
+import org.apache.hadoop.util.ExitUtil;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.apache.hadoop.fs.s3a.S3AUtils.ACCEPT_ALL;
+
+/**
+ * This is a low-level diagnostics entry point which does a CVE/TSV dump of
+ * the DDB state.
+ * As it also lists the filesystem, it actually changes the state of the store
+ * during the operation.
+ */
+@InterfaceAudience.Private
+@InterfaceStability.Unstable
+public class DumpS3GuardTable extends AbstractS3GuardDiagnostic {
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(DumpS3GuardTable.class);
+
+  /**
+   * Application name.
+   */
+  public static final String NAME = "DumpS3GuardTable";
+
+  /**
+   * Usage.
+   */
+  private static final String USAGE_MESSAGE = NAME
+      + " <filesystem> <dest-file>";
+
+  /**
+   * Suffix for the flat list: {@value}.
+   */
+  public static final String FLAT_CSV = "-flat.csv";
+
+  /**
+   * Suffix for the raw S3 dump: {@value}.
+   */
+  public static final String RAW_CSV = "-s3.csv";
+
+  /**
+   * Suffix for the DDB scan: {@value}.
+   */
+  public static final String SCAN_CSV = "-scan.csv";
+
+  /**
+   * Suffix for the second DDB scan: : {@value}.
+   */
+  public static final String SCAN2_CSV = "-scan-2.csv";
+
+  /**
+   * Suffix for the treewalk scan of the S3A Filesystem: {@value}.
+   */
+  public static final String TREE_CSV = "-tree.csv";
+
+  /**
+   * Suffix for a recursive treewalk through the metastore: {@value}.
+   */
+  public static final String STORE_CSV = "-store.csv";
+
+  /**
+   * Path in the local filesystem to save the data.
+   */
+  protected String destPath;
+
+  /**
+   * Instantiate.
+   * @param name application name.
+   */
+  public DumpS3GuardTable(final String name) {
+    super(name);
+  }
+
+  /**
+   * Instantiate with default name.
+   */
+  public DumpS3GuardTable() {
+    this(NAME);
+  }
+
+  /**
+   * Bind to a specific FS + store.
+   * @param fs filesystem
+   * @param store metastore to use
+   * @param destFile the base filename for output
+   * @param uri URI of store -only needed if FS is null.
+   */
+  public DumpS3GuardTable(
+      final S3AFileSystem fs,
+      final DynamoDBMetadataStore store,
+      final File destFile,
+      final URI uri) {
+    super(NAME, fs, store, uri);
+    this.destPath = destFile.getAbsolutePath();
+  }
+
+  /**
+   * Bind to the argument list, including validating the CLI
+   * @throws Exception failure.
+   */
+  @Override
+  protected void serviceStart() throws Exception {
+    if (getStore() == null) {
+      List<String> arg = getArgumentList(2, 2, USAGE_MESSAGE);
+      bindFromCLI(arg.get(0));
+      destPath = arg.get(1);
+    }
+  }
+
+  /**
+   * Dump the filesystem and the metastore.
+   * @return the exit code.
+   * @throws ServiceLaunchException on failure.
+   * @throws IOException IO failure.
+   */
+  @Override
+  public int execute() throws ServiceLaunchException, IOException {
+
+    try {
+      final File scanFile = new File(
+          destPath + SCAN_CSV).getCanonicalFile();
+      scanFile.getParentFile().mkdirs();
+
+      try (CsvFile csv = new CsvFile(scanFile);
+           DurationInfo ignored = new DurationInfo(LOG,
+               "scanFile dump to %s", scanFile)) {
+        scanMetastore(csv);
+      }
+
+      if (getFilesystem() != null) {
+
+        Path basePath = getFilesystem().qualify(new Path(getUri()));
+
+        final File destFile = new File(destPath + STORE_CSV)
+            .getCanonicalFile();
+        LOG.info("Writing Store details to {}", destFile);
+        try (CsvFile csv = new CsvFile(destFile);
+             DurationInfo ignored = new DurationInfo(LOG, "List metastore")) {
+
+          LOG.info("Base path: {}", basePath);
+          dumpMetastore(csv, basePath);
+        }
+
+        // these operations all update the metastore as they list,
+        // that is: they are side-effecting.
+        final File treewalkFile = new File(destPath + TREE_CSV)
+            .getCanonicalFile();
+
+        try (CsvFile csv = new CsvFile(treewalkFile);
+             DurationInfo ignored = new DurationInfo(LOG,
+                 "Treewalk to %s", treewalkFile)) {
+          treewalkFilesystem(csv, basePath);
+        }
+        final File flatlistFile = new File(
+            destPath + FLAT_CSV).getCanonicalFile();
+
+        try (CsvFile csv = new CsvFile(flatlistFile);
+             DurationInfo ignored = new DurationInfo(LOG,
+                 "Flat list to %s", flatlistFile)) {
+          listStatusFilesystem(csv, basePath);
+        }
+        final File rawFile = new File(
+            destPath + RAW_CSV).getCanonicalFile();
+
+        try (CsvFile csv = new CsvFile(rawFile);
+             DurationInfo ignored = new DurationInfo(LOG,
+                 "Raw dump to %s", rawFile)) {
+          dumpRawS3ObjectStore(csv);
+        }
+        final File scanFile2 = new File(
+            destPath + SCAN2_CSV).getCanonicalFile();
+
+        try (CsvFile csv = new CsvFile(scanFile);
+             DurationInfo ignored = new DurationInfo(LOG,
+                 "scanFile dump to %s", scanFile2)) {
+          scanMetastore(csv);
+        }
+
+      }
+
+      return LauncherExitCodes.EXIT_SUCCESS;
+    } catch (IOException | RuntimeException e) {
+      LOG.error("failure", e);
+      throw e;
+    }
+  }
+
+  /**
+   * Dump the filesystem via a recursive treewalk.
 
 Review comment:
   I prefer the queue, but this could a be a good solution as well.

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