You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ratis.apache.org by sz...@apache.org on 2019/09/19 18:41:42 UTC

[incubator-ratis] branch master updated: RATIS-681. Fix non-standard field names in config keys.

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

szetszwo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-ratis.git


The following commit(s) were added to refs/heads/master by this push:
     new 1b2fcd9  RATIS-681. Fix non-standard field names in config keys.
1b2fcd9 is described below

commit 1b2fcd9ce2447cffbe657ddf444dd943f9a085b8
Author: Tsz Wo Nicholas Sze <sz...@apache.org>
AuthorDate: Thu Sep 19 11:41:19 2019 -0700

    RATIS-681. Fix non-standard field names in config keys.
---
 .../main/java/org/apache/ratis/conf/ConfUtils.java | 50 ++++++++++++++------
 .../java/org/apache/ratis/grpc/GrpcConfigKeys.java | 26 +++++------
 .../apache/ratis/server/RaftServerConfigKeys.java  | 19 ++++----
 .../ratis/server/impl/StateMachineUpdater.java     |  2 +-
 .../java/org/apache/ratis/conf/TestConfUtils.java  | 53 ++++++++++++++++++++++
 5 files changed, 112 insertions(+), 38 deletions(-)

diff --git a/ratis-common/src/main/java/org/apache/ratis/conf/ConfUtils.java b/ratis-common/src/main/java/org/apache/ratis/conf/ConfUtils.java
index cbc3b5c..3e996f0 100644
--- a/ratis-common/src/main/java/org/apache/ratis/conf/ConfUtils.java
+++ b/ratis-common/src/main/java/org/apache/ratis/conf/ConfUtils.java
@@ -248,6 +248,9 @@ public interface ConfUtils {
   }
 
   static void printAll(Class<?> confClass, Consumer<Object> out) {
+    if (confClass.isEnum()) {
+      return;
+    }
     out.accept("");
     out.accept("******* " + confClass + " *******");
     Arrays.asList(confClass.getDeclaredFields())
@@ -257,14 +260,14 @@ public interface ConfUtils {
   }
 
   static void printField(Class<?> confClass, Consumer<Object> out, Field f) {
-    if (!Modifier.isStatic(f.getModifiers())) {
-      out.accept("WARNING: Found non-static field " + f);
-      return;
+    final int modifiers = f.getModifiers();
+    if (!Modifier.isStatic(modifiers)) {
+      throw new IllegalStateException("Found non-static field " + f);
+    }
+    if (!Modifier.isFinal(modifiers)) {
+      throw new IllegalStateException("Found non-final field " + f);
     }
-    if (printKey(confClass, out, f, "KEY", "DEFAULT",
-        (b, defaultField) ->
-            b.append(defaultField.getGenericType().getTypeName()).append(", ")
-             .append("default=").append(defaultField.get(null)))) {
+    if (printKey(confClass, out, f, "KEY", "DEFAULT", ConfUtils::append)) {
       return;
     }
     if (printKey(confClass, out, f, "PARAMETER", "CLASS",
@@ -272,13 +275,35 @@ public interface ConfUtils {
       return;
     }
     final String fieldName = f.getName();
+    if ("LOG".equals(fieldName)) {
+      return;
+    }
+    if (!"PREFIX".equals(fieldName)) {
+      throw new IllegalStateException("Unexpected field: " + fieldName);
+    }
     try {
       out.accept("constant: " + fieldName + " = " + f.get(null));
     } catch (IllegalAccessException e) {
-      out.accept("WARNING: Failed to access " + f);
+      throw new IllegalStateException("Failed to access " + f, e);
     }
   }
 
+  static void append(StringBuilder b, Field defaultField) throws IllegalAccessException {
+    b.append(defaultField.getGenericType().getTypeName());
+
+    final Class<?> type = defaultField.getType();
+    if (type.isEnum()) {
+      b.append(" enum[");
+      for(Object e : defaultField.getType().getEnumConstants()) {
+        b.append(e).append(", ");
+      }
+      b.setLength(b.length() - 2);
+      b.append("]");
+    }
+
+    b.append(", ").append("default=").append(defaultField.get(null));
+  }
+
   static boolean printKey(
       Class<?> confClass, Consumer<Object> out, Field f, String KEY, String DEFAULT,
       CheckedBiConsumer<StringBuilder, Field, IllegalAccessException> processDefault) {
@@ -294,8 +319,7 @@ public interface ConfUtils {
       final Object keyName = f.get(null);
       b.append(KEY.toLowerCase()).append(": ").append(keyName);
     } catch (IllegalAccessException e) {
-      out.accept("WARNING: Failed to access " + fieldName);
-      b.append(fieldName + " is not public");
+      throw new IllegalStateException("Failed to access " + fieldName, e);
     }
     final int len = fieldName.length() - KEY.length();
     final String defaultFieldName = fieldName.substring(0, len) + DEFAULT;
@@ -304,11 +328,9 @@ public interface ConfUtils {
       final Field defaultField = confClass.getDeclaredField(defaultFieldName);
       processDefault.accept(b, defaultField);
     } catch (NoSuchFieldException e) {
-      out.accept("WARNING: " + DEFAULT + " not found for field " + f);
-      b.append(DEFAULT).append(" not found");
+      throw new IllegalStateException(DEFAULT + " not found for field " + f, e);
     } catch (IllegalAccessException e) {
-      out.accept("WARNING: Failed to access " + defaultFieldName);
-      b.append(defaultFieldName).append(" is not public");
+      throw new IllegalStateException("Failed to access " + defaultFieldName, e);
     }
     b.append(")");
     out.accept(b);
diff --git a/ratis-grpc/src/main/java/org/apache/ratis/grpc/GrpcConfigKeys.java b/ratis-grpc/src/main/java/org/apache/ratis/grpc/GrpcConfigKeys.java
index 1f18810..8f7c261 100644
--- a/ratis-grpc/src/main/java/org/apache/ratis/grpc/GrpcConfigKeys.java
+++ b/ratis-grpc/src/main/java/org/apache/ratis/grpc/GrpcConfigKeys.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -30,48 +30,48 @@ import java.util.function.Consumer;
 import static org.apache.ratis.conf.ConfUtils.*;
 
 public interface GrpcConfigKeys {
+  Logger LOG = LoggerFactory.getLogger(GrpcConfigKeys.class);
+  static Consumer<String> getDefaultLog() {
+    return LOG::info;
+  }
+
   String PREFIX = "raft.grpc";
 
   interface TLS {
-    Logger LOG = LoggerFactory.getLogger(TLS.class);
-    static Consumer<String> getDefaultLog() {
-      return LOG::info;
-    }
-
-    String TLS_ROOT_PREFIX = GrpcConfigKeys.PREFIX + ".tls";
+    String PREFIX = GrpcConfigKeys.PREFIX + ".tls";
 
-    String TLS_ENABLED_KEY = TLS_ROOT_PREFIX + ".enabled";
+    String TLS_ENABLED_KEY = PREFIX + ".enabled";
     boolean TLS_ENABLED_DEFAULT = false;
     static boolean tlsEnabled(RaftProperties properties) {
       return getBoolean(properties::getBoolean, TLS_ENABLED_KEY, TLS_ENABLED_DEFAULT, getDefaultLog());
     }
 
-    String MUTUAL_AUTHN_ENABLED_KEY = TLS_ROOT_PREFIX + ".mutual_authn.enabled";
+    String MUTUAL_AUTHN_ENABLED_KEY = PREFIX + ".mutual_authn.enabled";
     boolean MUTUAL_AUTHN_ENABLED_DEFAULT = false;
     static boolean mutualAuthnEnabled(RaftProperties properties) {
       return getBoolean(properties::getBoolean,
           MUTUAL_AUTHN_ENABLED_KEY, MUTUAL_AUTHN_ENABLED_DEFAULT, getDefaultLog());
     }
 
-    String PRIVATE_KEY_FILE_KEY = TLS_ROOT_PREFIX + ".private.key.file.name";
+    String PRIVATE_KEY_FILE_KEY = PREFIX + ".private.key.file.name";
     String PRIVATE_KEY_FILE_DEFAULT = "private.pem";
     static String getPrivateKeyFile(RaftProperties properties) {
       return get(properties::get, PRIVATE_KEY_FILE_KEY, PRIVATE_KEY_FILE_DEFAULT, getDefaultLog());
     }
 
-    String CERT_CHAIN_FILE_KEY = TLS_ROOT_PREFIX + ".cert.chain.file.name";
+    String CERT_CHAIN_FILE_KEY = PREFIX + ".cert.chain.file.name";
     String CERT_CHAIN_FILE_DEFAULT = "certificate.crt";
     static String getCertChainFile(RaftProperties properties) {
       return get(properties::get, CERT_CHAIN_FILE_KEY, CERT_CHAIN_FILE_DEFAULT, getDefaultLog());
     }
 
-    String TRUST_STORE_KEY = TLS_ROOT_PREFIX + ".trust.store";
+    String TRUST_STORE_KEY = PREFIX + ".trust.store";
     String TRUST_STORE_DEFAULT = "ca.crt";
     static String getTrustStore(RaftProperties properties) {
       return get(properties::get, TRUST_STORE_KEY, TRUST_STORE_DEFAULT, getDefaultLog());
     }
 
-    String CONF_PARAMETER = TLS_ROOT_PREFIX + ".conf";
+    String CONF_PARAMETER = PREFIX + ".conf";
     Class<GrpcTlsConfig> CONF_CLASS = GrpcTlsConfig.class;
     static GrpcTlsConfig getConf(Parameters parameters) {
       return parameters != null ? parameters.get(CONF_PARAMETER, CONF_CLASS): null;
diff --git a/ratis-server/src/main/java/org/apache/ratis/server/RaftServerConfigKeys.java b/ratis-server/src/main/java/org/apache/ratis/server/RaftServerConfigKeys.java
index eefadde..405d684 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/RaftServerConfigKeys.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/RaftServerConfigKeys.java
@@ -48,14 +48,14 @@ public interface RaftServerConfigKeys {
     setFiles(properties::setFiles, STORAGE_DIR_KEY, storageDir);
   }
 
-  String SLEEP_DEVIATION_THRESHOLD = PREFIX + ".sleep.deviation.threshold";
+  String SLEEP_DEVIATION_THRESHOLD_KEY = PREFIX + ".sleep.deviation.threshold";
   int SLEEP_DEVIATION_THRESHOLD_DEFAULT = 300;
   static int sleepDeviationThreshold(RaftProperties properties) {
-    return getInt(properties::getInt, SLEEP_DEVIATION_THRESHOLD,
+    return getInt(properties::getInt, SLEEP_DEVIATION_THRESHOLD_KEY,
         SLEEP_DEVIATION_THRESHOLD_DEFAULT, getDefaultLog());
   }
   static void setSleepDeviationThreshold(RaftProperties properties, int thresholdMs) {
-    setInt(properties::setInt, SLEEP_DEVIATION_THRESHOLD, thresholdMs);
+    setInt(properties::setInt, SLEEP_DEVIATION_THRESHOLD_KEY, thresholdMs);
   }
 
   /**
@@ -336,14 +336,13 @@ public interface RaftServerConfigKeys {
       setLong(properties::setLong, AUTO_TRIGGER_THRESHOLD_KEY, autoTriggerThreshold);
     }
 
-    String RETENTION_POLICY_KEY = PREFIX + ".retention.num.files";
-    int DEFAULT_ALL_SNAPSHOTS_RETAINED = -1;
-    static int snapshotRetentionPolicy(RaftProperties raftProperties) {
-      return getInt(raftProperties::getInt,
-          RETENTION_POLICY_KEY, DEFAULT_ALL_SNAPSHOTS_RETAINED, getDefaultLog());
+    String RETENTION_FILE_NUM_KEY = PREFIX + ".retention.file.num";
+    int RETENTION_FILE_NUM_DEFAULT = -1;
+    static int retentionFileNum(RaftProperties raftProperties) {
+      return getInt(raftProperties::getInt, RETENTION_FILE_NUM_KEY, RETENTION_FILE_NUM_DEFAULT, getDefaultLog());
     }
-    static void setSnapshotRetentionPolicy(RaftProperties properties, int numSnapshotFilesRetained) {
-      setInt(properties::setInt, RETENTION_POLICY_KEY, numSnapshotFilesRetained);
+    static void setRetentionFileNum(RaftProperties properties, int numSnapshotFilesRetained) {
+      setInt(properties::setInt, RETENTION_FILE_NUM_KEY, numSnapshotFilesRetained);
     }
   }
 
diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/StateMachineUpdater.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/StateMachineUpdater.java
index 39eb472..2e9dbca 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/impl/StateMachineUpdater.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/StateMachineUpdater.java
@@ -93,7 +93,7 @@ class StateMachineUpdater implements Runnable {
 
     final boolean autoSnapshot = RaftServerConfigKeys.Snapshot.autoTriggerEnabled(properties);
     this.autoSnapshotThreshold = autoSnapshot? RaftServerConfigKeys.Snapshot.autoTriggerThreshold(properties): null;
-    int numSnapshotFilesRetained = RaftServerConfigKeys.Snapshot.snapshotRetentionPolicy(properties);
+    final int numSnapshotFilesRetained = RaftServerConfigKeys.Snapshot.retentionFileNum(properties);
     this.snapshotRetentionPolicy = new SnapshotRetentionPolicy() {
       @Override
       public int getNumSnapshotsRetained() {
diff --git a/ratis-test/src/test/java/org/apache/ratis/conf/TestConfUtils.java b/ratis-test/src/test/java/org/apache/ratis/conf/TestConfUtils.java
new file mode 100644
index 0000000..1600da1
--- /dev/null
+++ b/ratis-test/src/test/java/org/apache/ratis/conf/TestConfUtils.java
@@ -0,0 +1,53 @@
+/*
+ * 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.ratis.conf;
+
+import org.apache.ratis.BaseTest;
+import org.apache.ratis.RaftConfigKeys;
+import org.apache.ratis.client.RaftClientConfigKeys;
+import org.apache.ratis.grpc.GrpcConfigKeys;
+import org.apache.ratis.netty.NettyConfigKeys;
+import org.apache.ratis.server.RaftServerConfigKeys;
+import org.junit.Test;
+
+public class TestConfUtils  extends BaseTest {
+  @Test
+  public void testRaftConfigKeys() {
+    ConfUtils.printAll(RaftConfigKeys.class);
+  }
+
+  @Test
+  public void testRaftServerConfigKeys() {
+    ConfUtils.printAll(RaftServerConfigKeys.class);
+  }
+
+  @Test
+  public void testRaftClientConfigKeys() {
+    ConfUtils.printAll(RaftClientConfigKeys.class);
+  }
+
+  @Test
+  public void testGrpcConfigKeys() {
+    ConfUtils.printAll(GrpcConfigKeys.class);
+  }
+
+  @Test
+  public void testNettyConfigKeys() {
+    ConfUtils.printAll(NettyConfigKeys.class);
+  }
+}