HDFS-12448. Make sure user defined erasure coding policy ID will not overflow. Contributed by Huafeng Wang

This commit is contained in:
Kai Zheng 2017-10-20 09:42:04 +08:00
parent 0f1c037618
commit ce7cf66e5e
4 changed files with 44 additions and 6 deletions

View File

@ -50,6 +50,7 @@ private ErasureCodeConstants() {
public static final ECSchema REPLICATION_1_2_SCHEMA = new ECSchema(
REPLICATION_CODEC_NAME, 1, 2);
public static final byte MAX_POLICY_ID = Byte.MAX_VALUE;
public static final byte USER_DEFINED_POLICY_START_ID = (byte) 64;
public static final byte REPLICATION_POLICY_ID = (byte) 63;
public static final String REPLICATION_POLICY_NAME = REPLICATION_CODEC_NAME;

View File

@ -253,6 +253,14 @@ public synchronized ErasureCodingPolicy addPolicy(
return p;
}
}
if (getCurrentMaxPolicyID() == ErasureCodeConstants.MAX_POLICY_ID) {
throw new HadoopIllegalArgumentException("Adding erasure coding " +
"policy failed because the number of policies stored in the " +
"system already reached the threshold, which is " +
ErasureCodeConstants.MAX_POLICY_ID);
}
policy.setName(assignedNewName);
policy.setId(getNextAvailablePolicyID());
this.policiesByName.put(policy.getName(), policy);
@ -261,12 +269,14 @@ public synchronized ErasureCodingPolicy addPolicy(
return policy;
}
private byte getCurrentMaxPolicyID() {
return policiesByID.keySet().stream().max(Byte::compareTo).orElse((byte)0);
}
private byte getNextAvailablePolicyID() {
byte currentId = this.policiesByID.keySet().stream()
.max(Byte::compareTo)
.filter(id -> id >= ErasureCodeConstants.USER_DEFINED_POLICY_START_ID)
.orElse(ErasureCodeConstants.USER_DEFINED_POLICY_START_ID);
return (byte) (currentId + 1);
byte nextPolicyID = (byte)(getCurrentMaxPolicyID() + 1);
return nextPolicyID > ErasureCodeConstants.USER_DEFINED_POLICY_START_ID ?
nextPolicyID : ErasureCodeConstants.USER_DEFINED_POLICY_START_ID;
}
/**

View File

@ -193,7 +193,7 @@ Below are the details about each command.
* `[-addPolicies -policyFile <file>]`
Add a list of erasure coding policies. Please refer etc/hadoop/user_ec_policies.xml.template for the example policy file. The maximum cell size is defined in property 'dfs.namenode.ec.policies.max.cellsize' with the default value 4MB.
Add a list of erasure coding policies. Please refer etc/hadoop/user_ec_policies.xml.template for the example policy file. The maximum cell size is defined in property 'dfs.namenode.ec.policies.max.cellsize' with the default value 4MB. Currently HDFS allows the user to add 64 policies in total, and the added policy ID is in range of 64 to 127. Adding policy will fail if there are already 64 policies added.
* `[-listCodecs]`

View File

@ -751,6 +751,33 @@ public Object run() throws Exception {
});
}
@Test
public void testAddECPoliciesExceeded() throws Exception {
ECSchema toAddSchema = new ECSchema("rs", 3, 2);
int allowNumPolicies = ErasureCodeConstants.MAX_POLICY_ID -
ErasureCodeConstants.USER_DEFINED_POLICY_START_ID + 1;
for (int i = 0; i < allowNumPolicies; i++) {
ErasureCodingPolicy erasureCodingPolicy = new ErasureCodingPolicy(
toAddSchema, 1024 + 1024 * i);
ErasureCodingPolicy[] policyArray =
new ErasureCodingPolicy[]{erasureCodingPolicy};
AddErasureCodingPolicyResponse[] responses =
fs.addErasureCodingPolicies(policyArray);
assertEquals(1, responses.length);
assertTrue(responses[0].isSucceed());
assertEquals(responses[0].getPolicy().getId(),
ErasureCodeConstants.USER_DEFINED_POLICY_START_ID + i);
}
ErasureCodingPolicy erasureCodingPolicy = new ErasureCodingPolicy(
toAddSchema, 1024 + 1024 * allowNumPolicies);
ErasureCodingPolicy[] policyArray =
new ErasureCodingPolicy[]{erasureCodingPolicy};
AddErasureCodingPolicyResponse[] responses =
fs.addErasureCodingPolicies(policyArray);
assertEquals(1, responses.length);
assertFalse(responses[0].isSucceed());
}
@Test
public void testReplicationPolicy() throws Exception {
ErasureCodingPolicy replicaPolicy =