You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@ozone.apache.org by GitBox <gi...@apache.org> on 2021/08/24 23:47:09 UTC

[GitHub] [ozone] avijayanhwx commented on a change in pull request #2565: HDDS-5368. Add CLI command: ozone admin namespace summary

avijayanhwx commented on a change in pull request #2565:
URL: https://github.com/apache/ozone/pull/2565#discussion_r695287031



##########
File path: hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/admin/nssummary/NSSummaryCLIUtils.java
##########
@@ -0,0 +1,174 @@
+/*
+ * 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.ozone.admin.nssummary;
+
+import com.google.gson.Gson;
+import org.apache.commons.io.IOUtils;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hdds.conf.ConfigurationSource;
+import org.apache.hadoop.hdfs.web.URLConnectionFactory;
+import picocli.CommandLine.Help.Ansi;
+
+import javax.security.sasl.AuthenticationException;
+import java.io.InputStream;
+import java.net.ConnectException;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import java.util.HashMap;
+
+import static java.net.HttpURLConnection.HTTP_CREATED;
+import static java.net.HttpURLConnection.HTTP_OK;
+
+/**
+ * Utility class to support Namespace CLI.
+ */
+public final class NSSummaryCLIUtils {
+
+  private NSSummaryCLIUtils() {
+
+  }
+
+  private static final String OFS_PREFIX = "ofs://";
+
+  public static String makeHttpCall(StringBuffer url, String path,
+                                    boolean isSpnegoEnabled,
+                                    ConfigurationSource conf)
+      throws Exception {
+    return makeHttpCall(url, path, false, false, isSpnegoEnabled, conf);
+  }
+
+  public static String makeHttpCall(StringBuffer url, String path,
+                                    boolean listFile, boolean withReplica,
+                                    boolean isSpnegoEnabled,
+                                    ConfigurationSource conf)
+      throws Exception {
+
+    url.append("?path=").append(path);
+
+    if (listFile) {
+      url.append("&files=true");
+    }
+    if (withReplica) {
+      url.append("&replica=true");
+    }
+
+    System.out.println("Connecting to Recon: " + url + "...");
+    final URLConnectionFactory connectionFactory =
+        URLConnectionFactory.newDefaultURLConnectionFactory(
+            (Configuration) conf);
+
+    HttpURLConnection httpURLConnection;
+
+    try {
+      httpURLConnection = (HttpURLConnection)
+          connectionFactory.openConnection(new URL(url.toString()),
+              isSpnegoEnabled);
+      httpURLConnection.connect();
+      int errorCode = httpURLConnection.getResponseCode();
+      InputStream inputStream = httpURLConnection.getInputStream();
+
+      if ((errorCode == HTTP_OK) || (errorCode == HTTP_CREATED)) {
+        return IOUtils.toString(inputStream, StandardCharsets.UTF_8);
+      }
+
+      if (httpURLConnection.getErrorStream() != null) {
+        System.out.println("Recon is being initialized. Please wait a moment");
+        return null;
+      } else {
+        System.out.println("Unexpected null in http payload," +
+            " while processing request");
+      }
+      return null;
+    } catch (ConnectException ex) {
+      System.err.println("Connection Refused. Please make sure the " +
+          "Recon Server has been started.");
+      return null;
+    } catch (AuthenticationException authEx) {
+      System.err.println("Authentication Failed. Please make sure you " +
+          "have login or disable Ozone security settings.");
+      return null;
+    }
+  }
+
+  public static HashMap<String, Object> getResponseMap(String response) {
+    return new Gson().fromJson(response, HashMap.class);
+  }
+
+  public static void printNewLines(int cnt) {
+    for (int i = 0; i < cnt; ++i) {
+      System.out.println();
+    }
+  }
+
+  public static void printSpaces(int cnt) {
+    for (int i = 0; i < cnt; ++i) {
+      System.out.print(" ");
+    }
+  }
+
+  public static void printEmptyPathRequest() {
+    System.err.println("The path parameter is empty.\n" +
+        "If you mean the root path, use / instead.");
+  }
+
+  public static void printPathNotFound() {
+    System.err.println("Path not found in the system.\n" +
+        "Did you remove any protocol prefix before the path?");
+  }
+
+  public static void printTypeNA(String requestType) {
+    String markUp = "@|underline " + requestType + "|@";
+    System.err.println("Path found in the system.\nBut the entity type " +
+        "is not applicable to the " + Ansi.AUTO.string(markUp) + " request");
+  }
+
+  public static void printKVSeparator() {
+    System.out.print(" : ");
+  }
+
+  public static void printWithUnderline(String str, boolean newLine) {
+    String markupStr = "@|underline " + str + "|@";
+    if (newLine) {
+      System.out.println(Ansi.AUTO.string(markupStr));
+    } else {
+      System.out.print(Ansi.AUTO.string(markupStr));
+    }
+  }
+
+  public static void printFSOReminder() {
+    printNewLines(1);
+    System.out.println("[Warning] FSO is NOT enabled. " +
+        "Namespace CLI is only designed for FSO mode.\n" +
+        "To enable FSO set ozone.om.enable.filesystem.paths to true " +
+        "and ozone.om.metadata.layout to PREFIX.");
+    printNewLines(1);
+  }
+
+  public static String parseInputPath(String path) {
+    if (!path.startsWith("ofs://")) {
+      return path;
+    }
+    int idx = path.indexOf("/", OFS_PREFIX.length());

Review comment:
       ofs:// path takes in OM host:port or service id as the first part after the ofs:// prefix. Hence, this code needs to handle that as well. 

##########
File path: hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/admin/nssummary/NSSummaryAdmin.java
##########
@@ -0,0 +1,99 @@
+/*
+ * 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.ozone.admin.nssummary;
+
+import org.apache.hadoop.hdds.HddsUtils;
+import org.apache.hadoop.hdds.cli.GenericCli;
+import org.apache.hadoop.hdds.cli.HddsVersionProvider;
+import org.apache.hadoop.hdds.cli.OzoneAdmin;
+import org.apache.hadoop.hdds.cli.SubcommandWithParent;
+import org.apache.hadoop.hdds.conf.ConfigurationSource;
+import org.apache.hadoop.hdds.conf.OzoneConfiguration;
+import org.apache.hadoop.ozone.OzoneSecurityUtil;
+import org.kohsuke.MetaInfServices;
+import picocli.CommandLine;
+
+import java.net.InetSocketAddress;
+
+/**
+ * Subcommand for admin operations related to OM.
+ */
+@CommandLine.Command(
+    name = "namespace",
+    description = "Namespace Summary specific admin operations",
+    mixinStandardHelpOptions = true,
+    versionProvider = HddsVersionProvider.class,
+    subcommands = {
+        SummarySubCommand.class,
+        DiskUsageSubCommand.class,
+        QuotaUsageSubCommand.class,
+        FileSizeDistSubCommand.class
+    })
+@MetaInfServices(SubcommandWithParent.class)
+public class NSSummaryAdmin extends GenericCli implements SubcommandWithParent {
+  @CommandLine.ParentCommand
+  private OzoneAdmin parent;
+
+  @CommandLine.Spec
+  private CommandLine.Model.CommandSpec spec;
+
+  public OzoneAdmin getParent() {
+    return parent;
+  }
+
+  @Override
+  public Void call() throws Exception {
+    GenericCli.missingSubcommand(spec);
+    return null;
+  }
+
+  @Override
+  public Class<?> getParentType() {
+    return OzoneAdmin.class;
+  }
+
+  public boolean isFSOEnabled() {
+    OzoneConfiguration conf = parent.getOzoneConf();
+    return conf.getBoolean("ozone.om.enable.filesystem.paths", false)
+        && conf.get("ozone.om.metadata.layout").equalsIgnoreCase("PREFIX");
+  }
+
+  public String getReconWebAddress() {
+    OzoneConfiguration conf = parent.getOzoneConf();
+    String protocolPrefix = "";
+    InetSocketAddress reconSocket = null;
+    if (OzoneSecurityUtil.isHttpSecurityEnabled(conf)) {

Review comment:
       Need to look at ozone.http.policy to determine http vs https here.

##########
File path: hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/HddsUtils.java
##########
@@ -314,6 +316,48 @@ public static InetSocketAddress getReconAddresses(
     return NetUtils.createSocketAddr(hostname.get(), port);
   }
 
+  /**
+   * Retrieve the socket addresses of recon HTTP.
+   *
+   * @return Recon address
+   * @throws IllegalArgumentException If the configuration is invalid
+   */
+  public static InetSocketAddress getReconHTTPAddresses(
+      ConfigurationSource conf) {
+    String name = conf.get(OZONE_RECON_HTTP_ADDRESS_KEY);
+    if (StringUtils.isEmpty(name)) {
+      return null;
+    }
+    Optional<String> hostname = getHostName(name);
+    if (!hostname.isPresent()) {
+      throw new IllegalArgumentException("Invalid hostname for Recon: "
+          + name);
+    }
+    int port = getHostPort(name).orElse(OZONE_RECON_DATANODE_PORT_DEFAULT);
+    return NetUtils.createSocketAddr(hostname.get(), port);
+  }
+
+  /**
+   * Retrieve the socket addresses of recon HTTPS.
+   *
+   * @return Recon address
+   * @throws IllegalArgumentException If the configuration is invalid
+   */
+  public static InetSocketAddress getReconHTTPSAddresses(

Review comment:
       Seems to have common logic with last method, maybe single method is good? 

##########
File path: hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/admin/nssummary/NSSummaryCLIUtils.java
##########
@@ -0,0 +1,174 @@
+/*
+ * 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.ozone.admin.nssummary;
+
+import com.google.gson.Gson;
+import org.apache.commons.io.IOUtils;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hdds.conf.ConfigurationSource;
+import org.apache.hadoop.hdfs.web.URLConnectionFactory;
+import picocli.CommandLine.Help.Ansi;
+
+import javax.security.sasl.AuthenticationException;
+import java.io.InputStream;
+import java.net.ConnectException;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import java.util.HashMap;
+
+import static java.net.HttpURLConnection.HTTP_CREATED;
+import static java.net.HttpURLConnection.HTTP_OK;
+
+/**
+ * Utility class to support Namespace CLI.
+ */
+public final class NSSummaryCLIUtils {
+
+  private NSSummaryCLIUtils() {
+
+  }
+
+  private static final String OFS_PREFIX = "ofs://";
+
+  public static String makeHttpCall(StringBuffer url, String path,
+                                    boolean isSpnegoEnabled,
+                                    ConfigurationSource conf)
+      throws Exception {
+    return makeHttpCall(url, path, false, false, isSpnegoEnabled, conf);
+  }
+
+  public static String makeHttpCall(StringBuffer url, String path,
+                                    boolean listFile, boolean withReplica,
+                                    boolean isSpnegoEnabled,
+                                    ConfigurationSource conf)
+      throws Exception {
+
+    url.append("?path=").append(path);
+
+    if (listFile) {
+      url.append("&files=true");
+    }
+    if (withReplica) {
+      url.append("&replica=true");
+    }
+
+    System.out.println("Connecting to Recon: " + url + "...");
+    final URLConnectionFactory connectionFactory =
+        URLConnectionFactory.newDefaultURLConnectionFactory(
+            (Configuration) conf);
+
+    HttpURLConnection httpURLConnection;
+
+    try {
+      httpURLConnection = (HttpURLConnection)
+          connectionFactory.openConnection(new URL(url.toString()),
+              isSpnegoEnabled);
+      httpURLConnection.connect();
+      int errorCode = httpURLConnection.getResponseCode();
+      InputStream inputStream = httpURLConnection.getInputStream();
+
+      if ((errorCode == HTTP_OK) || (errorCode == HTTP_CREATED)) {
+        return IOUtils.toString(inputStream, StandardCharsets.UTF_8);
+      }
+
+      if (httpURLConnection.getErrorStream() != null) {
+        System.out.println("Recon is being initialized. Please wait a moment");
+        return null;
+      } else {
+        System.out.println("Unexpected null in http payload," +
+            " while processing request");
+      }
+      return null;
+    } catch (ConnectException ex) {
+      System.err.println("Connection Refused. Please make sure the " +
+          "Recon Server has been started.");
+      return null;
+    } catch (AuthenticationException authEx) {
+      System.err.println("Authentication Failed. Please make sure you " +
+          "have login or disable Ozone security settings.");
+      return null;
+    }
+  }
+
+  public static HashMap<String, Object> getResponseMap(String response) {
+    return new Gson().fromJson(response, HashMap.class);
+  }
+
+  public static void printNewLines(int cnt) {
+    for (int i = 0; i < cnt; ++i) {
+      System.out.println();
+    }
+  }
+
+  public static void printSpaces(int cnt) {
+    for (int i = 0; i < cnt; ++i) {
+      System.out.print(" ");
+    }
+  }
+
+  public static void printEmptyPathRequest() {
+    System.err.println("The path parameter is empty.\n" +
+        "If you mean the root path, use / instead.");
+  }
+
+  public static void printPathNotFound() {
+    System.err.println("Path not found in the system.\n" +
+        "Did you remove any protocol prefix before the path?");
+  }
+
+  public static void printTypeNA(String requestType) {
+    String markUp = "@|underline " + requestType + "|@";
+    System.err.println("Path found in the system.\nBut the entity type " +
+        "is not applicable to the " + Ansi.AUTO.string(markUp) + " request");
+  }
+
+  public static void printKVSeparator() {
+    System.out.print(" : ");
+  }
+
+  public static void printWithUnderline(String str, boolean newLine) {
+    String markupStr = "@|underline " + str + "|@";
+    if (newLine) {
+      System.out.println(Ansi.AUTO.string(markupStr));
+    } else {
+      System.out.print(Ansi.AUTO.string(markupStr));
+    }
+  }
+
+  public static void printFSOReminder() {
+    printNewLines(1);
+    System.out.println("[Warning] FSO is NOT enabled. " +
+        "Namespace CLI is only designed for FSO mode.\n" +
+        "To enable FSO set ozone.om.enable.filesystem.paths to true " +
+        "and ozone.om.metadata.layout to PREFIX.");
+    printNewLines(1);
+  }
+
+  public static String parseInputPath(String path) {
+    if (!path.startsWith("ofs://")) {
+      return path;
+    }
+    int idx = path.indexOf("/", OFS_PREFIX.length());

Review comment:
       ofs:// path takes in service id or om host as the first part. This logic needs to strip out that 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.

To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org