From c8abfa43056f44e4fa3ad7a5fda97589060b6c05 Mon Sep 17 00:00:00 2001 From: Alejandro Abdelnur Date: Mon, 13 Aug 2012 21:48:52 +0000 Subject: [PATCH] HADOOP-7703. Improved excpetion handling of shutting down web server. (Devaraj K via Eric Yang) git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-2@1372637 13f79535-47bb-0310-9956-ffa450edef68 --- .../hadoop-common/CHANGES.txt | 3 ++ .../org/apache/hadoop/http/HttpServer.java | 42 +++++++++++++++++-- .../hadoop/http/TestHttpServerLifecycle.java | 36 ++++++++++++---- 3 files changed, 69 insertions(+), 12 deletions(-) diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt index 50c06ffb277..723ebb27c0b 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -213,6 +213,9 @@ Release 2.0.1-alpha - UNRELEASED HADOOP-8660. TestPseudoAuthenticator failing with NPE. (tucu) + HADOOP-7703. Improved excpetion handling of shutting down web server. + (Devaraj K via Eric Yang) + Release 2.0.0-alpha - 05-23-2012 INCOMPATIBLE CHANGES diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/http/HttpServer.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/http/HttpServer.java index a7d2802c02b..c75c4cbacc6 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/http/HttpServer.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/http/HttpServer.java @@ -229,7 +229,7 @@ public class HttpServer implements FilterContainer { webServer.setHandler(contexts); webAppContext = new WebAppContext(); - webAppContext.setDisplayName("WepAppsContext"); + webAppContext.setDisplayName(name); webAppContext.setContextPath("/"); webAppContext.setWar(appDir + "/" + name); webAppContext.getServletContext().setAttribute(CONF_CONTEXT_ATTRIBUTE, conf); @@ -699,8 +699,44 @@ public class HttpServer implements FilterContainer { * stop the server */ public void stop() throws Exception { - listener.close(); - webServer.stop(); + MultiException exception = null; + try { + listener.close(); + } catch (Exception e) { + LOG.error("Error while stopping listener for webapp" + + webAppContext.getDisplayName(), e); + exception = addMultiException(exception, e); + } + + try { + // clear & stop webAppContext attributes to avoid memory leaks. + webAppContext.clearAttributes(); + webAppContext.stop(); + } catch (Exception e) { + LOG.error("Error while stopping web app context for webapp " + + webAppContext.getDisplayName(), e); + exception = addMultiException(exception, e); + } + try { + webServer.stop(); + } catch (Exception e) { + LOG.error("Error while stopping web server for webapp " + + webAppContext.getDisplayName(), e); + exception = addMultiException(exception, e); + } + + if (exception != null) { + exception.ifExceptionThrow(); + } + + } + + private MultiException addMultiException(MultiException exception, Exception e) { + if(exception == null){ + exception = new MultiException(); + } + exception.add(e); + return exception; } public void join() throws InterruptedException { diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/http/TestHttpServerLifecycle.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/http/TestHttpServerLifecycle.java index a205bf8519e..27dd67f39c5 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/http/TestHttpServerLifecycle.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/http/TestHttpServerLifecycle.java @@ -56,16 +56,14 @@ public class TestHttpServerLifecycle extends HttpServerFunctionalTest { * * @throws Throwable on failure */ - @Test public void testStartedServerIsAlive() throws Throwable { + @Test + public void testStartedServerIsAlive() throws Throwable { HttpServer server = null; - try { - server = createTestServer(); - assertNotLive(server); - server.start(); - assertAlive(server); - } finally { - stop(server); - } + server = createTestServer(); + assertNotLive(server); + server.start(); + assertAlive(server); + stop(server); } /** @@ -105,4 +103,24 @@ public class TestHttpServerLifecycle extends HttpServerFunctionalTest { assertNotLive(server); } + /** + * Test that the server is alive once started + * + * @throws Throwable + * on failure + */ + @Test + public void testWepAppContextAfterServerStop() throws Throwable { + HttpServer server = null; + String key = "test.attribute.key"; + String value = "test.attribute.value"; + server = createTestServer(); + assertNotLive(server); + server.start(); + server.setAttribute(key, value); + assertAlive(server); + assertEquals(value, server.getAttribute(key)); + stop(server); + assertNull("Server context should have cleared", server.getAttribute(key)); + } }