1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.mapreduce;
19
20 import java.io.IOException;
21 import java.lang.reflect.Constructor;
22 import java.lang.reflect.InvocationTargetException;
23 import java.lang.reflect.Method;
24
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.mapred.JobConf;
27 import org.apache.hadoop.mapred.MiniMRCluster;
28 import org.apache.hadoop.mapreduce.Job;
29 import org.apache.hadoop.mapreduce.JobContext;
30 import org.apache.hadoop.mapreduce.JobID;
31
32
33
34
35
36
37
38 abstract public class MapreduceTestingShim {
39 private static MapreduceTestingShim instance;
40 private static Class[] emptyParam = new Class[] {};
41
42 static {
43 try {
44
45 Class c = Class
46 .forName("org.apache.hadoop.mapreduce.task.TaskAttemptContextImpl");
47 instance = new MapreduceV2Shim();
48 } catch (Exception e) {
49 instance = new MapreduceV1Shim();
50 }
51 }
52
53 abstract public JobContext newJobContext(Configuration jobConf)
54 throws IOException;
55
56 abstract public JobConf obtainJobConf(MiniMRCluster cluster);
57
58 public static JobContext createJobContext(Configuration jobConf)
59 throws IOException {
60 return instance.newJobContext(jobConf);
61 }
62
63 public static JobConf getJobConf(MiniMRCluster cluster) {
64 return instance.obtainJobConf(cluster);
65 }
66
67 private static class MapreduceV1Shim extends MapreduceTestingShim {
68 public JobContext newJobContext(Configuration jobConf) throws IOException {
69
70
71 JobID jobId = new JobID();
72 Constructor<JobContext> c;
73 try {
74 c = JobContext.class.getConstructor(Configuration.class, JobID.class);
75 return c.newInstance(jobConf, jobId);
76 } catch (Exception e) {
77 throw new IllegalStateException(
78 "Failed to instantiate new JobContext(jobConf, new JobID())", e);
79 }
80 }
81 public JobConf obtainJobConf(MiniMRCluster cluster) {
82 if (cluster == null) return null;
83 try {
84 Object runner = cluster.getJobTrackerRunner();
85 Method meth = runner.getClass().getDeclaredMethod("getJobTracker", emptyParam);
86 Object tracker = meth.invoke(runner, new Object []{});
87 Method m = tracker.getClass().getDeclaredMethod("getConf", emptyParam);
88 return (JobConf) m.invoke(tracker, new Object []{});
89 } catch (NoSuchMethodException nsme) {
90 return null;
91 } catch (InvocationTargetException ite) {
92 return null;
93 } catch (IllegalAccessException iae) {
94 return null;
95 }
96 }
97 };
98
99 private static class MapreduceV2Shim extends MapreduceTestingShim {
100 public JobContext newJobContext(Configuration jobConf) {
101
102
103 try {
104 Method m = Job.class.getMethod("getInstance", Configuration.class);
105 return (JobContext) m.invoke(null, jobConf);
106 } catch (Exception e) {
107 e.printStackTrace();
108 throw new IllegalStateException(
109 "Failed to return from Job.getInstance(jobConf)");
110 }
111 }
112 public JobConf obtainJobConf(MiniMRCluster cluster) {
113 try {
114 Method meth = MiniMRCluster.class.getMethod("getJobTrackerConf", emptyParam);
115 return (JobConf) meth.invoke(cluster, new Object []{});
116 } catch (NoSuchMethodException nsme) {
117 return null;
118 } catch (InvocationTargetException ite) {
119 return null;
120 } catch (IllegalAccessException iae) {
121 return null;
122 }
123 }
124 };
125
126 }