You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lens.apache.org by sr...@apache.org on 2015/01/01 15:19:20 UTC

incubator-lens git commit: LENS-163 Fix Lens Server & Stop related issues. Contributed by Srikanth Sundarrajan

Repository: incubator-lens
Updated Branches:
  refs/heads/master 5e6d8623a -> 92c5ba344


LENS-163 Fix Lens Server & Stop related issues. Contributed by Srikanth Sundarrajan


Project: http://git-wip-us.apache.org/repos/asf/incubator-lens/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-lens/commit/92c5ba34
Tree: http://git-wip-us.apache.org/repos/asf/incubator-lens/tree/92c5ba34
Diff: http://git-wip-us.apache.org/repos/asf/incubator-lens/diff/92c5ba34

Branch: refs/heads/master
Commit: 92c5ba344b1278b1cc91a459892fbc8769ed16cd
Parents: 5e6d862
Author: srikanth.sundarrajan <sr...@inmobi.com>
Authored: Thu Jan 1 19:09:33 2015 +0530
Committer: srikanth.sundarrajan <sr...@inmobi.com>
Committed: Thu Jan 1 19:09:33 2015 +0530

----------------------------------------------------------------------
 lens-server/pom.xml                             |  25 +++-
 .../java/org/apache/lens/server/LensServer.java | 132 ++++++++++++++-----
 .../org/apache/lens/server/LensServices.java    |  21 ++-
 .../main/resources/lens-build-info.properties   |  25 ++++
 pom.xml                                         |   6 +
 5 files changed, 173 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/92c5ba34/lens-server/pom.xml
----------------------------------------------------------------------
diff --git a/lens-server/pom.xml b/lens-server/pom.xml
index 55d5d58..cd4ccaf 100644
--- a/lens-server/pom.xml
+++ b/lens-server/pom.xml
@@ -209,6 +209,12 @@
   </dependencies>
 
   <build>
+    <resources>
+      <resource>
+        <directory>src/main/resources</directory>
+        <filtering>true</filtering>
+      </resource>
+    </resources>
     <plugins>
       <plugin>
         <artifactId>maven-war-plugin</artifactId>
@@ -229,6 +235,23 @@
         </executions>
       </plugin>
       <plugin>
+        <groupId>org.codehaus.mojo</groupId>
+        <artifactId>buildnumber-maven-plugin</artifactId>
+        <executions>
+          <execution>
+            <phase>validate</phase>
+            <goals>
+              <goal>create</goal>
+            </goals>
+          </execution>
+        </executions>
+        <configuration>
+          <revisionOnScmFailure>release</revisionOnScmFailure>
+          <doCheck>false</doCheck>
+          <doUpdate>false</doUpdate>
+        </configuration>
+      </plugin>
+      <plugin>
         <groupId>org.mortbay.jetty</groupId>
         <artifactId>maven-jetty-plugin</artifactId>
       </plugin>
@@ -286,4 +309,4 @@
     </plugins>
   </build>
 
-</project>
\ No newline at end of file
+</project>

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/92c5ba34/lens-server/src/main/java/org/apache/lens/server/LensServer.java
----------------------------------------------------------------------
diff --git a/lens-server/src/main/java/org/apache/lens/server/LensServer.java b/lens-server/src/main/java/org/apache/lens/server/LensServer.java
index 66abbcd..d59e93b 100644
--- a/lens-server/src/main/java/org/apache/lens/server/LensServer.java
+++ b/lens-server/src/main/java/org/apache/lens/server/LensServer.java
@@ -31,11 +31,12 @@ import org.glassfish.jersey.filter.LoggingFilter;
 import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
 import org.glassfish.jersey.server.ResourceConfig;
 import org.slf4j.bridge.SLF4JBridgeHandler;
-import sun.misc.Signal;
-import sun.misc.SignalHandler;
 
 import javax.ws.rs.core.UriBuilder;
 import java.io.IOException;
+import java.io.InputStream;
+import java.util.Map;
+import java.util.Properties;
 import java.util.logging.Logger;
 
 /**
@@ -46,6 +47,9 @@ public class LensServer {
   /** The Constant LOG. */
   public static final Log LOG = LogFactory.getLog(LensServer.class);
 
+  private static final String SEP_LINE =
+    "\n###############################################################\n";
+
   /** The server. */
   final HttpServer server;
 
