[120] | 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 | |
---|
| 19 | package org.apache.hadoop.mapred; |
---|
| 20 | |
---|
| 21 | import java.io.*; |
---|
| 22 | import java.net.*; |
---|
| 23 | import junit.framework.TestCase; |
---|
| 24 | import org.apache.hadoop.conf.Configuration; |
---|
| 25 | import org.apache.hadoop.hdfs.MiniDFSCluster; |
---|
| 26 | import org.apache.hadoop.fs.FileSystem; |
---|
| 27 | import org.apache.hadoop.fs.Path; |
---|
| 28 | import org.apache.hadoop.io.IntWritable; |
---|
| 29 | import org.apache.hadoop.io.Text; |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | |
---|
| 33 | /** |
---|
| 34 | * A JUnit test to test that killing completed jobs does not move them |
---|
| 35 | * to the failed sate - See JIRA HADOOP-2132 |
---|
| 36 | */ |
---|
| 37 | public class TestKillCompletedJob extends TestCase { |
---|
| 38 | |
---|
| 39 | |
---|
| 40 | static Boolean launchWordCount(String fileSys, |
---|
| 41 | String jobTracker, |
---|
| 42 | JobConf conf, |
---|
| 43 | String input, |
---|
| 44 | int numMaps, |
---|
| 45 | int numReduces) throws IOException { |
---|
| 46 | final Path inDir = new Path("/testing/wc/input"); |
---|
| 47 | final Path outDir = new Path("/testing/wc/output"); |
---|
| 48 | FileSystem fs = FileSystem.get(URI.create(fileSys), conf); |
---|
| 49 | fs.delete(outDir, true); |
---|
| 50 | if (!fs.mkdirs(inDir)) { |
---|
| 51 | throw new IOException("Mkdirs failed to create " + inDir.toString()); |
---|
| 52 | } |
---|
| 53 | { |
---|
| 54 | DataOutputStream file = fs.create(new Path(inDir, "part-0")); |
---|
| 55 | file.writeBytes(input); |
---|
| 56 | file.close(); |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | FileSystem.setDefaultUri(conf, fileSys); |
---|
| 60 | conf.set("mapred.job.tracker", jobTracker); |
---|
| 61 | conf.setJobName("wordcount"); |
---|
| 62 | conf.setInputFormat(TextInputFormat.class); |
---|
| 63 | |
---|
| 64 | // the keys are words (strings) |
---|
| 65 | conf.setOutputKeyClass(Text.class); |
---|
| 66 | // the values are counts (ints) |
---|
| 67 | conf.setOutputValueClass(IntWritable.class); |
---|
| 68 | |
---|
| 69 | conf.setMapperClass(WordCount.MapClass.class); |
---|
| 70 | conf.setCombinerClass(WordCount.Reduce.class); |
---|
| 71 | conf.setReducerClass(WordCount.Reduce.class); |
---|
| 72 | |
---|
| 73 | FileInputFormat.setInputPaths(conf, inDir); |
---|
| 74 | FileOutputFormat.setOutputPath(conf, outDir); |
---|
| 75 | conf.setNumMapTasks(numMaps); |
---|
| 76 | conf.setNumReduceTasks(numReduces); |
---|
| 77 | |
---|
| 78 | RunningJob rj = JobClient.runJob(conf); |
---|
| 79 | JobID jobId = rj.getID(); |
---|
| 80 | |
---|
| 81 | // Kill the job after it is successful |
---|
| 82 | if (rj.isSuccessful()) |
---|
| 83 | { |
---|
| 84 | System.out.println("Job Id:" + jobId + |
---|
| 85 | " completed successfully. Killing it now"); |
---|
| 86 | rj.killJob(); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | |
---|
| 90 | return rj.isSuccessful(); |
---|
| 91 | |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | |
---|
| 95 | public void testKillCompJob() throws IOException { |
---|
| 96 | String namenode = null; |
---|
| 97 | MiniDFSCluster dfs = null; |
---|
| 98 | MiniMRCluster mr = null; |
---|
| 99 | FileSystem fileSys = null; |
---|
| 100 | try { |
---|
| 101 | final int taskTrackers = 1; |
---|
| 102 | |
---|
| 103 | Configuration conf = new Configuration(); |
---|
| 104 | dfs = new MiniDFSCluster(conf, 1, true, null); |
---|
| 105 | fileSys = dfs.getFileSystem(); |
---|
| 106 | namenode = fileSys.getUri().toString(); |
---|
| 107 | mr = new MiniMRCluster(taskTrackers, namenode, 3); |
---|
| 108 | JobConf jobConf = new JobConf(); |
---|
| 109 | |
---|
| 110 | Boolean result; |
---|
| 111 | final String jobTrackerName = "localhost:" + mr.getJobTrackerPort(); |
---|
| 112 | result = launchWordCount(namenode, jobTrackerName, jobConf, |
---|
| 113 | "Small text\n", |
---|
| 114 | 1, 0); |
---|
| 115 | assertTrue(result); |
---|
| 116 | |
---|
| 117 | } finally { |
---|
| 118 | if (dfs != null) { dfs.shutdown(); } |
---|
| 119 | if (mr != null) { mr.shutdown(); |
---|
| 120 | } |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | } |
---|