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 static org.junit.Assert.assertEquals;
21 
22 import java.io.File;
23 import java.io.IOException;
24 
25 import org.apache.hadoop.examples.SleepJob;
26 import org.junit.Test;
27 
28 public class TestTaskTrackerInstrumentation {
29 
30   @Test
testStartup()31   public void testStartup() throws IOException {
32     MiniMRCluster mr = null;
33     try {
34       JobConf jtConf = new JobConf();
35       // Set a bad class.
36       jtConf.set("mapred.tasktracker.instrumentation",
37           "org.nowhere.FUBAR");
38       mr = new MiniMRCluster(1, "file:///", 1, null, null, jtConf);
39       // Assert that the TT fell back to default class.
40       TaskTracker tt = mr.getTaskTrackerRunner(0).getTaskTracker();
41       assertEquals(TaskTrackerInstrumentation.create(tt).getClass(),
42           mr.getTaskTrackerRunner(0).getTaskTracker()
43           .getTaskTrackerInstrumentation().getClass());
44     } finally {
45       mr.shutdown();
46     }
47   }
48 
49   @Test
testSlots()50   public void testSlots() throws IOException {
51     MiniMRCluster mr = null;
52     try {
53       JobConf jtConf = new JobConf();
54       jtConf.set("mapred.tasktracker.instrumentation",
55           MyTaskTrackerMetricsInst.class.getName());
56       mr = new MiniMRCluster(1, "file:///", 1, null, null, jtConf);
57       MyTaskTrackerMetricsInst instr = (MyTaskTrackerMetricsInst)
58         mr.getTaskTrackerRunner(0).getTaskTracker()
59         .getTaskTrackerInstrumentation();
60 
61       JobConf conf = mr.createJobConf();
62       SleepJob job = new SleepJob();
63       job.setConf(conf);
64       int numMapTasks = 3;
65       int numReduceTasks = 2;
66       job.run(numMapTasks, numReduceTasks, 1, 1, 1, 1);
67 
68       synchronized (instr) {
69         // 5 regular tasks + 2 setup/cleanup tasks.
70         assertEquals(7, instr.complete);
71         assertEquals(7, instr.end);
72         assertEquals(7, instr.launch);
73       }
74     } finally {
75       if (mr != null) {
76         mr.shutdown();
77       }
78     }
79   }
80 
81   static class MyTaskTrackerMetricsInst extends TaskTrackerInstrumentation  {
82     public int complete = 0;
83     public int launch = 0;
84     public int end = 0;
85 
MyTaskTrackerMetricsInst(TaskTracker tracker)86     public MyTaskTrackerMetricsInst(TaskTracker tracker) {
87       super(tracker);
88     }
89 
90     @Override
completeTask(TaskAttemptID t)91     public void completeTask(TaskAttemptID t) {
92       this.complete++;
93     }
94 
95     @Override
reportTaskLaunch(TaskAttemptID t, File stdout, File stderr)96     public void reportTaskLaunch(TaskAttemptID t, File stdout, File stderr) {
97       this.launch++;
98     }
99 
100     @Override
reportTaskEnd(TaskAttemptID t)101     public void reportTaskEnd(TaskAttemptID t) {
102       this.end++;
103     }
104   }
105 }
106