@Test
public void testResourceReport() throws InterruptedException, ExecutionException, IOException,
URISyntaxException, TimeoutException {
TwillRunner runner = YarnTestUtils.getTwillRunner();
TwillController controller = runner.prepare(new ResourceApplication())
.addLogHandler(new PrinterLogHandler(new PrintWriter(System.out, true)))
.withApplicationArguments("echo")
.withArguments("echo1", "echo1")
.withArguments("echo2", "echo2")
.start();
final CountDownLatch running = new CountDownLatch(1);
controller.addListener(new ServiceListenerAdapter() {
@Override
public void running() {
running.countDown();
}
}, Threads.SAME_THREAD_EXECUTOR);
Assert.assertTrue(running.await(30, TimeUnit.SECONDS));
// wait for 3 echo servers to come up
Iterable<Discoverable> echoServices = controller.discoverService("echo");
Assert.assertTrue(YarnTestUtils.waitForSize(echoServices, 3, 60));
ResourceReport report = controller.getResourceReport();
// make sure resources for echo1 and echo2 are there
Map<String, Collection<TwillRunResources>> usedResources = report.getResources();
Assert.assertEquals(2, usedResources.keySet().size());
Assert.assertTrue(usedResources.containsKey("echo1"));
Assert.assertTrue(usedResources.containsKey("echo2"));
Collection<TwillRunResources> echo1Resources = usedResources.get("echo1");
// 2 instances of echo1
Assert.assertEquals(2, echo1Resources.size());
// TODO: check cores after hadoop-2.1.0
for (TwillRunResources resources : echo1Resources) {
Assert.assertEquals(128, resources.getMemoryMB());
}
Collection<TwillRunResources> echo2Resources = usedResources.get("echo2");
// 2 instances of echo1
Assert.assertEquals(1, echo2Resources.size());
// TODO: check cores after hadoop-2.1.0
for (TwillRunResources resources : echo2Resources) {
Assert.assertEquals(256, resources.getMemoryMB());
}
// Decrease number of instances of echo1 from 2 to 1
controller.changeInstances("echo1", 1);
echoServices = controller.discoverService("echo1");
Assert.assertTrue(YarnTestUtils.waitForSize(echoServices, 1, 60));
report = controller.getResourceReport();
// make sure resources for echo1 and echo2 are there
usedResources = report.getResources();
Assert.assertEquals(2, usedResources.keySet().size());
Assert.assertTrue(usedResources.containsKey("echo1"));
Assert.assertTrue(usedResources.containsKey("echo2"));
echo1Resources = usedResources.get("echo1");
// 1 instance of echo1 now
Assert.assertEquals(1, echo1Resources.size());
// TODO: check cores after hadoop-2.1.0
for (TwillRunResources resources : echo1Resources) {
Assert.assertEquals(128, resources.getMemoryMB());
}
echo2Resources = usedResources.get("echo2");
// 2 instances of echo1
Assert.assertEquals(1, echo2Resources.size());
// TODO: check cores after hadoop-2.1.0
for (TwillRunResources resources : echo2Resources) {
Assert.assertEquals(256, resources.getMemoryMB());
}
controller.stop().get(30, TimeUnit.SECONDS);
// Sleep a bit before exiting.
TimeUnit.SECONDS.sleep(2);
}