use of java.io.FileReader in project hadoop by apache.
the class TestContainerManager method testContainerLaunchAndStop.
//@Test
public void testContainerLaunchAndStop() throws IOException, InterruptedException, YarnException {
containerManager.start();
File scriptFile = Shell.appendScriptExtension(tmpDir, "scriptFile");
PrintWriter fileWriter = new PrintWriter(scriptFile);
File processStartFile = new File(tmpDir, "start_file.txt").getAbsoluteFile();
// ////// Construct the Container-id
ContainerId cId = createContainerId(0);
if (Shell.WINDOWS) {
fileWriter.println("@echo Hello World!> " + processStartFile);
fileWriter.println("@echo " + cId + ">> " + processStartFile);
fileWriter.println("@ping -n 100 127.0.0.1 >nul");
} else {
// So that start file is readable by the test
fileWriter.write("\numask 0");
fileWriter.write("\necho Hello World! > " + processStartFile);
fileWriter.write("\necho $$ >> " + processStartFile);
fileWriter.write("\nexec sleep 100");
}
fileWriter.close();
ContainerLaunchContext containerLaunchContext = recordFactory.newRecordInstance(ContainerLaunchContext.class);
URL resource_alpha = URL.fromPath(localFS.makeQualified(new Path(scriptFile.getAbsolutePath())));
LocalResource rsrc_alpha = recordFactory.newRecordInstance(LocalResource.class);
rsrc_alpha.setResource(resource_alpha);
rsrc_alpha.setSize(-1);
rsrc_alpha.setVisibility(LocalResourceVisibility.APPLICATION);
rsrc_alpha.setType(LocalResourceType.FILE);
rsrc_alpha.setTimestamp(scriptFile.lastModified());
String destinationFile = "dest_file";
Map<String, LocalResource> localResources = new HashMap<String, LocalResource>();
localResources.put(destinationFile, rsrc_alpha);
containerLaunchContext.setLocalResources(localResources);
List<String> commands = Arrays.asList(Shell.getRunScriptCommand(scriptFile));
containerLaunchContext.setCommands(commands);
StartContainerRequest scRequest = StartContainerRequest.newInstance(containerLaunchContext, createContainerToken(cId, DUMMY_RM_IDENTIFIER, context.getNodeId(), user, context.getContainerTokenSecretManager()));
List<StartContainerRequest> list = new ArrayList<>();
list.add(scRequest);
StartContainersRequest allRequests = StartContainersRequest.newInstance(list);
containerManager.startContainers(allRequests);
int timeoutSecs = 0;
while (!processStartFile.exists() && timeoutSecs++ < 20) {
Thread.sleep(1000);
LOG.info("Waiting for process start-file to be created");
}
Assert.assertTrue("ProcessStartFile doesn't exist!", processStartFile.exists());
// Now verify the contents of the file
BufferedReader reader = new BufferedReader(new FileReader(processStartFile));
Assert.assertEquals("Hello World!", reader.readLine());
// Get the pid of the process
String pid = reader.readLine().trim();
// No more lines
Assert.assertEquals(null, reader.readLine());
// Now test the stop functionality.
// Assert that the process is alive
Assert.assertTrue("Process is not alive!", DefaultContainerExecutor.containerIsAlive(pid));
// Once more
Assert.assertTrue("Process is not alive!", DefaultContainerExecutor.containerIsAlive(pid));
List<ContainerId> containerIds = new ArrayList<>();
containerIds.add(cId);
StopContainersRequest stopRequest = StopContainersRequest.newInstance(containerIds);
containerManager.stopContainers(stopRequest);
BaseContainerManagerTest.waitForContainerState(containerManager, cId, ContainerState.COMPLETE);
GetContainerStatusesRequest gcsRequest = GetContainerStatusesRequest.newInstance(containerIds);
ContainerStatus containerStatus = containerManager.getContainerStatuses(gcsRequest).getContainerStatuses().get(0);
int expectedExitCode = ContainerExitStatus.KILLED_BY_APPMASTER;
Assert.assertEquals(expectedExitCode, containerStatus.getExitStatus());
// Assert that the process is not alive anymore
Assert.assertFalse("Process is still alive!", DefaultContainerExecutor.containerIsAlive(pid));
}
use of java.io.FileReader in project hadoop by apache.
the class TestContainerManager method testContainerUpgradeSuccessExplicitRollback.
@Test
public void testContainerUpgradeSuccessExplicitRollback() throws IOException, InterruptedException, YarnException {
String[] pids = testContainerReInitSuccess(false);
// Test that the container can be Restarted after the successful upgrrade.
// Also, since there is a rollback context present before the restart, it
// should be possible to rollback the container AFTER the restart.
pids[1] = doRestartTests(createContainerId(0), new File(tmpDir, "start_file_n.txt").getAbsoluteFile(), "Upgrade World!", pids[1], true);
// Delete the old start File..
File oldStartFile = new File(tmpDir, "start_file_o.txt").getAbsoluteFile();
oldStartFile.delete();
ContainerId cId = createContainerId(0);
// Explicit Rollback
containerManager.rollbackLastReInitialization(cId);
Container container = containerManager.getContext().getContainers().get(cId);
Assert.assertTrue(container.isReInitializing());
// Original should be dead anyway
Assert.assertFalse("Original Process is still alive!", DefaultContainerExecutor.containerIsAlive(pids[0]));
// Wait for new container to startup
int timeoutSecs = 0;
while (container.isReInitializing() && timeoutSecs++ < 20) {
Thread.sleep(1000);
LOG.info("Waiting for ReInitialization to complete..");
}
Assert.assertFalse(container.isReInitializing());
timeoutSecs = 0;
// Wait for new processStartfile to be created
while (!oldStartFile.exists() && timeoutSecs++ < 20) {
Thread.sleep(1000);
LOG.info("Waiting for New process start-file to be created");
}
// Now verify the contents of the file
BufferedReader reader = new BufferedReader(new FileReader(oldStartFile));
Assert.assertEquals("Hello World!", reader.readLine());
// Get the pid of the process
String rolledBackPid = reader.readLine().trim();
// No more lines
Assert.assertEquals(null, reader.readLine());
Assert.assertNotEquals("The Rolled-back process should be a different pid", pids[0], rolledBackPid);
}
use of java.io.FileReader in project hadoop by apache.
the class TestContainerManager method testContainerReInitSuccess.
private String[] testContainerReInitSuccess(boolean autoCommit) throws IOException, InterruptedException, YarnException {
containerManager.start();
// ////// Construct the Container-id
ContainerId cId = createContainerId(0);
File oldStartFile = new File(tmpDir, "start_file_o.txt").getAbsoluteFile();
String pid = prepareInitialContainer(cId, oldStartFile);
File newStartFile = new File(tmpDir, "start_file_n.txt").getAbsoluteFile();
prepareContainerUpgrade(autoCommit, false, false, cId, newStartFile);
// Assert that the First process is not alive anymore
Assert.assertFalse("Process is still alive!", DefaultContainerExecutor.containerIsAlive(pid));
BufferedReader reader = new BufferedReader(new FileReader(newStartFile));
Assert.assertEquals("Upgrade World!", reader.readLine());
// Get the pid of the process
String newPid = reader.readLine().trim();
Assert.assertNotEquals("Old and New Pids must be different !", pid, newPid);
// No more lines
Assert.assertEquals(null, reader.readLine());
reader.close();
// Verify old file still exists and is accessible by
// the new process...
reader = new BufferedReader(new FileReader(oldStartFile));
Assert.assertEquals("Hello World!", reader.readLine());
// Assert that the New process is alive
Assert.assertTrue("New Process is not alive!", DefaultContainerExecutor.containerIsAlive(newPid));
return new String[] { pid, newPid };
}
use of java.io.FileReader in project hadoop by apache.
the class TestContainerManager method doRestartTests.
private String doRestartTests(ContainerId cId, File oldStartFile, String testString, String pid, boolean canRollback) throws YarnException, IOException, InterruptedException {
int beforeRestart = metrics.getRunningContainers();
Container container = containerManager.getContext().getContainers().get(cId);
Assert.assertFalse(container.isReInitializing());
containerManager.restartContainer(cId);
Assert.assertTrue(container.isReInitializing());
// Wait for original process to die and the new process to restart
int timeoutSecs = 0;
while (DefaultContainerExecutor.containerIsAlive(pid) && (metrics.getRunningContainers() == beforeRestart) && container.isReInitializing() && timeoutSecs++ < 20) {
Thread.sleep(1000);
LOG.info("Waiting for Original process to die.." + "and new process to start!!");
}
Assert.assertFalse("Old Process Still alive!!", DefaultContainerExecutor.containerIsAlive(pid));
String newPid = null;
timeoutSecs = 0;
while (timeoutSecs++ < 20) {
LOG.info("Waiting for New process file to be created!!");
// Now verify the contents of the file
BufferedReader reader = new BufferedReader(new FileReader(oldStartFile));
Assert.assertEquals(testString, reader.readLine());
// Get the pid of the process
newPid = reader.readLine().trim();
// No more lines
Assert.assertEquals(null, reader.readLine());
reader.close();
if (!newPid.equals(pid)) {
break;
}
Thread.sleep(1000);
}
// Assert both pids are different
Assert.assertNotEquals(pid, newPid);
// Container cannot rollback from a restart
Assert.assertEquals(canRollback, container.canRollback());
return newPid;
}
use of java.io.FileReader in project hadoop by apache.
the class TestContainerManager method testContainerUpgradeRollbackDueToFailure.
@Test
public void testContainerUpgradeRollbackDueToFailure() throws IOException, InterruptedException, YarnException {
if (Shell.WINDOWS) {
return;
}
containerManager.start();
// ////// Construct the Container-id
ContainerId cId = createContainerId(0);
File oldStartFile = new File(tmpDir, "start_file_o.txt").getAbsoluteFile();
String pid = prepareInitialContainer(cId, oldStartFile);
File newStartFile = new File(tmpDir, "start_file_n.txt").getAbsoluteFile();
prepareContainerUpgrade(false, true, false, cId, newStartFile);
// Assert that the First process is not alive anymore
Assert.assertFalse("Original Process is still alive!", DefaultContainerExecutor.containerIsAlive(pid));
int timeoutSecs = 0;
// Wait for oldStartFile to be created
while (!oldStartFile.exists() && timeoutSecs++ < 20) {
System.out.println("\nFiles: " + Arrays.toString(oldStartFile.getParentFile().list()));
Thread.sleep(1000);
LOG.info("Waiting for New process start-file to be created");
}
// Now verify the contents of the file
BufferedReader reader = new BufferedReader(new FileReader(oldStartFile));
Assert.assertEquals("Hello World!", reader.readLine());
// Get the pid of the process
String rolledBackPid = reader.readLine().trim();
// No more lines
Assert.assertEquals(null, reader.readLine());
Assert.assertNotEquals("The Rolled-back process should be a different pid", pid, rolledBackPid);
}
Aggregations