Skip to content

Commit 677259d

Browse files
committed
HADOOP-10355. Fix TestLoadGenerator#testLoadGenerator. Contributed by Haohui Mai.
git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1570460 13f79535-47bb-0310-9956-ffa450edef68
1 parent c510da9 commit 677259d

File tree

2 files changed

+44
-40
lines changed

2 files changed

+44
-40
lines changed

hadoop-common-project/hadoop-common/CHANGES.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,8 @@ Release 2.4.0 - UNRELEASED
396396
HADOOP-10328. loadGenerator exit code is not reliable.
397397
(Haohui Mai via cnauroth)
398398

399+
HADOOP-10355. Fix TestLoadGenerator#testLoadGenerator. (Haohui Mai via jing9)
400+
399401
Release 2.3.1 - UNRELEASED
400402

401403
INCOMPATIBLE CHANGES

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/loadGenerator/LoadGenerator.java

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
import org.apache.hadoop.util.Tool;
4646
import org.apache.hadoop.util.ToolRunner;
4747

48+
import com.google.common.base.Preconditions;
49+
4850
/** The load generator is a tool for testing NameNode behavior under
4951
* different client loads.
5052
* It allows the user to generate different mixes of read, write,
@@ -488,7 +490,35 @@ private int init(String[] args) throws IOException {
488490

489491
return initFileDirTables();
490492
}
491-
493+
494+
private static void parseScriptLine(String line, ArrayList<Long> duration,
495+
ArrayList<Double> readProb, ArrayList<Double> writeProb) {
496+
String[] a = line.split("\\s");
497+
498+
if (a.length != 3) {
499+
throw new IllegalArgumentException("Incorrect number of parameters: "
500+
+ line);
501+
}
502+
503+
try {
504+
long d = Long.parseLong(a[0]);
505+
double r = Double.parseDouble(a[1]);
506+
double w = Double.parseDouble(a[2]);
507+
508+
Preconditions.checkArgument(d >= 0, "Invalid duration: " + d);
509+
Preconditions.checkArgument(0 <= r && r <= 1.0,
510+
"The read probability must be [0, 1]: " + r);
511+
Preconditions.checkArgument(0 <= w && w <= 1.0,
512+
"The read probability must be [0, 1]: " + w);
513+
514+
readProb.add(r);
515+
duration.add(d);
516+
writeProb.add(w);
517+
} catch (NumberFormatException nfe) {
518+
throw new IllegalArgumentException("Cannot parse: " + line);
519+
}
520+
}
521+
492522
/**
493523
* Read a script file of the form: lines of text with duration in seconds,
494524
* read probability and write probability, separated by white space.
@@ -508,47 +538,19 @@ private int loadScriptFile(String filename) throws IOException {
508538
String line;
509539
// Read script, parse values, build array of duration, read and write probs
510540

511-
while ((line = br.readLine()) != null) {
512-
lineNum++;
513-
if (line.startsWith("#") || line.isEmpty()) // skip comments and blanks
514-
continue;
515-
516-
String[] a = line.split("\\s");
517-
if (a.length != 3) {
518-
System.err.println("Line " + lineNum
519-
+ ": Incorrect number of parameters: " + line);
520-
}
521-
522-
try {
523-
long d = Long.parseLong(a[0]);
524-
if (d < 0) {
525-
System.err.println("Line " + lineNum + ": Invalid duration: " + d);
526-
return -1;
527-
}
528-
529-
double r = Double.parseDouble(a[1]);
530-
if (r < 0.0 || r > 1.0) {
531-
System.err.println("Line " + lineNum
532-
+ ": The read probability must be [0, 1]: " + r);
533-
return -1;
534-
}
535-
536-
double w = Double.parseDouble(a[2]);
537-
if (w < 0.0 || w > 1.0) {
538-
System.err.println("Line " + lineNum
539-
+ ": The read probability must be [0, 1]: " + r);
540-
return -1;
541-
}
541+
try {
542+
while ((line = br.readLine()) != null) {
543+
lineNum++;
544+
if (line.startsWith("#") || line.isEmpty()) // skip comments and blanks
545+
continue;
542546

543-
readProb.add(r);
544-
duration.add(d);
545-
writeProb.add(w);
546-
} catch (NumberFormatException nfe) {
547-
System.err.println(lineNum + ": Can't parse: " + line);
548-
return -1;
549-
} finally {
550-
IOUtils.cleanup(LOG, br);
547+
parseScriptLine(line, duration, readProb, writeProb);
551548
}
549+
} catch (IllegalArgumentException e) {
550+
System.err.println("Line: " + lineNum + ", " + e.getMessage());
551+
return -1;
552+
} finally {
553+
IOUtils.cleanup(LOG, br);
552554
}
553555

554556
// Copy vectors to arrays of values, to avoid autoboxing overhead later

0 commit comments

Comments
 (0)