* @throws IOException
*/
@Test
public void testLogTruncationOnFinishingWithJVMReuse() throws IOException {
Configuration conf = setRetainSizes(150L, 150L);
UserLogManager logManager = new UtilsForTests.InLineUserLogManager(conf);
TaskID baseTaskID = new TaskID();
int attemptsCount = 0;
// Assuming the job's retain size is 150
TaskAttemptID attempt1 = new TaskAttemptID(baseTaskID, attemptsCount++);
Task task1 = new MapTask(null, attempt1, 0, new JobSplit.TaskSplitIndex(),
0);
// Let the tasks write logs more than retain-size
writeRealChars(attempt1, attempt1, LogName.SYSLOG, 200, 'A');
File attemptDir = TaskLog.getAttemptDir(attempt1, false);
assertTrue(attemptDir + " doesn't exist!", attemptDir.exists());
// Start another attempt in the same JVM
TaskAttemptID attempt2 = new TaskAttemptID(baseTaskID, attemptsCount++);
Task task2 = new MapTask(null, attempt2, 0, new JobSplit.TaskSplitIndex(),
0);
// Let attempt2 also write some logs
writeRealChars(attempt1, attempt2, LogName.SYSLOG, 100, 'B');
// Start yet another attempt in the same JVM
TaskAttemptID attempt3 = new TaskAttemptID(baseTaskID, attemptsCount++);
Task task3 = new MapTask(null, attempt3, 0, new JobSplit.TaskSplitIndex(),
0);
// Let attempt3 also write some logs
writeRealChars(attempt1, attempt3, LogName.SYSLOG, 225, 'C');
// Finish the JVM.
JVMInfo jvmInfo = new JVMInfo(attemptDir,
Arrays.asList((new Task[] { task1, task2, task3 })));
logManager.addLogEvent(new JvmFinishedEvent(jvmInfo));
// The log-file should now be truncated.
assertTrue(attemptDir.exists());
File logFile = TaskLog.getTaskLogFile(attempt1, false, LogName.SYSLOG);
assertEquals(400 + (2 * truncatedMsgSize), logFile.length());
// The index files should also be proper.
assertEquals(150 + truncatedMsgSize, getAllLogsFileLengths(attempt1, false)
.get(LogName.SYSLOG).longValue());
assertEquals(100, getAllLogsFileLengths(attempt2, false)
.get(LogName.SYSLOG).longValue());
assertEquals(150 + truncatedMsgSize, getAllLogsFileLengths(attempt3, false)
.get(LogName.SYSLOG).longValue());
// assert data for attempt1
String syslog = TestMiniMRMapRedDebugScript.readTaskLog(LogName.SYSLOG,
attempt1, false);
assertTrue(syslog.startsWith(TaskLogsTruncater.TRUNCATED_MSG));
String truncatedLog = syslog.substring(truncatedMsgSize);
for (int i = 0 ; i < 150; i++) {
assertEquals("Truncation didn't happen properly. At "
+ (i + 1) + "th byte, expected 'A' but found "
+ truncatedLog.charAt(i), 'A', truncatedLog.charAt(i));
}
// assert data for attempt2
syslog = TestMiniMRMapRedDebugScript.readTaskLog(LogName.SYSLOG,
attempt2, false);
for (int i = 0 ; i < 100; i++) {
assertEquals("Truncation didn't happen properly. At "
+ (i + 1) + "th byte, expected 'B' but found "
+ truncatedLog.charAt(i), 'B', syslog.charAt(i));
}
// assert data for attempt3
syslog = TestMiniMRMapRedDebugScript.readTaskLog(LogName.SYSLOG,
attempt3, false);
assertTrue(syslog.startsWith(TaskLogsTruncater.TRUNCATED_MSG));
truncatedLog = syslog.substring(truncatedMsgSize);
for (int i = 0 ; i < 150; i++) {
assertEquals("Truncation didn't happen properly. At "
+ (i + 1) + "th byte, expected 'C' but found "
+ truncatedLog.charAt(i), 'C', truncatedLog.charAt(i));
}
logManager.addLogEvent(new JvmFinishedEvent(jvmInfo));
// First and third attempts' logs are only truncated, so include 2*length of
// TRUNCATED_MSG header
assertEquals(400 + 2 * truncatedMsgSize, logFile.length());
}