airavata-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From samin...@apache.org
Subject [3/4] git commit: updating server force stop to have kill commands at unix level
Date Wed, 12 Mar 2014 14:24:50 GMT
updating server force stop to have kill commands at unix level


Project: http://git-wip-us.apache.org/repos/asf/airavata/repo
Commit: http://git-wip-us.apache.org/repos/asf/airavata/commit/9b4dedf0
Tree: http://git-wip-us.apache.org/repos/asf/airavata/tree/9b4dedf0
Diff: http://git-wip-us.apache.org/repos/asf/airavata/diff/9b4dedf0

Branch: refs/heads/master
Commit: 9b4dedf0307687eef408f557c27bda56d76eb021
Parents: f9b95c9
Author: Saminda Wijeratne <samindaw@gmail.com>
Authored: Wed Mar 12 10:24:24 2014 -0400
Committer: Saminda Wijeratne <samindaw@gmail.com>
Committed: Wed Mar 12 10:24:24 2014 -0400

----------------------------------------------------------------------
 .../src/main/resources/bin/airavata-server.sh   | 63 ++++++++++----
 .../org/apache/airavata/server/ServerMain.java  | 88 +++++++++-----------
 2 files changed, 85 insertions(+), 66 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/airavata/blob/9b4dedf0/modules/distribution/airavata-server/src/main/resources/bin/airavata-server.sh
----------------------------------------------------------------------
diff --git a/modules/distribution/airavata-server/src/main/resources/bin/airavata-server.sh
b/modules/distribution/airavata-server/src/main/resources/bin/airavata-server.sh
index 9247d68..e7ed1be 100755
--- a/modules/distribution/airavata-server/src/main/resources/bin/airavata-server.sh
+++ b/modules/distribution/airavata-server/src/main/resources/bin/airavata-server.sh
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/bin/bash
 
 # Licensed to the Apache Software Foundation (ASF) under one
 # or more contributor license agreements. See the NOTICE file
@@ -25,6 +25,8 @@ JAVA_OPTS=""
 AIRAVATA_COMMAND=""
 IS_DAEMON_MODE=false
 LOGO=true
+STOP=false
+FORCE=false
 for var in "$@"
 do
     case $var in
@@ -42,9 +44,14 @@ do
         ;;
 	stop)
 	    LOGO=false
+	    STOP=true
 	    AIRAVATA_COMMAND="$AIRAVATA_COMMAND $var"
             shift
         ;;
+	--force)
+	    FORCE=true
+            shift
+        ;;
 	-nologo)
 	    LOGO=false
             shift
@@ -52,13 +59,13 @@ do
         -h)
             echo "Usage: airavata-server.sh [command-options]"
             echo "command options:"
-	    echo "  start                            Start server in daemon mode"
-	    echo "  stop [--serverIndex n] [--force] Stop all airavata servers. Specify serverIndex
stop a particular instance"
-	    echo "  --<key>[=<value>]         	     Server setting(s) to override or
introduce (overrides values in airavata-server.properties)"
-            echo "  -nologo                 	     Do not show airavata logo"
-            echo "  -xdebug                 	     Start Airavata Server under JPDA debugger"
-            echo "  -security               	     Enable Java 2 security"
-            echo "  -h                               Display this help and exit"
+	    echo "  start              Start server in daemon mode"
+	    echo "  stop [--force]     Stop all airavata servers."
+	    echo "  --<key>[=<value>]  Server setting(s) to override or introduce (overrides
values in airavata-server.properties)"
+            echo "  -nologo            Do not show airavata logo"
+            echo "  -xdebug            Start Airavata Server under JPDA debugger"
+            echo "  -security          Enable Java 2 security"
+            echo "  -h                 Display this help and exit"
             shift
             exit 0
         ;;
@@ -73,16 +80,36 @@ if $LOGO ; then
 		cat $LOGO_FILE
 	fi
 fi