@@ -55,6 +59,11 @@ public class LensServer {
   /** The conf. */
   final HiveConf conf;
 
+  /** This flag indicates that the lens server can run,
+   * When this is set to false, main thread bails out.
+   */
+  volatile boolean canRun = true;
+
   static {
     SLF4JBridgeHandler.removeHandlersForRootLogger();
     SLF4JBridgeHandler.install();
@@ -120,7 +129,7 @@ public class LensServer {
    * @throws IOException
    *           Signals that an I/O exception has occurred.
    */
-  public void start() throws IOException {
+  public synchronized void start() throws IOException {
     server.start();
     uiServer.start();
   }
@@ -128,14 +137,31 @@ public class LensServer {
   /**
    * Stop.
    */
-  public void stop() {
+  public synchronized void stop() {
     server.shutdownNow();
     uiServer.shutdownNow();
     LensServices.get().stop();
+    printShutdownMessage();
   }
 
-  /** The this server. */
-  private static LensServer thisServer;
+  /**
+   * This keeps the server running till a shutdown is
+   * triggered. Either through a shutdown sequence initiated
+   * by an administrator or if applications encounters a
+   * fatal exception or it enters an unrecoverable state.
+   */
+  private void join() {
+    while (canRun) {
+      synchronized (this) {
+        try {
+          wait(2000);
+        } catch (InterruptedException e) {
+          LOG.warn("Received an interrupt in the main loop", e);
+        }
+      }
+    }
+    LOG.info("Exiting main run loop...");
+  }
 
   /**
    * The main method.
@@ -145,27 +171,17 @@ public class LensServer {
    * @throws Exception
    *           the exception
    */
-  @SuppressWarnings("restriction")
   public static void main(String[] args) throws Exception {
-    Signal.handle(new Signal("TERM"), new SignalHandler() {
-
-      @Override
-      public void handle(Signal signal) {
-        try {
-          LOG.info("Request for stopping lens server received");
-          if (thisServer != null) {
-            synchronized (thisServer) {
-              thisServer.notify();
-            }
-          }
-        } catch (Exception e) {
-          LOG.warn("Error in shutting down databus", e);
-        }
-      }
-    });
 
+    printStartupMessage();
     try {
-      thisServer = new LensServer(LensServerConf.get());
+      final LensServer thisServer = new LensServer(LensServerConf.get());
+
+      registerShutdownHook(thisServer);
+      registerDefaultExceptionHandler();
+
+      thisServer.start();
+      thisServer.join();
     } catch (Exception exc) {
       LOG.fatal("Error while creating Lens server", exc);
       try {
@@ -173,14 +189,70 @@ public class LensServer {
       } catch (Exception e) {
         LOG.error("Error stopping services", e);
       }
-      System.exit(1);
     }
+  }
 
-    thisServer.start();
-    synchronized (thisServer) {
-      thisServer.wait();
+  /**
+   * Print message from lens-build-info file during startup.
+   */
+  private static void printStartupMessage() {
+    StringBuilder buffer = new StringBuilder();
+    buffer.append(SEP_LINE);
+    buffer.append("                    Lens Server (STARTUP)");
+    Properties buildProperties = new Properties();
+    InputStream buildPropertiesResource = LensServer.class.
+      getResourceAsStream("/lens-build-info.properties");
+    if (buildPropertiesResource != null) {
+      try {
+        buildProperties.load(buildPropertiesResource);
+        for (Map.Entry entry : buildProperties.entrySet()) {
+              buffer.append('\n').append('\t').append(entry.getKey()).
+                      append(":\t").append(entry.getValue());
+          }
+      } catch (Throwable e) {
+          buffer.append("*** Unable to get build info ***");
+      }
+    } else {
+      buffer.append("*** Unable to get build info ***");
     }
-    thisServer.stop();
-    System.exit(0);
+    buffer.append(SEP_LINE);
+    LOG.info(buffer.toString());
+  }
+
+  /**
+   * Print message before the lens server stops.
+   */
+  private static void printShutdownMessage() {
+    StringBuilder buffer = new StringBuilder();
+    buffer.append(SEP_LINE);
+    buffer.append("                    Lens Server (SHUTDOWN)");
+    buffer.append(SEP_LINE);
+    LOG.info(buffer.toString());
+  }
+
+  /** Registering a shutdown hook to listen to SIGTERM events.
+   * Upon receiving a SIGTERM, notify the server, which is put
+   * on wait state.
+   */
+  private static void registerShutdownHook(final LensServer thisServer) {
+    Runtime.getRuntime().addShutdownHook(new Thread() {
+      @Override
+      public void run() {
+        Thread.currentThread().setName("Shutdown");
+        LOG.info("Server has been requested to be stopped.");
+        thisServer.canRun = false;
+        thisServer.stop();
+      }
+    });
+  }
+
+  /** Registering a default uncaught exception handler. */
+  private static void registerDefaultExceptionHandler() {
+    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
+      @Override
+      public void uncaughtException(Thread t, Throwable e) {
+        LOG.fatal("Uncaught exception in Thread " + t, e);
+      }
+    });
   }
 }

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/92c5ba34/lens-server/src/main/java/org/apache/lens/server/LensServices.java
----------------------------------------------------------------------
diff --git a/lens-server/src/main/java/org/apache/lens/server/LensServices.java b/lens-server/src/main/java/org/apache/lens/server/LensServices.java
index fe2fc75..3b0e948 100644
--- a/lens-server/src/main/java/org/apache/lens/server/LensServices.java
+++ b/lens-server/src/main/java/org/apache/lens/server/LensServices.java
@@ -31,6 +31,7 @@ import lombok.Setter;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.Path;
 import org.apache.hadoop.hive.conf.HiveConf;
@@ -56,6 +57,9 @@ public class LensServices extends CompositeService implements ServiceProvider {
   /** The Constant LENS_SERVICES_NAME. */
   public static final String LENS_SERVICES_NAME = "lens_services";
 
+  /** Constant for FileSystem auto close on shutdown config */
+  private static final String FS_AUTOMATIC_CLOSE = "fs.automatic.close";
+
   /** The instance. */
   private static LensServices INSTANCE = new LensServices(LENS_SERVICES_NAME);
 
@@ -74,6 +78,9 @@ public class LensServices extends CompositeService implements ServiceProvider {
   /** The persist dir. */
   private Path persistDir;
 
+  /** The persistence file system. */
+  private FileSystem persistenceFS;
+
   /** The stopping. */
   private boolean stopping = false;
 
@@ -190,6 +197,9 @@ public class LensServices extends CompositeService implements ServiceProvider {
           LensConfConstants.DEFAULT_SERVER_STATE_PERSIST_LOCATION);
       persistDir = new Path(persistPathStr);
       try {
+        Configuration configuration = new Configuration(conf);
+        configuration.setBoolean(FS_AUTOMATIC_CLOSE, false);
+        persistenceFS = FileSystem.newInstance(persistDir.toUri(), conf);
         setupPersistedState();
       } catch (Exception e) {
         LOG.error("Could not recover from persisted state", e);
@@ -236,13 +246,12 @@ public class LensServices extends CompositeService implements ServiceProvider {
   private void setupPersistedState() throws IOException, ClassNotFoundException {
     if (conf.getBoolean(LensConfConstants.SERVER_RECOVER_ON_RESTART,
         LensConfConstants.DEFAULT_SERVER_RECOVER_ON_RESTART)) {
-      FileSystem fs = persistDir.getFileSystem(conf);
 
       for (LensService service : lensServices) {
         ObjectInputStream in = null;
         try {
           try {
-            in = new ObjectInputStream(fs.open(getServicePersistPath(service)));
+            in = new ObjectInputStream(persistenceFS.open(getServicePersistPath(service)));
           } catch (FileNotFoundException fe) {
             LOG.warn("No persist path available for service:" + service.getName());
             continue;
@@ -267,7 +276,6 @@ public class LensServices extends CompositeService implements ServiceProvider {
   private synchronized void persistLensServiceState() throws IOException {
     if (conf.getBoolean(LensConfConstants.SERVER_RESTART_ENABLED, LensConfConstants.DEFAULT_SERVER_RESTART_ENABLED)) {
       if (persistDir != null) {
-        FileSystem fs = persistDir.getFileSystem(conf);
         LOG.info("Persisting server state in " + persistDir);
 
         for (LensService service : lensServices) {
@@ -275,7 +283,7 @@ public class LensServices extends CompositeService implements ServiceProvider {
           Path serviceWritePath = new Path(persistDir, service.getName() + ".out");
           ObjectOutputStream out = null;
           try {
-            out = new ObjectOutputStream(fs.create(serviceWritePath));
+            out = new ObjectOutputStream(persistenceFS.create(serviceWritePath));
             service.writeExternal(out);
           } finally {
             if (out != null) {
@@ -283,7 +291,7 @@ public class LensServices extends CompositeService implements ServiceProvider {
             }
           }
           Path servicePath = getServicePersistPath(service);
-          fs.rename(serviceWritePath, servicePath);
+          persistenceFS.rename(serviceWritePath, servicePath);
           LOG.info("Persisted service " + service.getName() + " to " + servicePath);
         }
       }
@@ -323,6 +331,9 @@ public class LensServices extends CompositeService implements ServiceProvider {
       try {
         // persist all the services
         persistLensServiceState();
+
+        persistenceFS.close();
+        LOG.info("Persistence File system object close complete");
       } catch (IOException e) {
         LOG.error("Could not persist server state", e);
         throw new IllegalStateException(e);

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/92c5ba34/lens-server/src/main/resources/lens-build-info.properties
----------------------------------------------------------------------
diff --git a/lens-server/src/main/resources/lens-build-info.properties b/lens-server/src/main/resources/lens-build-info.properties
new file mode 100644
index 0000000..a6af0d7
--- /dev/null
+++ b/lens-server/src/main/resources/lens-build-info.properties
@@ -0,0 +1,25 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+######################
+build.user=${user.name}
+build.epoch=${timestamp}
+project.version=${pom.version}
+build.version=${pom.version}-r${buildNumber}
+vc.source.url=${scm.connection}
+######################

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/92c5ba34/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 2fb1005..079971f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -98,6 +98,7 @@
     <checkstyle.plugin.version>2.9.1</checkstyle.plugin.version>
     <war.plugin.version>2.1.1</war.plugin.version>
     <license.plugin.version>2.6</license.plugin.version>
+    <buildnumber.plugin.version>1.0</buildnumber.plugin.version>
   </properties>
 
   <groupId>org.apache.lens</groupId>
@@ -243,6 +244,11 @@
           </configuration>
         </plugin>
         <plugin>
+          <groupId>org.codehaus.mojo</groupId>
+          <artifactId>buildnumber-maven-plugin</artifactId>
+          <version>${buildnumber.plugin.version}</version>
+        </plugin>
+        <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-compiler-plugin</artifactId>
           <configuration>