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.util.ArrayList; |
---|
22 | |
---|
23 | import org.apache.hadoop.conf.Configuration; |
---|
24 | import org.apache.hadoop.fs.FileSystem; |
---|
25 | import org.apache.hadoop.fs.Path; |
---|
26 | import org.apache.hadoop.mapred.JobConf; |
---|
27 | |
---|
28 | /** |
---|
29 | * This class performs unit test for Job/JobControl classes. |
---|
30 | * |
---|
31 | */ |
---|
32 | public class TestJobControl extends junit.framework.TestCase { |
---|
33 | |
---|
34 | /** |
---|
35 | * This is a main function for testing JobControl class. |
---|
36 | * It first cleans all the dirs it will use. Then it generates some random text |
---|
37 | * data in TestJobControlData/indir. Then it creates 4 jobs: |
---|
38 | * Job 1: copy data from indir to outdir_1 |
---|
39 | * Job 2: copy data from indir to outdir_2 |
---|
40 | * Job 3: copy data from outdir_1 and outdir_2 to outdir_3 |
---|
41 | * Job 4: copy data from outdir to outdir_4 |
---|
42 | * The jobs 1 and 2 have no dependency. The job 3 depends on jobs 1 and 2. |
---|
43 | * The job 4 depends on job 3. |
---|
44 | * |
---|
45 | * Then it creates a JobControl object and add the 4 jobs to the JobControl object. |
---|
46 | * Finally, it creates a thread to run the JobControl object and monitors/reports |
---|
47 | * the job states. |
---|
48 | */ |
---|
49 | public static void doJobControlTest() throws Exception { |
---|
50 | |
---|
51 | Configuration defaults = new Configuration(); |
---|
52 | FileSystem fs = FileSystem.get(defaults); |
---|
53 | Path rootDataDir = new Path(System.getProperty("test.build.data", "."), "TestJobControlData"); |
---|
54 | Path indir = new Path(rootDataDir, "indir"); |
---|
55 | Path outdir_1 = new Path(rootDataDir, "outdir_1"); |
---|
56 | Path outdir_2 = new Path(rootDataDir, "outdir_2"); |
---|
57 | Path outdir_3 = new Path(rootDataDir, "outdir_3"); |
---|
58 | Path outdir_4 = new Path(rootDataDir, "outdir_4"); |
---|
59 | |
---|
60 | JobControlTestUtils.cleanData(fs, indir); |
---|
61 | JobControlTestUtils.generateData(fs, indir); |
---|
62 | |
---|
63 | JobControlTestUtils.cleanData(fs, outdir_1); |
---|
64 | JobControlTestUtils.cleanData(fs, outdir_2); |
---|
65 | JobControlTestUtils.cleanData(fs, outdir_3); |
---|
66 | JobControlTestUtils.cleanData(fs, outdir_4); |
---|
67 | |
---|
68 | ArrayList<Job> dependingJobs = null; |
---|
69 | |
---|
70 | ArrayList<Path> inPaths_1 = new ArrayList<Path>(); |
---|
71 | inPaths_1.add(indir); |
---|
72 | JobConf jobConf_1 = JobControlTestUtils.createCopyJob(inPaths_1, outdir_1); |
---|
73 | Job job_1 = new Job(jobConf_1, dependingJobs); |
---|
74 | ArrayList<Path> inPaths_2 = new ArrayList<Path>(); |
---|
75 | inPaths_2.add(indir); |
---|
76 | JobConf jobConf_2 = JobControlTestUtils.createCopyJob(inPaths_2, outdir_2); |
---|
77 | Job job_2 = new Job(jobConf_2, dependingJobs); |
---|
78 | |
---|
79 | ArrayList<Path> inPaths_3 = new ArrayList<Path>(); |
---|
80 | inPaths_3.add(outdir_1); |
---|
81 | inPaths_3.add(outdir_2); |
---|
82 | JobConf jobConf_3 = JobControlTestUtils.createCopyJob(inPaths_3, outdir_3); |
---|
83 | dependingJobs = new ArrayList<Job>(); |
---|
84 | dependingJobs.add(job_1); |
---|
85 | dependingJobs.add(job_2); |
---|
86 | Job job_3 = new Job(jobConf_3, dependingJobs); |
---|
87 | |
---|
88 | ArrayList<Path> inPaths_4 = new ArrayList<Path>(); |
---|
89 | inPaths_4.add(outdir_3); |
---|
90 | JobConf jobConf_4 = JobControlTestUtils.createCopyJob(inPaths_4, outdir_4); |
---|
91 | dependingJobs = new ArrayList<Job>(); |
---|
92 | dependingJobs.add(job_3); |
---|
93 | Job job_4 = new Job(jobConf_4, dependingJobs); |
---|
94 | |
---|
95 | JobControl theControl = new JobControl("Test"); |
---|
96 | theControl.addJob(job_1); |
---|
97 | theControl.addJob(job_2); |
---|
98 | theControl.addJob(job_3); |
---|
99 | theControl.addJob(job_4); |
---|
100 | |
---|
101 | Thread theController = new Thread(theControl); |
---|
102 | theController.start(); |
---|
103 | while (!theControl.allFinished()) { |
---|
104 | |
---|
105 | System.out.println("Jobs in waiting state: " |
---|
106 | + theControl.getWaitingJobs().size()); |
---|
107 | System.out.println("Jobs in ready state: " |
---|
108 | + theControl.getReadyJobs().size()); |
---|
109 | System.out.println("Jobs in running state: " |
---|
110 | + theControl.getRunningJobs().size()); |
---|
111 | System.out.println("Jobs in success state: " |
---|
112 | + theControl.getSuccessfulJobs().size()); |
---|
113 | System.out.println("Jobs in failed state: " |
---|
114 | + theControl.getFailedJobs().size()); |
---|
115 | System.out.println("\n"); |
---|
116 | |
---|
117 | try { |
---|
118 | Thread.sleep(5000); |
---|
119 | } catch (Exception e) { |
---|
120 | |
---|
121 | } |
---|
122 | } |
---|
123 | System.out.println("Jobs are all done???"); |
---|
124 | System.out.println("Jobs in waiting state: " |
---|
125 | + theControl.getWaitingJobs().size()); |
---|
126 | System.out.println("Jobs in ready state: " |
---|
127 | + theControl.getReadyJobs().size()); |
---|
128 | System.out.println("Jobs in running state: " |
---|
129 | + theControl.getRunningJobs().size()); |
---|
130 | System.out.println("Jobs in success state: " |
---|
131 | + theControl.getSuccessfulJobs().size()); |
---|
132 | System.out.println("Jobs in failed state: " |
---|
133 | + theControl.getFailedJobs().size()); |
---|
134 | System.out.println("\n"); |
---|
135 | |
---|
136 | if (job_1.getState() != Job.FAILED && |
---|
137 | job_1.getState() != Job.DEPENDENT_FAILED && |
---|
138 | job_1.getState() != Job.SUCCESS) { |
---|
139 | |
---|
140 | String states = "job_1: " + job_1.getState() + "\n"; |
---|
141 | throw new Exception("The state of job_1 is not in a complete state\n" + states); |
---|
142 | } |
---|
143 | |
---|
144 | if (job_2.getState() != Job.FAILED && |
---|
145 | job_2.getState() != Job.DEPENDENT_FAILED && |
---|
146 | job_2.getState() != Job.SUCCESS) { |
---|
147 | |
---|
148 | String states = "job_2: " + job_2.getState() + "\n"; |
---|
149 | throw new Exception("The state of job_2 is not in a complete state\n" + states); |
---|
150 | } |
---|
151 | |
---|
152 | if (job_3.getState() != Job.FAILED && |
---|
153 | job_3.getState() != Job.DEPENDENT_FAILED && |
---|
154 | job_3.getState() != Job.SUCCESS) { |
---|
155 | |
---|
156 | String states = "job_3: " + job_3.getState() + "\n"; |
---|
157 | throw new Exception("The state of job_3 is not in a complete state\n" + states); |
---|
158 | } |
---|
159 | if (job_4.getState() != Job.FAILED && |
---|
160 | job_4.getState() != Job.DEPENDENT_FAILED && |
---|
161 | job_4.getState() != Job.SUCCESS) { |
---|
162 | |
---|
163 | String states = "job_4: " + job_4.getState() + "\n"; |
---|
164 | throw new Exception("The state of job_4 is not in a complete state\n" + states); |
---|
165 | } |
---|
166 | |
---|
167 | if (job_1.getState() == Job.FAILED || |
---|
168 | job_2.getState() == Job.FAILED || |
---|
169 | job_1.getState() == Job.DEPENDENT_FAILED || |
---|
170 | job_2.getState() == Job.DEPENDENT_FAILED) { |
---|
171 | if (job_3.getState() != Job.DEPENDENT_FAILED) { |
---|
172 | String states = "job_1: " + job_1.getState() + "\n"; |
---|
173 | states = "job_2: " + job_2.getState() + "\n"; |
---|
174 | states = "job_3: " + job_3.getState() + "\n"; |
---|
175 | states = "job_4: " + job_4.getState() + "\n"; |
---|
176 | throw new Exception("The states of jobs 1, 2, 3, 4 are not consistent\n" + states); |
---|
177 | } |
---|
178 | } |
---|
179 | if (job_3.getState() == Job.FAILED || |
---|
180 | job_3.getState() == Job.DEPENDENT_FAILED) { |
---|
181 | if (job_4.getState() != Job.DEPENDENT_FAILED) { |
---|
182 | String states = "job_3: " + job_3.getState() + "\n"; |
---|
183 | states = "job_4: " + job_4.getState() + "\n"; |
---|
184 | throw new Exception("The states of jobs 3, 4 are not consistent\n" + states); |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | theControl.stop(); |
---|
189 | } |
---|
190 | |
---|
191 | public void testJobControl() throws Exception { |
---|
192 | doJobControlTest(); |
---|
193 | } |
---|
194 | |
---|
195 | public static void main(String[] args) { |
---|
196 | TestJobControl test = new TestJobControl(); |
---|
197 | try { |
---|
198 | test.testJobControl(); |
---|
199 | } |
---|
200 | catch (Exception e) { |
---|
201 | e.printStackTrace(); |
---|
202 | } |
---|
203 | } |
---|
204 | } |
---|