You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ozone.apache.org by el...@apache.org on 2021/04/06 16:41:18 UTC

[ozone] branch master updated: HDDS-5035. Use default config values to solve generated config file conflict (#2087)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 843d39e  HDDS-5035. Use default config values to solve generated config file conflict (#2087)
843d39e is described below

commit 843d39ee3c2b1e0dc602874c14dfd9cf225e0119
Author: Symious <yi...@foxmail.com>
AuthorDate: Wed Apr 7 00:41:02 2021 +0800

    HDDS-5035. Use default config values to solve generated config file conflict (#2087)
---
 .../conf/TestGeneratedConfigurationOverwrite.java  | 66 ++++++++++++++++++++++
 .../hdds/conf/ConfigurationReflectionUtil.java     | 25 ++++----
 2 files changed, 81 insertions(+), 10 deletions(-)

diff --git a/hadoop-hdds/common/src/test/java/org/apache/hadoop/hdds/conf/TestGeneratedConfigurationOverwrite.java b/hadoop-hdds/common/src/test/java/org/apache/hadoop/hdds/conf/TestGeneratedConfigurationOverwrite.java
new file mode 100644
index 0000000..f00107b
--- /dev/null
+++ b/hadoop-hdds/common/src/test/java/org/apache/hadoop/hdds/conf/TestGeneratedConfigurationOverwrite.java
@@ -0,0 +1,66 @@
+/*
+ * 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.hdds.conf;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+/**
+ * In HDDS-5035, we met the case that ozone-default-generated.xml got
+ * overwritten in an assemble jar, here we are trying to simulate this case
+ * by rename the generated config file.
+ */
+public class TestGeneratedConfigurationOverwrite {
+
+  private final Path generatedConfigurationPath =
+      Paths.get("target/test-classes/ozone-default-generated.xml");
+  private final Path generatedConfigurationPathBak =
+      Paths.get("target/test-classes/ozone-default-generated.xml.bak");
+
+  private OzoneConfiguration conf;
+
+  @Before
+  public void overwriteConfigFile() throws Exception {
+    Files.move(generatedConfigurationPath, generatedConfigurationPathBak);
+    conf = new OzoneConfiguration();
+  }
+
+  @After
+  public void recoverConfigFile() throws Exception {
+    Files.move(generatedConfigurationPathBak, generatedConfigurationPath);
+  }
+
+  @Test
+  public void getConfigurationObject() {
+    // Check Config Type of String
+    Assert.assertNotNull(
+        conf.getObject(SimpleConfiguration.class).getBindHost());
+    // Check Config Type of Int
+    Assert.assertNotEquals(
+        conf.getObject(SimpleConfiguration.class).getPort(), 0);
+    // Check Config Type of Time
+    Assert.assertNotEquals(
+        conf.getObject(SimpleConfiguration.class).getWaitTime(), 0);
+  }
+}
\ No newline at end of file
diff --git a/hadoop-hdds/config/src/main/java/org/apache/hadoop/hdds/conf/ConfigurationReflectionUtil.java b/hadoop-hdds/config/src/main/java/org/apache/hadoop/hdds/conf/ConfigurationReflectionUtil.java
index 229390e..5b6d430 100644
--- a/hadoop-hdds/config/src/main/java/org/apache/hadoop/hdds/conf/ConfigurationReflectionUtil.java
+++ b/hadoop-hdds/config/src/main/java/org/apache/hadoop/hdds/conf/ConfigurationReflectionUtil.java
@@ -68,35 +68,40 @@ public final class ConfigurationReflectionUtil {
 
         String key = prefix + "." + configAnnotation.key();
 
+        String defaultValue = configAnnotation.defaultValue();
+
         ConfigType type = configAnnotation.type();
 
         if (type == ConfigType.AUTO) {
           type = detectConfigType(field.getType(), fieldLocation);
         }
 
-        //Note: default value is handled by ozone-default.xml. Here we can
-        //use any default.
         try {
           switch (type) {
           case STRING:
-            forcedFieldSet(field, configuration, from.get(key));
+            forcedFieldSet(field, configuration, from.get(key, defaultValue));
             break;
           case INT:
-            forcedFieldSet(field, configuration, from.getInt(key, 0));
+            forcedFieldSet(field, configuration,
+                from.getInt(key, Integer.parseInt(defaultValue)));
             break;
           case BOOLEAN:
-            forcedFieldSet(field, configuration, from.getBoolean(key, false));
+            forcedFieldSet(field, configuration,
+                from.getBoolean(key, Boolean.parseBoolean(defaultValue)));
             break;
           case LONG:
-            forcedFieldSet(field, configuration, from.getLong(key, 0));
+            forcedFieldSet(field, configuration,
+                from.getLong(key, Long.parseLong(defaultValue)));
             break;
           case TIME:
             forcedFieldSet(field, configuration,
-                from.getTimeDuration(key, "0s", configAnnotation.timeUnit()));
+                from.getTimeDuration(key, defaultValue,
+                    configAnnotation.timeUnit()));
             break;
           case SIZE:
             final long value =
-                Math.round(from.getStorageSize(key, "0b", StorageUnit.BYTES));
+                Math.round(from.getStorageSize(key,
+                    defaultValue, StorageUnit.BYTES));
             if (field.getType() == int.class) {
               forcedFieldSet(field, configuration, (int) value);
             } else {
@@ -106,13 +111,13 @@ public final class ConfigurationReflectionUtil {
             break;
           case CLASS:
             forcedFieldSet(field, configuration,
-                from.getClass(key, Object.class));
+                from.getClass(key, Class.forName(defaultValue)));
             break;
           default:
             throw new ConfigurationException(
                 "Unsupported ConfigType " + type + " on " + fieldLocation);
           }
-        } catch (IllegalAccessException e) {
+        } catch (IllegalAccessException | ClassNotFoundException e) {
           throw new ConfigurationException(
               "Can't inject configuration to " + fieldLocation, e);
         }

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