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.jobcontrol; |
---|
20 | |
---|
21 | import java.io.IOException; |
---|
22 | import java.util.ArrayList; |
---|
23 | |
---|
24 | import org.apache.commons.logging.Log; |
---|
25 | import org.apache.commons.logging.LogFactory; |
---|
26 | import org.apache.hadoop.fs.FileSystem; |
---|
27 | import org.apache.hadoop.fs.Path; |
---|
28 | import org.apache.hadoop.mapred.HadoopTestCase; |
---|
29 | import org.apache.hadoop.mapred.JobConf; |
---|
30 | |
---|
31 | /** |
---|
32 | * HadoopTestCase that tests the local job runner. |
---|
33 | */ |
---|
34 | public class TestLocalJobControl extends HadoopTestCase { |
---|
35 | |
---|
36 | public static final Log LOG = LogFactory.getLog(TestLocalJobControl.class |
---|
37 | .getName()); |
---|
38 | |
---|
39 | /** |
---|
40 | * Initialises a new instance of this test case to use a Local MR cluster and |
---|
41 | * a local filesystem. |
---|
42 | * |
---|
43 | * @throws IOException If an error occurs initialising this object. |
---|
44 | */ |
---|
45 | public TestLocalJobControl() throws IOException { |
---|
46 | super(HadoopTestCase.LOCAL_MR, HadoopTestCase.LOCAL_FS, 2, 2); |
---|
47 | } |
---|
48 | |
---|
49 | /** |
---|
50 | * This is a main function for testing JobControl class. It first cleans all |
---|
51 | * the dirs it will use. Then it generates some random text data in |
---|
52 | * TestJobControlData/indir. Then it creates 4 jobs: Job 1: copy data from |
---|
53 | * indir to outdir_1 Job 2: copy data from indir to outdir_2 Job 3: copy data |
---|
54 | * from outdir_1 and outdir_2 to outdir_3 Job 4: copy data from outdir to |
---|
55 | * outdir_4 The jobs 1 and 2 have no dependency. The job 3 depends on jobs 1 |
---|
56 | * and 2. The job 4 depends on job 3. |
---|
57 | * |
---|
58 | * Then it creates a JobControl object and add the 4 jobs to the JobControl |
---|
59 | * object. Finally, it creates a thread to run the JobControl object and |
---|
60 | * monitors/reports the job states. |
---|
61 | */ |
---|
62 | public void testLocalJobControlDataCopy() throws Exception { |
---|
63 | |
---|
64 | FileSystem fs = FileSystem.get(createJobConf()); |
---|
65 | Path rootDataDir = new Path(System.getProperty("test.build.data", "."), |
---|
66 | "TestLocalJobControlData"); |
---|
67 | Path indir = new Path(rootDataDir, "indir"); |
---|
68 | Path outdir_1 = new Path(rootDataDir, "outdir_1"); |
---|
69 | Path outdir_2 = new Path(rootDataDir, "outdir_2"); |
---|
70 | Path outdir_3 = new Path(rootDataDir, "outdir_3"); |
---|
71 | Path outdir_4 = new Path(rootDataDir, "outdir_4"); |
---|
72 | |
---|
73 | JobControlTestUtils.cleanData(fs, indir); |
---|
74 | JobControlTestUtils.generateData(fs, indir); |
---|
75 | |
---|
76 | JobControlTestUtils.cleanData(fs, outdir_1); |
---|
77 | JobControlTestUtils.cleanData(fs, outdir_2); |
---|
78 | JobControlTestUtils.cleanData(fs, outdir_3); |
---|
79 | JobControlTestUtils.cleanData(fs, outdir_4); |
---|
80 | |
---|
81 | ArrayList<Job> dependingJobs = null; |
---|
82 | |
---|
83 | ArrayList<Path> inPaths_1 = new ArrayList<Path>(); |
---|
84 | inPaths_1.add(indir); |
---|
85 | JobConf jobConf_1 = JobControlTestUtils.createCopyJob(inPaths_1, outdir_1); |
---|
86 | Job job_1 = new Job(jobConf_1, dependingJobs); |
---|
87 | ArrayList<Path> inPaths_2 = new ArrayList<Path>(); |
---|
88 | inPaths_2.add(indir); |
---|
89 | JobConf jobConf_2 = JobControlTestUtils.createCopyJob(inPaths_2, outdir_2); |
---|
90 | Job job_2 = new Job(jobConf_2, dependingJobs); |
---|
91 | |
---|
92 | ArrayList<Path> inPaths_3 = new ArrayList<Path>(); |
---|
93 | inPaths_3.add(outdir_1); |
---|
94 | inPaths_3.add(outdir_2); |
---|
95 | JobConf jobConf_3 = JobControlTestUtils.createCopyJob(inPaths_3, outdir_3); |
---|
96 | dependingJobs = new ArrayList<Job>(); |
---|
97 | dependingJobs.add(job_1); |
---|
98 | dependingJobs.add(job_2); |
---|
99 | Job job_3 = new Job(jobConf_3, dependingJobs); |
---|
100 | |
---|
101 | ArrayList<Path> inPaths_4 = new ArrayList<Path>(); |
---|
102 | inPaths_4.add(outdir_3); |
---|
103 | JobConf jobConf_4 = JobControlTestUtils.createCopyJob(inPaths_4, outdir_4); |
---|
104 | dependingJobs = new ArrayList<Job>(); |
---|
105 | dependingJobs.add(job_3); |
---|
106 | Job job_4 = new Job(jobConf_4, dependingJobs); |
---|
107 | |
---|
108 | JobControl theControl = new JobControl("Test"); |
---|
109 | theControl.addJob(job_1); |
---|
110 | theControl.addJob(job_2); |
---|
111 | theControl.addJob(job_3); |
---|
112 | theControl.addJob(job_4); |
---|
113 | |
---|
114 | Thread theController = new Thread(theControl); |
---|
115 | theController.start(); |
---|
116 | while (!theControl.allFinished()) { |
---|
117 | LOG.debug("Jobs in waiting state: " + theControl.getWaitingJobs().size()); |
---|
118 | LOG.debug("Jobs in ready state: " + theControl.getReadyJobs().size()); |
---|
119 | LOG.debug("Jobs in running state: " + theControl.getRunningJobs().size()); |
---|
120 | LOG.debug("Jobs in success state: " |
---|
121 | + theControl.getSuccessfulJobs().size()); |
---|
122 | LOG.debug("Jobs in failed state: " + theControl.getFailedJobs().size()); |
---|
123 | LOG.debug("\n"); |
---|
124 | try { |
---|
125 | Thread.sleep(5000); |
---|
126 | } catch (Exception e) { |
---|
127 | |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | assertEquals("Some jobs failed", 0, theControl.getFailedJobs().size()); |
---|
132 | theControl.stop(); |
---|
133 | } |
---|
134 | |
---|
135 | } |
---|