[120] | 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; |
---|
| 20 | |
---|
| 21 | import java.io.IOException; |
---|
| 22 | import junit.framework.TestCase; |
---|
| 23 | |
---|
| 24 | import org.apache.hadoop.fs.FSDataOutputStream; |
---|
| 25 | import org.apache.hadoop.fs.FileSystem; |
---|
| 26 | import org.apache.hadoop.fs.Path; |
---|
| 27 | import org.apache.hadoop.mapred.JobHistory.JobInfo; |
---|
| 28 | import org.apache.hadoop.mapred.JobHistory.RecordTypes; |
---|
| 29 | |
---|
| 30 | /** |
---|
| 31 | * Tests the JobHistory parser with different versions of job history files. |
---|
| 32 | * This may have to change when new versions of job history files come up. |
---|
| 33 | */ |
---|
| 34 | public class TestJobHistoryVersion extends TestCase { |
---|
| 35 | private static final String HOSTNAME = "localhost"; |
---|
| 36 | private static final String TIME= "1234567890123"; |
---|
| 37 | private static final String USER = "user"; |
---|
| 38 | private static final String JOBNAME = "job"; |
---|
| 39 | private static final String JOB = "job_200809180000_0001"; |
---|
| 40 | private static final String FILENAME = |
---|
| 41 | HOSTNAME + "_" + TIME + "_" + JOB + "_" + USER + "_" + JOBNAME; |
---|
| 42 | private static final String TASK_ID = "tip_200809180000_0001_0"; |
---|
| 43 | private static final String TASK_ATTEMPT_ID = |
---|
| 44 | "attempt_200809180000_0001_0_1234567890123"; |
---|
| 45 | private static final String COUNTERS = |
---|
| 46 | "Job Counters.Launched map tasks:1," |
---|
| 47 | + "Map-Reduce Framework.Map input records:0," |
---|
| 48 | + "Map-Reduce Framework.Map input bytes:0," |
---|
| 49 | + "File Systems.HDFS bytes written:0,"; |
---|
| 50 | private static final Path TEST_DIR = |
---|
| 51 | new Path(System.getProperty("test.build.data", "/tmp"), |
---|
| 52 | "test-history-version"); |
---|
| 53 | private static final String DELIM = "."; |
---|
| 54 | |
---|
| 55 | /** |
---|
| 56 | * Creates a job history file of a given specific version. This method should |
---|
| 57 | * change if format/content of future versions of job history file changes. |
---|
| 58 | */ |
---|
| 59 | private void writeHistoryFile(FSDataOutputStream out, long version) |
---|
| 60 | throws IOException { |
---|
| 61 | String delim = "\n"; // '\n' for version 0 |
---|
| 62 | String counters = COUNTERS; |
---|
| 63 | String jobConf = "job.xml"; |
---|
| 64 | if (version > 0) { // line delimeter should be '.' for later versions |
---|
| 65 | // Change the delimiter |
---|
| 66 | delim = DELIM + delim; |
---|
| 67 | |
---|
| 68 | // Write the version line |
---|
| 69 | out.writeBytes(RecordTypes.Meta.name() + " VERSION=\"" |
---|
| 70 | + JobHistory.VERSION + "\" " + delim); |
---|
| 71 | jobConf = JobHistory.escapeString(jobConf); |
---|
| 72 | counters = JobHistory.escapeString(counters); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | // Write the job-start line |
---|
| 76 | |
---|
| 77 | |
---|
| 78 | out.writeBytes("Job JOBID=\"" + JOB + "\" JOBNAME=\"" + JOBNAME + "\"" |
---|
| 79 | + " USER=\"" + USER + "\" SUBMIT_TIME=\"" + TIME + "\"" |
---|
| 80 | + " JOBCONF=\"" + jobConf + "\" " + delim); |
---|
| 81 | |
---|
| 82 | // Write the job-launch line |
---|
| 83 | out.writeBytes("Job JOBID=\"" + JOB + "\" LAUNCH_TIME=\"" + TIME + "\"" |
---|
| 84 | + " TOTAL_MAPS=\"1\" TOTAL_REDUCES=\"0\" " + delim); |
---|
| 85 | |
---|
| 86 | // Write the task start line |
---|
| 87 | out.writeBytes("Task TASKID=\"" + TASK_ID + "\" TASK_TYPE=\"MAP\"" |
---|
| 88 | + " START_TIME=\"" + TIME + "\" SPLITS=\"\"" |
---|
| 89 | + " TOTAL_MAPS=\"1\" TOTAL_REDUCES=\"0\" " + delim); |
---|
| 90 | |
---|
| 91 | // Write the attempt start line |
---|
| 92 | out.writeBytes("MapAttempt TASK_TYPE=\"MAP\" TASKID=\"" + TASK_ID + "\"" |
---|
| 93 | + " TASK_ATTEMPT_ID=\"" + TASK_ATTEMPT_ID + "\"" |
---|
| 94 | + " START_TIME=\"" + TIME + "\"" |
---|
| 95 | + " HOSTNAME=\"" + HOSTNAME + "\" " + delim); |
---|
| 96 | |
---|
| 97 | // Write the attempt finish line |
---|
| 98 | out.writeBytes("MapAttempt TASK_TYPE=\"MAP\" TASKID=\"" + TASK_ID + "\"" |
---|
| 99 | + " TASK_ATTEMPT_ID=\"" + TASK_ATTEMPT_ID + "\"" |
---|
| 100 | + " FINISH_TIME=\"" + TIME + "\"" |
---|
| 101 | + " TASK_STATUS=\"SUCCESS\"" |
---|
| 102 | + " HOSTNAME=\"" + HOSTNAME + "\" " + delim); |
---|
| 103 | |
---|
| 104 | // Write the task finish line |
---|
| 105 | out.writeBytes("Task TASKID=\"" + TASK_ID + "\" TASK_TYPE=\"MAP\"" |
---|
| 106 | + " TASK_STATUS=\"SUCCESS\"" |
---|
| 107 | + " FINISH_TIME=\"" + TIME + "\"" |
---|
| 108 | + " COUNTERS=\"" + counters + "\" " + delim); |
---|
| 109 | |
---|
| 110 | // Write the job-finish line |
---|
| 111 | out.writeBytes("Job JOBID=\"" + JOB + "\" FINISH_TIME=\"" + TIME + "\"" |
---|
| 112 | + " TOTAL_MAPS=\"1\" TOTAL_REDUCES=\"0\"" |
---|
| 113 | + " JOB_STATUS=\"SUCCESS\" FINISHED_MAPS=\"1\"" |
---|
| 114 | + " FINISHED_REDUCES=\"0\" FAILED_MAPS=\"0\"" |
---|
| 115 | + " FAILED_REDUCES=\"0\"" |
---|
| 116 | + " COUNTERS=\"" + counters + "\" " + delim); |
---|
| 117 | |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | /** |
---|
| 121 | * Tests the JobHistory parser with different versions of job history files |
---|
| 122 | */ |
---|
| 123 | public void testJobHistoryVersion() throws IOException { |
---|
| 124 | // If new job history version comes up, the modified parser may fail for |
---|
| 125 | // the history file created by writeHistoryFile(). |
---|
| 126 | for (long version = 0; version <= JobHistory.VERSION; version++) { |
---|
| 127 | JobConf conf = new JobConf(); |
---|
| 128 | FileSystem fs = FileSystem.getLocal(conf); |
---|
| 129 | |
---|
| 130 | // cleanup |
---|
| 131 | fs.delete(TEST_DIR, true); |
---|
| 132 | |
---|
| 133 | Path historyPath = new Path(TEST_DIR + "/_logs/history/" + |
---|
| 134 | FILENAME + version); |
---|
| 135 | |
---|
| 136 | fs.delete(historyPath, false); |
---|
| 137 | |
---|
| 138 | FSDataOutputStream out = fs.create(historyPath); |
---|
| 139 | writeHistoryFile(out, version); |
---|
| 140 | out.close(); |
---|
| 141 | |
---|
| 142 | JobInfo job = new JobHistory.JobInfo(JOB); |
---|
| 143 | DefaultJobHistoryParser.parseJobTasks(historyPath.toString(), job, fs); |
---|
| 144 | |
---|
| 145 | assertTrue("Failed to parse jobhistory files of version " + version, |
---|
| 146 | job.getAllTasks().size() > 0); |
---|
| 147 | |
---|
| 148 | // cleanup |
---|
| 149 | fs.delete(TEST_DIR, true); |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | } |
---|