From c03024a5305bea1a40c87a4abc0793802bea5c75 Mon Sep 17 00:00:00 2001 From: Vinayakumar B Date: Thu, 6 Dec 2018 18:20:28 +0530 Subject: [PATCH] HDFS-14113. EC : Add Configuration to restrict UserDefined Policies. Contributed by Ayush Saxena. --- .../org/apache/hadoop/hdfs/DFSConfigKeys.java | 4 + .../namenode/ErasureCodingPolicyManager.java | 13 +++ .../src/main/resources/hdfs-default.xml | 8 ++ .../hdfs/TestErasureCodingAddConfig.java | 79 +++++++++++++++++++ 4 files changed, 104 insertions(+) create mode 100644 hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestErasureCodingAddConfig.java diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/DFSConfigKeys.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/DFSConfigKeys.java index 5899c9297b6..f6ce0d53692 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/DFSConfigKeys.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/DFSConfigKeys.java @@ -698,6 +698,10 @@ public class DFSConfigKeys extends CommonConfigurationKeys { public static final String DFS_NAMENODE_EC_POLICIES_MAX_CELLSIZE_KEY = "dfs.namenode.ec.policies.max.cellsize"; public static final int DFS_NAMENODE_EC_POLICIES_MAX_CELLSIZE_DEFAULT = 4 * 1024 * 1024; + public static final String DFS_NAMENODE_EC_POLICIES_USERPOLICIES_ALLOWED_KEY = + "dfs.namenode.ec.userdefined.policy.allowed"; + public static final boolean + DFS_NAMENODE_EC_POLICIES_USERPOLICIES_ALLOWED_KEY_DEFAULT = true; public static final String DFS_NAMENODE_EC_SYSTEM_DEFAULT_POLICY = "dfs.namenode.ec.system.default.policy"; public static final String DFS_NAMENODE_EC_SYSTEM_DEFAULT_POLICY_DEFAULT = diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/ErasureCodingPolicyManager.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/ErasureCodingPolicyManager.java index fc673f73999..ebb60d746b6 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/ErasureCodingPolicyManager.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/ErasureCodingPolicyManager.java @@ -58,6 +58,9 @@ public final class ErasureCodingPolicyManager { private int maxCellSize = DFSConfigKeys.DFS_NAMENODE_EC_POLICIES_MAX_CELLSIZE_DEFAULT; + private boolean userDefinedAllowed = + DFSConfigKeys.DFS_NAMENODE_EC_POLICIES_USERPOLICIES_ALLOWED_KEY_DEFAULT; + // Supported storage policies for striped EC files private static final byte[] SUITABLE_STORAGE_POLICIES_FOR_EC_STRIPED_MODE = new byte[]{ @@ -143,6 +146,11 @@ public final class ErasureCodingPolicyManager { maxCellSize = conf.getInt( DFSConfigKeys.DFS_NAMENODE_EC_POLICIES_MAX_CELLSIZE_KEY, DFSConfigKeys.DFS_NAMENODE_EC_POLICIES_MAX_CELLSIZE_DEFAULT); + + userDefinedAllowed = conf.getBoolean( + DFSConfigKeys.DFS_NAMENODE_EC_POLICIES_USERPOLICIES_ALLOWED_KEY, + DFSConfigKeys. + DFS_NAMENODE_EC_POLICIES_USERPOLICIES_ALLOWED_KEY_DEFAULT); } /** @@ -270,6 +278,11 @@ public final class ErasureCodingPolicyManager { */ public synchronized ErasureCodingPolicy addPolicy( ErasureCodingPolicy policy) { + if (!userDefinedAllowed) { + throw new HadoopIllegalArgumentException( + "Addition of user defined erasure coding policy is disabled."); + } + if (!CodecUtil.hasCodec(policy.getCodecName())) { throw new HadoopIllegalArgumentException("Codec name " + policy.getCodecName() + " is not supported"); diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/resources/hdfs-default.xml b/hadoop-hdfs-project/hadoop-hdfs/src/main/resources/hdfs-default.xml index 4e6c4db03d7..2c054396f9d 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/resources/hdfs-default.xml +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/resources/hdfs-default.xml @@ -3212,6 +3212,14 @@ + + dfs.namenode.ec.userdefined.policy.allowed + true + If set to false, doesn't allow addition of user defined + erasure coding policies. + + + dfs.datanode.ec.reconstruction.stripedread.timeout.millis 5000 diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestErasureCodingAddConfig.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestErasureCodingAddConfig.java new file mode 100644 index 00000000000..24c88bd6291 --- /dev/null +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestErasureCodingAddConfig.java @@ -0,0 +1,79 @@ +/** + * 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.hdfs; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hdfs.protocol.AddErasureCodingPolicyResponse; +import org.apache.hadoop.hdfs.protocol.ErasureCodingPolicy; +import org.apache.hadoop.io.erasurecode.ECSchema; +import org.junit.Test; + +/** + * Test that ensures addition of user defined EC policies is allowed only when + * dfs.namenode.ec.userdefined.policy.allowed is set to true. + */ +public class TestErasureCodingAddConfig { + + @Test + public void testECAddPolicyConfigDisable() throws IOException { + Configuration conf = new HdfsConfiguration(); + conf.setBoolean( + DFSConfigKeys.DFS_NAMENODE_EC_POLICIES_USERPOLICIES_ALLOWED_KEY, + false); + try (MiniDFSCluster cluster = + new MiniDFSCluster.Builder(conf).numDataNodes(0).build()) { + cluster.waitActive(); + DistributedFileSystem fs = cluster.getFileSystem(); + + ErasureCodingPolicy newPolicy1 = + new ErasureCodingPolicy(new ECSchema("rs", 5, 3), 1024 * 1024); + + AddErasureCodingPolicyResponse[] response = + fs.addErasureCodingPolicies(new ErasureCodingPolicy[] {newPolicy1}); + + assertFalse(response[0].isSucceed()); + assertEquals( + "Addition of user defined erasure coding policy is disabled.", + response[0].getErrorMsg()); + } + } + + @Test + public void testECAddPolicyConfigEnable() throws IOException { + Configuration conf = new HdfsConfiguration(); + conf.setBoolean( + DFSConfigKeys.DFS_NAMENODE_EC_POLICIES_USERPOLICIES_ALLOWED_KEY, true); + try (MiniDFSCluster cluster = + new MiniDFSCluster.Builder(conf).numDataNodes(0).build()) { + DistributedFileSystem fs = cluster.getFileSystem(); + ErasureCodingPolicy newPolicy1 = + new ErasureCodingPolicy(new ECSchema("rs", 5, 3), 1024 * 1024); + AddErasureCodingPolicyResponse[] response = + fs.addErasureCodingPolicies(new ErasureCodingPolicy[] {newPolicy1}); + assertTrue(response[0].isSucceed()); + assertNull(response[0].getErrorMsg()); + } + } +} \ No newline at end of file