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.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   * This class provides shims for HBase to interact with the Hadoop 1.0.x and the
34   * Hadoop 0.23.x series.
35   *
36   * NOTE: No testing done against 0.22.x, or 0.21.x.
37   */
38  abstract public class MapreduceTestingShim {
39    private static MapreduceTestingShim instance;
40    private static Class[] emptyParam = new Class[] {};
41  
42    static {
43      try {
44        // This class exists in hadoop 0.22+ but not in Hadoop 20.x/1.x
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        // Implementing:
70        // return new JobContext(jobConf, new JobID());
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       // Implementing:
102       // return Job.getInstance(jobConf);
103       try {
104         Method m = Job.class.getMethod("getInstance", Configuration.class);
105         return (JobContext) m.invoke(null, jobConf); // static method, then arg
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 }