HDFS-12402. Refactor ErasureCodingPolicyManager and related codes. Contributed by Sammi Chen

This commit is contained in:
Kai Zheng 2017-09-07 20:38:23 +08:00
parent 6f101e7df1
commit 2adf8bed71
6 changed files with 27 additions and 60 deletions

View File

@ -17,6 +17,8 @@
*/
package org.apache.hadoop.hdfs.protocol;
import org.apache.hadoop.HadoopIllegalArgumentException;
/**
* A response of add an ErasureCoding policy.
*/
@ -38,7 +40,7 @@ public class AddECPolicyResponse {
}
public AddECPolicyResponse(ErasureCodingPolicy policy,
IllegalECPolicyException e) {
HadoopIllegalArgumentException e) {
this(policy, e.getMessage());
}

View File

@ -1,34 +0,0 @@
/**
* 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.protocol;
import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability;
/**
* An Exception indicates the error when adding an ErasureCoding policy.
*/
@InterfaceAudience.Private
@InterfaceStability.Evolving
public class IllegalECPolicyException extends Exception {
static final long serialVersionUID = 1L;
public IllegalECPolicyException(String msg) {
super(msg);
}
}

View File

@ -19,11 +19,11 @@ package org.apache.hadoop.hdfs.server.namenode;
import com.google.common.annotations.VisibleForTesting;
import org.apache.commons.lang.ArrayUtils;
import org.apache.hadoop.HadoopIllegalArgumentException;
import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hdfs.DFSConfigKeys;
import org.apache.hadoop.hdfs.protocol.ErasureCodingPolicyState;
import org.apache.hadoop.hdfs.protocol.IllegalECPolicyException;
import org.apache.hadoop.hdfs.protocol.SystemErasureCodingPolicies;
import org.apache.hadoop.hdfs.protocol.ErasureCodingPolicy;
import org.apache.hadoop.hdfs.protocol.HdfsConstants;
@ -144,7 +144,7 @@ public final class ErasureCodingPolicyManager {
policyName,
DFSConfigKeys.DFS_NAMENODE_EC_POLICIES_ENABLED_KEY,
names);
throw new IllegalArgumentException(msg);
throw new HadoopIllegalArgumentException(msg);
}
enabledPoliciesByName.put(ecPolicy.getName(), ecPolicy);
}
@ -230,33 +230,34 @@ public final class ErasureCodingPolicyManager {
* Add an erasure coding policy.
* @return the added policy
*/
public synchronized ErasureCodingPolicy addPolicy(ErasureCodingPolicy policy)
throws IllegalECPolicyException {
public synchronized ErasureCodingPolicy addPolicy(
ErasureCodingPolicy policy) {
// Set policy state into DISABLED when adding into Hadoop.
policy.setState(ErasureCodingPolicyState.DISABLED);
if (!CodecUtil.hasCodec(policy.getCodecName())) {
throw new IllegalECPolicyException("Codec name "
throw new HadoopIllegalArgumentException("Codec name "
+ policy.getCodecName() + " is not supported");
}
if (policy.getCellSize() > maxCellSize) {
throw new IllegalECPolicyException("Cell size " + policy.getCellSize()
+ " should not exceed maximum " + maxCellSize + " byte");
throw new HadoopIllegalArgumentException("Cell size " +
policy.getCellSize() + " should not exceed maximum " +
maxCellSize + " bytes");
}
String assignedNewName = ErasureCodingPolicy.composePolicyName(
policy.getSchema(), policy.getCellSize());
for (ErasureCodingPolicy p : getPolicies()) {
if (p.getName().equals(assignedNewName)) {
throw new IllegalECPolicyException("The policy name " + assignedNewName
+ " already exists");
throw new HadoopIllegalArgumentException("The policy name " +
assignedNewName + " already exists");
}
if (p.getSchema().equals(policy.getSchema()) &&
p.getCellSize() == policy.getCellSize()) {
throw new IllegalECPolicyException("A policy with same schema "
throw new HadoopIllegalArgumentException("A policy with same schema "
+ policy.getSchema().toString() + " and cell size "
+ p.getCellSize() + " is already exists");
+ p.getCellSize() + " already exists");
}
}
policy.setName(assignedNewName);
@ -281,12 +282,12 @@ public final class ErasureCodingPolicyManager {
public synchronized void removePolicy(String name) {
ErasureCodingPolicy ecPolicy = policiesByName.get(name);
if (ecPolicy == null) {
throw new IllegalArgumentException("The policy name " +
name + " does not exists");
throw new HadoopIllegalArgumentException("The policy name " +
name + " does not exist");
}
if (ecPolicy.isSystemPolicy()) {
throw new IllegalArgumentException("System erasure coding policy " +
throw new HadoopIllegalArgumentException("System erasure coding policy " +
name + " cannot be removed");
}
@ -317,8 +318,8 @@ public final class ErasureCodingPolicyManager {
public synchronized void disablePolicy(String name) {
ErasureCodingPolicy ecPolicy = policiesByName.get(name);
if (ecPolicy == null) {
throw new IllegalArgumentException("The policy name " +
name + " does not exists");
throw new HadoopIllegalArgumentException("The policy name " +
name + " does not exist");
}
if (enabledPoliciesByName.containsKey(name)) {
@ -336,8 +337,8 @@ public final class ErasureCodingPolicyManager {
public synchronized void enablePolicy(String name) {
ErasureCodingPolicy ecPolicy = policiesByName.get(name);
if (ecPolicy == null) {
throw new IllegalArgumentException("The policy name " +
name + " does not exists");
throw new HadoopIllegalArgumentException("The policy name " +
name + " does not exist");
}
enabledPoliciesByName.put(name, ecPolicy);
@ -346,4 +347,4 @@ public final class ErasureCodingPolicyManager {
enabledPoliciesByName.values().toArray(new ErasureCodingPolicy[0]);
LOG.info("Enable the erasure coding policy " + name);
}
}
}

View File

@ -27,7 +27,6 @@ import org.apache.hadoop.fs.permission.FsAction;
import org.apache.hadoop.hdfs.DFSConfigKeys;
import org.apache.hadoop.hdfs.XAttrHelper;
import org.apache.hadoop.hdfs.protocol.ErasureCodingPolicy;
import org.apache.hadoop.hdfs.protocol.IllegalECPolicyException;
import org.apache.hadoop.hdfs.server.namenode.FSDirectory.DirOp;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.WritableUtils;
@ -212,7 +211,7 @@ final class FSDirErasureCodingOp {
}
static ErasureCodingPolicy addErasureCodePolicy(final FSNamesystem fsn,
ErasureCodingPolicy policy) throws IllegalECPolicyException {
ErasureCodingPolicy policy) {
Preconditions.checkNotNull(policy);
return fsn.getErasureCodingPolicyManager().addPolicy(policy);
}

View File

@ -201,7 +201,6 @@ import org.apache.hadoop.hdfs.protocol.HdfsConstants.DatanodeReportType;
import org.apache.hadoop.hdfs.protocol.HdfsConstants.ReencryptAction;
import org.apache.hadoop.hdfs.protocol.HdfsConstants.SafeModeAction;
import org.apache.hadoop.hdfs.protocol.HdfsFileStatus;
import org.apache.hadoop.hdfs.protocol.IllegalECPolicyException;
import org.apache.hadoop.hdfs.protocol.LastBlockWithStatus;
import org.apache.hadoop.hdfs.protocol.LocatedBlock;
import org.apache.hadoop.hdfs.protocol.LocatedBlocks;
@ -7207,7 +7206,7 @@ public class FSNamesystem implements Namesystem, FSNamesystemMBean,
FSDirErasureCodingOp.addErasureCodePolicy(this, policy);
addECPolicyName = newPolicy.getName();
responses.add(new AddECPolicyResponse(newPolicy));
} catch (IllegalECPolicyException e) {
} catch (HadoopIllegalArgumentException e) {
responses.add(new AddECPolicyResponse(policy, e));
}
}

View File

@ -1617,7 +1617,7 @@ public class TestDistributedFileSystem {
fs.enableErasureCodingPolicy("notExistECName");
Assert.fail("enable the policy that doesn't exist should fail");
} catch (Exception e) {
GenericTestUtils.assertExceptionContains("does not exists", e);
GenericTestUtils.assertExceptionContains("does not exist", e);
// pass
}
@ -1626,7 +1626,7 @@ public class TestDistributedFileSystem {
fs.disableErasureCodingPolicy("notExistECName");
Assert.fail("disable the policy that doesn't exist should fail");
} catch (Exception e) {
GenericTestUtils.assertExceptionContains("does not exists", e);
GenericTestUtils.assertExceptionContains("does not exist", e);
// pass
}