source: proiecte/HadoopJUnit/hadoop-0.20.1/src/test/org/apache/hadoop/mapred/TestTaskLimits.java @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 3.8 KB
Line 
1/**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19package org.apache.hadoop.mapred;
20
21import junit.framework.TestCase;
22import java.io.IOException;
23
24import org.apache.commons.logging.Log;
25import org.apache.commons.logging.LogFactory;
26import org.apache.hadoop.conf.Configuration;
27import org.apache.hadoop.hdfs.MiniDFSCluster;
28import org.apache.hadoop.examples.PiEstimator;
29import org.apache.hadoop.fs.FileSystem;
30
31import org.apache.commons.logging.impl.Log4JLogger;
32import org.apache.log4j.Level;
33
34/**
35 * A JUnit test to test configired task limits.
36 */
37public class TestTaskLimits extends TestCase {
38
39  {     
40    ((Log4JLogger)JobInProgress.LOG).getLogger().setLevel(Level.ALL);
41  }     
42
43  private static final Log LOG =
44    LogFactory.getLog(TestMiniMRWithDFS.class.getName());
45 
46  static final int NUM_MAPS = 5;
47  static final int NUM_SAMPLES = 100;
48 
49  public static class TestResult {
50    public String output;
51    public RunningJob job;
52    TestResult(RunningJob job, String output) {
53      this.job = job;
54      this.output = output;
55    }
56  }
57 
58  static void runPI(MiniMRCluster mr, JobConf jobconf) throws IOException {
59    LOG.info("runPI");
60    double estimate = PiEstimator.estimate(NUM_MAPS, NUM_SAMPLES, jobconf).doubleValue();
61    double error = Math.abs(Math.PI - estimate);
62    System.out.println("PI estimation " + error);
63  }
64
65  /**
66   * Run the pi test with a specifix value of
67   * mapred.jobtracker.maxtasks.per.job. Returns true if the job succeeded.
68   */
69  private boolean runOneTest(int maxTasks) throws IOException {
70    MiniDFSCluster dfs = null;
71    MiniMRCluster mr = null;
72    FileSystem fileSys = null;
73    boolean success = false;
74    try {
75      final int taskTrackers = 2;
76
77      Configuration conf = new Configuration();
78      conf.setInt("mapred.jobtracker.maxtasks.per.job", maxTasks);
79      dfs = new MiniDFSCluster(conf, 4, true, null);
80      fileSys = dfs.getFileSystem();
81      JobConf jconf = new JobConf(conf);
82      mr = new MiniMRCluster(0, 0, taskTrackers, fileSys.getUri().toString(), 1,
83                             null, null, null, jconf);
84     
85      JobConf jc = mr.createJobConf();
86      try {
87        runPI(mr, jc);
88        success = true;
89      } catch (IOException e) {
90        success = false;
91      }
92    } finally {
93      if (dfs != null) { dfs.shutdown(); }
94      if (mr != null) { mr.shutdown(); }
95    }
96    return success;
97  }
98
99  public void testTaskLimits() throws IOException {
100
101    System.out.println("Job 1 running with max set to 2");
102    boolean status = runOneTest(2);
103    assertTrue(status == false);
104    System.out.println("Job 1 failed as expected.");
105
106    // verify that checking this limit works well. The job
107    // needs 5 mappers and we set the limit to 7.
108    System.out.println("Job 2 running with max set to 7.");
109    status = runOneTest(7);
110    assertTrue(status == true);
111    System.out.println("Job 2 succeeded as expected.");
112
113    System.out.println("Job 3 running with max disabled.");
114    status = runOneTest(-1);
115    assertTrue(status == true);
116    System.out.println("Job 3 succeeded as expected.");
117  }
118}
Note: See TracBrowser for help on using the repository browser.