[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 | package org.apache.hadoop.mapred; |
---|
| 19 | |
---|
| 20 | import java.io.*; |
---|
| 21 | import junit.framework.TestCase; |
---|
| 22 | |
---|
| 23 | import org.apache.commons.logging.Log; |
---|
| 24 | import org.apache.commons.logging.LogFactory; |
---|
| 25 | import org.apache.hadoop.io.IntWritable; |
---|
| 26 | import org.apache.hadoop.io.LongWritable; |
---|
| 27 | import org.apache.hadoop.io.Text; |
---|
| 28 | import org.apache.hadoop.fs.FileSystem; |
---|
| 29 | import org.apache.hadoop.fs.Path; |
---|
| 30 | import org.apache.hadoop.mapred.lib.IdentityReducer; |
---|
| 31 | import org.apache.hadoop.conf.Configuration; |
---|
| 32 | import org.apache.hadoop.hdfs.MiniDFSCluster; |
---|
| 33 | |
---|
| 34 | /** |
---|
| 35 | * Class to test mapred task's temp directory |
---|
| 36 | */ |
---|
| 37 | public class TestMiniMRTaskTempDir extends TestCase { |
---|
| 38 | private static final Log LOG = |
---|
| 39 | LogFactory.getLog(TestMiniMRTaskTempDir.class.getName()); |
---|
| 40 | |
---|
| 41 | private MiniMRCluster mr; |
---|
| 42 | private MiniDFSCluster dfs; |
---|
| 43 | private FileSystem fileSys; |
---|
| 44 | |
---|
| 45 | /** |
---|
| 46 | * Map class which checks whether temp directory exists |
---|
| 47 | * and check the value of java.io.tmpdir |
---|
| 48 | * Creates a tempfile and checks whether that is created in |
---|
| 49 | * temp directory specified. |
---|
| 50 | */ |
---|
| 51 | public static class MapClass extends MapReduceBase |
---|
| 52 | implements Mapper<LongWritable, Text, Text, IntWritable> { |
---|
| 53 | Path tmpDir; |
---|
| 54 | FileSystem localFs; |
---|
| 55 | public void map (LongWritable key, Text value, |
---|
| 56 | OutputCollector<Text, IntWritable> output, |
---|
| 57 | Reporter reporter) throws IOException { |
---|
| 58 | String tmp = null; |
---|
| 59 | if (localFs.exists(tmpDir)) { |
---|
| 60 | tmp = tmpDir.makeQualified(localFs).toString(); |
---|
| 61 | |
---|
| 62 | assertEquals(tmp, new Path(System.getProperty("java.io.tmpdir")). |
---|
| 63 | makeQualified(localFs).toString()); |
---|
| 64 | } else { |
---|
| 65 | fail("Temp directory "+tmpDir +" doesnt exist."); |
---|
| 66 | } |
---|
| 67 | File tmpFile = File.createTempFile("test", ".tmp"); |
---|
| 68 | assertEquals(tmp, new Path(tmpFile.getParent()). |
---|
| 69 | makeQualified(localFs).toString()); |
---|
| 70 | } |
---|
| 71 | public void configure(JobConf job) { |
---|
| 72 | tmpDir = new Path(job.get("mapred.child.tmp", "./tmp")); |
---|
| 73 | try { |
---|
| 74 | localFs = FileSystem.getLocal(job); |
---|
| 75 | } catch (IOException ioe) { |
---|
| 76 | ioe.printStackTrace(); |
---|
| 77 | fail("IOException in getting localFS"); |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | /** |
---|
| 83 | * Launch tests |
---|
| 84 | * @param conf Configuration of the mapreduce job. |
---|
| 85 | * @param inDir input path |
---|
| 86 | * @param outDir output path |
---|
| 87 | * @param input Input text |
---|
| 88 | * @throws IOException |
---|
| 89 | */ |
---|
| 90 | public void launchTest(JobConf conf, |
---|
| 91 | Path inDir, |
---|
| 92 | Path outDir, |
---|
| 93 | String input) |
---|
| 94 | throws IOException { |
---|
| 95 | |
---|
| 96 | // set up the input file system and write input text. |
---|
| 97 | FileSystem inFs = inDir.getFileSystem(conf); |
---|
| 98 | FileSystem outFs = outDir.getFileSystem(conf); |
---|
| 99 | outFs.delete(outDir, true); |
---|
| 100 | if (!inFs.mkdirs(inDir)) { |
---|
| 101 | throw new IOException("Mkdirs failed to create " + inDir.toString()); |
---|
| 102 | } |
---|
| 103 | { |
---|
| 104 | // write input into input file |
---|
| 105 | DataOutputStream file = inFs.create(new Path(inDir, "part-0")); |
---|
| 106 | file.writeBytes(input); |
---|
| 107 | file.close(); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | // configure the mapred Job which creates a tempfile in map. |
---|
| 111 | conf.setJobName("testmap"); |
---|
| 112 | conf.setMapperClass(MapClass.class); |
---|
| 113 | conf.setReducerClass(IdentityReducer.class); |
---|
| 114 | conf.setNumMapTasks(1); |
---|
| 115 | conf.setNumReduceTasks(0); |
---|
| 116 | FileInputFormat.setInputPaths(conf, inDir); |
---|
| 117 | FileOutputFormat.setOutputPath(conf, outDir); |
---|
| 118 | String TEST_ROOT_DIR = new Path(System.getProperty("test.build.data", |
---|
| 119 | "/tmp")).toString().replace(' ', '+'); |
---|
| 120 | conf.set("test.build.data", TEST_ROOT_DIR); |
---|
| 121 | |
---|
| 122 | // Launch job with default option for temp dir. |
---|
| 123 | // i.e. temp dir is ./tmp |
---|
| 124 | JobClient.runJob(conf); |
---|
| 125 | outFs.delete(outDir, true); |
---|
| 126 | |
---|
| 127 | // Launch job by giving relative path to temp dir. |
---|
| 128 | conf.set("mapred.child.tmp", "../temp"); |
---|
| 129 | JobClient.runJob(conf); |
---|
| 130 | outFs.delete(outDir, true); |
---|
| 131 | |
---|
| 132 | // Launch job by giving absolute path to temp dir |
---|
| 133 | conf.set("mapred.child.tmp", "/tmp"); |
---|
| 134 | JobClient.runJob(conf); |
---|
| 135 | outFs.delete(outDir, true); |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | /** |
---|
| 139 | * Tests task's temp directory. |
---|
| 140 | * |
---|
| 141 | * In this test, we give different values to mapred.child.tmp |
---|
| 142 | * both relative and absolute. And check whether the temp directory |
---|
| 143 | * is created. We also check whether java.io.tmpdir value is same as |
---|
| 144 | * the directory specified. We create a temp file and check if is is |
---|
| 145 | * created in the directory specified. |
---|
| 146 | */ |
---|
| 147 | public void testTaskTempDir(){ |
---|
| 148 | try { |
---|
| 149 | |
---|
| 150 | // create configuration, dfs, file system and mapred cluster |
---|
| 151 | dfs = new MiniDFSCluster(new Configuration(), 1, true, null); |
---|
| 152 | fileSys = dfs.getFileSystem(); |
---|
| 153 | mr = new MiniMRCluster(2, fileSys.getUri().toString(), 1); |
---|
| 154 | JobConf conf = mr.createJobConf(); |
---|
| 155 | |
---|
| 156 | // intialize input, output directories |
---|
| 157 | Path inDir = new Path("testing/wc/input"); |
---|
| 158 | Path outDir = new Path("testing/wc/output"); |
---|
| 159 | String input = "The input"; |
---|
| 160 | |
---|
| 161 | launchTest(conf, inDir, outDir, input); |
---|
| 162 | |
---|
| 163 | } catch(Exception e) { |
---|
| 164 | e.printStackTrace(); |
---|
| 165 | fail("Exception in testing temp dir"); |
---|
| 166 | // close file system and shut down dfs and mapred cluster |
---|
| 167 | try { |
---|
| 168 | if (fileSys != null) { |
---|
| 169 | fileSys.close(); |
---|
| 170 | } |
---|
| 171 | if (dfs != null) { |
---|
| 172 | dfs.shutdown(); |
---|
| 173 | } |
---|
| 174 | if (mr != null) { |
---|
| 175 | mr.shutdown(); |
---|
| 176 | } |
---|
| 177 | } catch (IOException ioe) { |
---|
| 178 | LOG.info("IO exception in closing file system)" ); |
---|
| 179 | ioe.printStackTrace(); |
---|
| 180 | } |
---|
| 181 | } |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | public static void main(String args[]){ |
---|
| 185 | TestMiniMRTaskTempDir test = new TestMiniMRTaskTempDir(); |
---|
| 186 | test.testTaskTempDir(); |
---|
| 187 | } |
---|
| 188 | } |
---|