HDFS-328. Improve fs -setrep error message for invalid replication factors. Contributed by Daniel Templeton.

This commit is contained in:
Andrew Wang 2015-09-02 13:45:20 -07:00
parent c006c3a1e6
commit afc88b396f
4 changed files with 141 additions and 17 deletions

View File

@ -877,6 +877,9 @@ Release 2.8.0 - UNRELEASED
HDFS-2070. Add more unit tests for FsShell getmerge (Daniel Templeton via
Colin P. McCabe)
HDFS-328. Improve fs -setrep error message for invalid replication factors.
(Daniel Templeton via wang)
OPTIMIZATIONS
HDFS-8026. Trace FSOutputSummer#writeChecksumChunks rather than

View File

@ -953,28 +953,39 @@ public short adjustReplication(short replication) {
/**
* Check whether the replication parameter is within the range
* determined by system configuration.
* determined by system configuration and throw an exception if it's not.
*
* @param src the path to the target file
* @param replication the requested replication factor
* @param clientName the name of the client node making the request
* @throws java.io.IOException thrown if the requested replication factor
* is out of bounds
*/
public void verifyReplication(String src,
short replication,
String clientName) throws IOException {
if (replication >= minReplication && replication <= maxReplication) {
//common case. avoid building 'text'
return;
if (replication < minReplication || replication > maxReplication) {
StringBuilder msg = new StringBuilder("Requested replication factor of ");
msg.append(replication);
if (replication > maxReplication) {
msg.append(" exceeds maximum of ");
msg.append(maxReplication);
} else {
msg.append(" is less than the required minimum of ");
msg.append(minReplication);
}
String text = "file " + src
+ ((clientName != null) ? " on client " + clientName : "")
+ ".\n"
+ "Requested replication " + replication;
msg.append(" for ").append(src);
if (replication > maxReplication)
throw new IOException(text + " exceeds maximum " + maxReplication);
if (clientName != null) {
msg.append(" from ").append(clientName);
}
if (replication < minReplication)
throw new IOException(text + " is less than the required minimum " +
minReplication);
throw new IOException(msg.toString());
}
}
/**

View File

@ -2412,7 +2412,63 @@ public void testCopyFromLocalWithPermissionDenied() throws Exception {
}
}
/**
* Test -setrep with a replication factor that is too low. We have to test
* this here because the mini-cluster used with testHDFSConf.xml uses a
* replication factor of 1 (for good reason).
*/
@Test (timeout = 30000)
public void testSetrepLow() throws Exception {
Configuration conf = new Configuration();
conf.setInt(DFSConfigKeys.DFS_NAMENODE_REPLICATION_MIN_KEY, 2);
MiniDFSCluster.Builder builder = new MiniDFSCluster.Builder(conf);
MiniDFSCluster cluster = builder.numDataNodes(2).format(true).build();
FsShell shell = new FsShell(conf);
cluster.waitActive();
final String testdir = "/tmp/TestDFSShell-testSetrepLow";
final Path hdfsFile = new Path(testdir, "testFileForSetrepLow");
final PrintStream origOut = System.out;
final PrintStream origErr = System.err;
try {
final FileSystem fs = cluster.getFileSystem();
assertTrue("Unable to create test directory",
fs.mkdirs(new Path(testdir)));
fs.create(hdfsFile, true).close();
// Capture the command output so we can examine it
final ByteArrayOutputStream bao = new ByteArrayOutputStream();
final PrintStream capture = new PrintStream(bao);
System.setOut(capture);
System.setErr(capture);
final String[] argv = new String[] { "-setrep", "1", hdfsFile.toString() };
try {
assertEquals("Command did not return the expected exit code",
1, shell.run(argv));
} finally {
System.setOut(origOut);
System.setErr(origErr);
}
assertEquals("Error message is not the expected error message",
"setrep: Requested replication factor of 1 is less than "
+ "the required minimum of 2 for /tmp/TestDFSShell-"
+ "testSetrepLow/testFileForSetrepLow\n",
bao.toString());
} finally {
shell.close();
cluster.shutdown();
}
}
// setrep for file and directory.
@Test (timeout = 30000)

View File

@ -6472,6 +6472,60 @@
</comparators>
</test>
<test> <!-- TESTED -->
<description>setrep: invalid replication factor -- too high</description>
<test-commands>
<command>-fs NAMENODE -mkdir -p /dir0</command>
<command>-fs NAMENODE -touchz /dir0/file0</command>
<command>-fs NAMENODE -setrep 1025 /dir0/file0</command>
</test-commands>
<cleanup-commands>
<command>-fs NAMENODE -rm -r /user</command>
</cleanup-commands>
<comparators>
<comparator>
<type>RegexpComparator</type>
<expected-output>^setrep: Requested replication factor of 1025 exceeds maximum of [0-9]+ for /dir0/file0</expected-output>
</comparator>
</comparators>
</test>
<test> <!-- TESTED -->
<description>setrep: invalid replication factor -- 0</description>
<test-commands>
<command>-fs NAMENODE -mkdir -p dir0</command>
<command>-fs NAMENODE -touchz dir0/file0</command>
<command>-fs NAMENODE -setrep 0 dir0/file0</command>
</test-commands>
<cleanup-commands>
<command>-fs NAMENODE -rm -r /user</command>
</cleanup-commands>
<comparators>
<comparator>
<type>RegexpComparator</type>
<expected-output>^-setrep: replication must be >= 1</expected-output>
</comparator>
</comparators>
</test>
<test> <!-- TESTED -->
<description>setrep: invalid replication factor -- NaN</description>
<test-commands>
<command>-fs NAMENODE -mkdir -p dir0</command>
<command>-fs NAMENODE -touchz dir0/file0</command>
<command>-fs NAMENODE -setrep three dir0/file0</command>
</test-commands>
<cleanup-commands>
<command>-fs NAMENODE -rm -r /user</command>
</cleanup-commands>
<comparators>
<comparator>
<type>RegexpComparator</type>
<expected-output>^setrep: Illegal replication, a positive integer expected</expected-output>
</comparator>
</comparators>
</test>
<!-- Tests for touchz-->
<test> <!-- TESTED -->
<description>touchz: touching file (absolute path) </description>