You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ozone.apache.org by di...@apache.org on 2020/06/23 00:27:15 UTC

[hadoop-ozone] branch master updated: HDDS-3828. Configuration parsing of ozone insight should be based on fields (#1105)

This is an automated email from the ASF dual-hosted git repository.

dineshc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hadoop-ozone.git


The following commit(s) were added to refs/heads/master by this push:
     new 7ff5b5b  HDDS-3828. Configuration parsing of ozone insight should be based on fields (#1105)
7ff5b5b is described below

commit 7ff5b5bada3470c42be36443bb4e018bde90e1d9
Author: Elek, Márton <el...@users.noreply.github.com>
AuthorDate: Tue Jun 23 02:27:08 2020 +0200

    HDDS-3828. Configuration parsing of ozone insight should be based on fields (#1105)
---
 .../ozone/insight/ConfigurationSubCommand.java     |  31 +++++--
 .../ozone/insight/TestConfigurationSubCommand.java | 100 +++++++++++++++++++++
 2 files changed, 122 insertions(+), 9 deletions(-)

diff --git a/hadoop-ozone/insight/src/main/java/org/apache/hadoop/ozone/insight/ConfigurationSubCommand.java b/hadoop-ozone/insight/src/main/java/org/apache/hadoop/ozone/insight/ConfigurationSubCommand.java
index 0da8c20..dd565e5 100644
--- a/hadoop-ozone/insight/src/main/java/org/apache/hadoop/ozone/insight/ConfigurationSubCommand.java
+++ b/hadoop-ozone/insight/src/main/java/org/apache/hadoop/ozone/insight/ConfigurationSubCommand.java
@@ -17,6 +17,9 @@
  */
 package org.apache.hadoop.ozone.insight;
 
+import java.lang.reflect.Field;
+import java.util.concurrent.Callable;
+
 import org.apache.hadoop.hdds.cli.HddsVersionProvider;
 import org.apache.hadoop.hdds.conf.Config;
 import org.apache.hadoop.hdds.conf.ConfigGroup;
@@ -25,9 +28,6 @@ import org.apache.hadoop.ozone.insight.Component.Type;
 
 import picocli.CommandLine;
 
-import java.lang.reflect.Method;
-import java.util.concurrent.Callable;
-
 /**
  * Subcommand to show configuration values/documentation.
  */
@@ -60,20 +60,31 @@ public class ConfigurationSubCommand extends BaseInsightSubCommand
     return null;
   }
 
-  private void showConfig(Class clazz, Type type) {
+  protected void showConfig(Class clazz, Type type) {
     OzoneConfiguration conf = new OzoneConfiguration();
     conf.addResource(getHost(conf, new Component(type)) + "/conf");
+    printConfig(clazz, conf);
+  }
+
+  /**
+   * Print all the configuration annotated on the class or any superclass.
+   */
+  protected void printConfig(Class clazz, OzoneConfiguration conf) {
     ConfigGroup configGroup =
         (ConfigGroup) clazz.getAnnotation(ConfigGroup.class);
     if (configGroup == null) {
       return;
     }
+    printConfig(configGroup, clazz, conf);
+  }
 
+  private void printConfig(ConfigGroup configGroup, Class clazz,
+      OzoneConfiguration conf) {
     String prefix = configGroup.prefix();
 
-    for (Method method : clazz.getMethods()) {
-      if (method.isAnnotationPresent(Config.class)) {
-        Config config = method.getAnnotation(Config.class);
+    for (Field field : clazz.getDeclaredFields()) {
+      if (field.isAnnotationPresent(Config.class)) {
+        Config config = field.getAnnotation(Config.class);
         String key = prefix + "." + config.key();
         System.out.println(">>> " + key);
         System.out.println("       default: " + config.defaultValue());
@@ -82,10 +93,12 @@ public class ConfigurationSubCommand extends BaseInsightSubCommand
         System.out.println(config.description());
         System.out.println();
         System.out.println();
-
+      }
+      final Class superclass = clazz.getSuperclass();
+      if (superclass != Object.class) {
+        printConfig(configGroup, superclass, conf);
       }
     }
-
   }
 
 }
diff --git a/hadoop-ozone/insight/src/test/java/org/apache/hadoop/ozone/insight/TestConfigurationSubCommand.java b/hadoop-ozone/insight/src/test/java/org/apache/hadoop/ozone/insight/TestConfigurationSubCommand.java
new file mode 100644
index 0000000..8a201fa
--- /dev/null
+++ b/hadoop-ozone/insight/src/test/java/org/apache/hadoop/ozone/insight/TestConfigurationSubCommand.java
@@ -0,0 +1,100 @@
+/*
+ * 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.ozone.insight;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+
+import org.apache.hadoop.hdds.conf.Config;
+import org.apache.hadoop.hdds.conf.ConfigGroup;
+import org.apache.hadoop.hdds.conf.ConfigTag;
+import org.apache.hadoop.hdds.conf.OzoneConfiguration;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Test insight report which prints out configs.
+ */
+public class TestConfigurationSubCommand {
+
+  private static final PrintStream OLD_OUT = System.out;
+
+  private final ByteArrayOutputStream out = new ByteArrayOutputStream();
+
+  @Before
+  public void setup() throws Exception {
+    System.setOut(new PrintStream(out));
+  }
+
+  @After
+  public void reset() {
+    System.setOut(OLD_OUT);
+  }
+
+  @Test
+  public void testPrintConfig() {
+    OzoneConfiguration conf = new OzoneConfiguration();
+    conf.set("ozone.scm.client.address", "omclient");
+    ConfigurationSubCommand subCommand = new ConfigurationSubCommand();
+
+    subCommand.printConfig(CustomConfig.class, conf);
+
+    final String output = out.toString();
+    Assert.assertTrue(output.contains(">>> ozone.scm.client.address"));
+    Assert.assertTrue(output.contains("default: localhost"));
+    Assert.assertTrue(output.contains("current: omclient"));
+    Assert.assertTrue(output.contains(">>> ozone.scm.client.secure"));
+    Assert.assertTrue(output.contains("default: true"));
+    Assert.assertTrue(output.contains("current: true"));
+  }
+
+  /**
+   * Example configuration parent.
+   */
+  public static class ParentConfig {
+    @Config(key = "secure", defaultValue = "true", description = "Make "
+        + "everything secure.", tags = ConfigTag.MANAGEMENT)
+    private boolean secure = true;
+
+    public boolean isSecure() {
+      return secure;
+    }
+  }
+
+  /**
+   * Example configuration.
+   */
+  @ConfigGroup(prefix = "ozone.scm.client")
+  public static class CustomConfig extends ParentConfig {
+
+    @Config(key = "address", defaultValue = "localhost", description = "Client "
+        + "addres (To test string injection).", tags = ConfigTag.MANAGEMENT)
+    private String clientAddress;
+
+    public String getClientAddress() {
+      return clientAddress;
+    }
+
+    public void setClientAddress(String clientAddress) {
+      this.clientAddress = clientAddress;
+    }
+  }
+}
\ No newline at end of file


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