-if $IS_DAEMON_MODE ; then
-	echo "Starting airavata server in daemon mode..."
-	nohup java $JAVA_OPTS -classpath "$XBAYA_CLASSPATH" \
-	    -Djava.endorsed.dirs="$AIRAVATA_HOME/lib/endorsed":"$JAVA_HOME/jre/lib/endorsed":"$JAVA_HOME/lib/endorsed"
\
-	    org.apache.airavata.server.ServerMain $AIRAVATA_COMMAND $* > airavata-server.out
& 
-	echo
-	echo
+if $STOP && $FORCE ; 
+then
+	for f in `find . -name "server-start_*"`; do 
+		f_split=(${f//_/ });
+		echo "Found process file : $f" 
+		echo -n "    Sending kill signals to process ${f_split[1]}..."
+		out=`kill -9 ${f_split[1]} 2>&1`
+		if [ -z "$out" ]; then
+		    echo "done"
+		else
+		    echo "failed (REASON: $out)"
+		fi
+		echo -n "    Removing process file..."
+		out=`rm $f 2>&1`
+		if [ -z "$out" ]; then
+		    echo "done"
+		else
+		    echo "failed (REASON: $out)"
+		fi
+	done
 else
-	java $JAVA_OPTS -classpath "$XBAYA_CLASSPATH" \
-	    -Djava.endorsed.dirs="$AIRAVATA_HOME/lib/endorsed":"$JAVA_HOME/jre/lib/endorsed":"$JAVA_HOME/lib/endorsed"
\
-	    org.apache.airavata.server.ServerMain $AIRAVATA_COMMAND $*
+	if $IS_DAEMON_MODE ; then
+		echo "Starting airavata server in daemon mode..."
+		nohup java $JAVA_OPTS -classpath "$XBAYA_CLASSPATH" \
+		    -Djava.endorsed.dirs="$AIRAVATA_HOME/lib/endorsed":"$JAVA_HOME/jre/lib/endorsed":"$JAVA_HOME/lib/endorsed"
\
+		    org.apache.airavata.server.ServerMain $AIRAVATA_COMMAND $* > airavata-server.out
& 
+ 	else
+		java $JAVA_OPTS -classpath "$XBAYA_CLASSPATH" \
+		    -Djava.endorsed.dirs="$AIRAVATA_HOME/lib/endorsed":"$JAVA_HOME/jre/lib/endorsed":"$JAVA_HOME/lib/endorsed"
\
+		    org.apache.airavata.server.ServerMain $AIRAVATA_COMMAND $*
+	fi
 fi
 

http://git-wip-us.apache.org/repos/asf/airavata/blob/9b4dedf0/modules/server/src/main/java/org/apache/airavata/server/ServerMain.java
----------------------------------------------------------------------
diff --git a/modules/server/src/main/java/org/apache/airavata/server/ServerMain.java b/modules/server/src/main/java/org/apache/airavata/server/ServerMain.java
index 20534fb..8a99013 100644
--- a/modules/server/src/main/java/org/apache/airavata/server/ServerMain.java
+++ b/modules/server/src/main/java/org/apache/airavata/server/ServerMain.java
@@ -22,6 +22,7 @@ package org.apache.airavata.server;
 
 import java.io.File;
 import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.RandomAccessFile;
 import java.util.ArrayList;
@@ -44,10 +45,8 @@ public class ServerMain {
     private final static Logger logger = LoggerFactory.getLogger(ServerMain.class);
     private static boolean serversLoaded=false;
 	private static final String stopFileNamePrefix = "airavata-server-stop";
-	private static final String stopFileNamePrefixForced = "airavata-server-stop-forced";
-	private static int serverIndex=-1;
-	private static final String serverStartedFileNamePrefix = "airavata-server-start";
-	private static boolean forcedStop=false; 
+	private static int serverPID=-1;
+	private static final String serverStartedFileNamePrefix = "server-start";
 	private static boolean systemShutDown=false;
     static{
 		servers = new ArrayList<IServer>();
@@ -100,7 +99,7 @@ public class ServerMain {
 
 	private static void performServerStart(String[] args) {
 		setServerStarted();
-		logger.info("Airavata server instance "+serverIndex+" starting...");
+		logger.info("Airavata server instance starting...");
 		ServerSettings.mergeSettingsCommandLineArgs(args);
 		startAllServers();
 		while(!hasStopRequested()){
@@ -136,14 +135,10 @@ public class ServerMain {
 		deleteOldStartRecords();
 		String serverIndexOption = "serverIndex";
 		if (commandLineParameters.getParameters().containsKey(serverIndexOption)){
-			serverIndex=Integer.parseInt(commandLineParameters.getParameters().get(serverIndexOption));
-		}
-		String serverForcedStop = "force";
-		if (commandLineParameters.getParameters().containsKey(serverForcedStop)){
-			forcedStop=true;
+			serverPID=Integer.parseInt(commandLineParameters.getParameters().get(serverIndexOption));
 		}
 		if (isServerRunning()) {
-			logger.info("Requesting airavata server"+(serverIndex==-1? "(s)":" instance "+serverIndex)+"
to stop...");
+			logger.info("Requesting airavata server"+(serverPID==-1? "(s)":" instance "+serverPID)+"
to stop...");
 			requestStop();
 			while(isServerRunning()){
 				try {
@@ -152,9 +147,9 @@ public class ServerMain {
 					e.printStackTrace();
 				}
 			}
-			logger.info("Server"+(serverIndex==-1? "(s)":" instance "+serverIndex)+" stopped!!!");
+			logger.info("Server"+(serverPID==-1? "(s)":" instance "+serverPID)+" stopped!!!");
 		}else{
-			logger.error("Server"+(serverIndex==-1? "":" instance "+serverIndex)+" is not running!!!");
+			logger.error("Server"+(serverPID==-1? "":" instance "+serverPID)+" is not running!!!");
 		}
 	}
 
@@ -164,38 +159,21 @@ public class ServerMain {
 		file.createNewFile();
 		new RandomAccessFile(file, "rw").getChannel().lock();
 		file.deleteOnExit();
-		if (forcedStop){ 
-			// incase a previous attempt of stopping without forcing is present, best to delete that
file
-			File f=new File((serverIndex==-1)? stopFileNamePrefix:stopFileNamePrefix+serverIndex);
-			if (f.exists()){
-				f.deleteOnExit();
-			}
-		}
 	}
 	
 	private static boolean hasStopRequested(){
-		forcedStop=new File(getServerStopForceFileName()).exists() || new File(stopFileNamePrefixForced).exists();
-		return  isSystemShutDown() || forcedStop || new File(getServerStopFileName()).exists()
|| new File(stopFileNamePrefix).exists(); 
+		return  isSystemShutDown() || new File(getServerStopFileName()).exists() || new File(stopFileNamePrefix).exists();

 	}
 
 	private static String getServerStopFileName() {
-		String filePrefix = forcedStop? stopFileNamePrefixForced : stopFileNamePrefix;
-		return (serverIndex==-1)? filePrefix:filePrefix+serverIndex;
+		return stopFileNamePrefix;
 	}
 	
-	private static String getServerStopForceFileName() {
-		return (serverIndex==-1)?stopFileNamePrefixForced:stopFileNamePrefixForced+serverIndex;
-	}
-
 	private static void deleteOldStopRequests(){
 		File[] files = new File(".").listFiles();
 		for (File file : files) {
-			if (file.getName().contains(stopFileNamePrefix) || file.getName().contains(stopFileNamePrefixForced)){
-				try {
-					file.delete();
-				} catch (Exception e) {
-					//file is locked which means there's an active process using it
-				}
+			if (file.getName().contains(stopFileNamePrefix)){
+				file.delete();
 			}
 		}
 	}
@@ -205,6 +183,7 @@ public class ServerMain {
 		for (File file : files) {
 			if (file.getName().contains(serverStartedFileNamePrefix)){
 				try {
+					new FileOutputStream(file);
 					file.delete();
 				} catch (Exception e) {
 					//file is locked which means there's an active process using it
@@ -214,7 +193,7 @@ public class ServerMain {
 	}
 	
 	private static boolean isServerRunning(){
-		if (serverIndex==-1){
+		if (serverPID==-1){
 			String[] files = new File(".").list();
 			for (String file : files) {
 				if (file.contains(serverStartedFileNamePrefix)){
@@ -230,12 +209,10 @@ public class ServerMain {
 	@SuppressWarnings({ "resource" })
 	private static void setServerStarted(){
 		try {
+			serverPID = getPID();
 			deleteOldStopRequests();
 			File serverStartedFile = null;
-			while(serverStartedFile==null || serverStartedFile.exists()){
-				serverIndex++;
-				serverStartedFile = new File(getServerStartedFileName());
-			}
+			serverStartedFile = new File(getServerStartedFileName());
 			serverStartedFile.createNewFile();
 			serverStartedFile.deleteOnExit();
 			new RandomAccessFile(serverStartedFile,"rw").getChannel().lock();
@@ -247,20 +224,15 @@ public class ServerMain {
 	}
 
 	private static String getServerStartedFileName() {
-		return serverStartedFileNamePrefix+serverIndex;
+		return serverStartedFileNamePrefix+"_"+Integer.toString(serverPID);
 	}
-	
-	private static int DEFAULT_FORCE_STOP_WAIT_INTERVAL=3000;
+
 	public static void stopAllServers() {
 		//stopping should be done in reverse order of starting the servers
 		for(int i=servers.size()-1;i>=0;i--){
 			try {
 				servers.get(i).stop();
-				if (forcedStop) {
-					waitForServerToStop(servers.get(i),DEFAULT_FORCE_STOP_WAIT_INTERVAL);
-				}else{
-					waitForServerToStop(servers.get(i),null);
-				}
+				waitForServerToStop(servers.get(i),null);
 			} catch (Exception e) {
 				logger.error("Server Stop Error:",e);
 			}
@@ -302,7 +274,7 @@ public class ServerMain {
 			logger.info("Waiting for " + server.getName() + " to stop...");
 		}
 		//we are doing hasStopRequested() check because while we are stuck in the loop to stop
there could be a forceStop request 
-		while(server.getStatus()==ServerStatus.STOPING && (maxWait==null || count<maxWait)
&& hasStopRequested() && (maxWait!=null || !forcedStop)){
+		while(server.getStatus()==ServerStatus.STOPING && (maxWait==null || count<maxWait)){
 			Thread.sleep(SERVER_STATUS_CHANGE_WAIT_INTERVAL);
 			count+=SERVER_STATUS_CHANGE_WAIT_INTERVAL;
 		}
@@ -318,4 +290,24 @@ public class ServerMain {
 	private static void setSystemShutDown(boolean systemShutDown) {
 		ServerMain.systemShutDown = systemShutDown;
 	}
+	
+	private static int getPID(){
+		try {
+			java.lang.management.RuntimeMXBean runtime = java.lang.management.ManagementFactory
+					.getRuntimeMXBean();
+			java.lang.reflect.Field jvm = runtime.getClass()
+					.getDeclaredField("jvm");
+			jvm.setAccessible(true);
+			sun.management.VMManagement mgmt = (sun.management.VMManagement) jvm
+					.get(runtime);
+			java.lang.reflect.Method pid_method = mgmt.getClass()
+					.getDeclaredMethod("getProcessId");
+			pid_method.setAccessible(true);
+
+			int pid = (Integer) pid_method.invoke(mgmt);
+			return pid;
+		} catch (Exception e) {
+			return -1;
+		}
+	}
 }
\ No newline at end of file


Mime
View raw message