This is an automated email from the ASF dual-hosted git repository.

hxd pushed a commit to branch 4230-2-0.13
in repository https://gitbox.apache.org/repos/asf/iotdb.git

commit 2d125a4ddf8c86cb65aedacefdf9e35f4337fed0
Author: xiangdong huang <[email protected]>
AuthorDate: Sun Aug 28 19:15:18 2022 +0800

    add -p pid, -v version feature
---
 server/src/assembly/resources/sbin/start-server.sh | 95 ++++++++++++++++++++--
 server/src/assembly/resources/sbin/stop-server.bat |  3 +
 server/src/assembly/resources/sbin/stop-server.sh  |  4 +-
 .../org/apache/iotdb/db/conf/IoTDBConstant.java    |  3 +
 .../org/apache/iotdb/db/service/GetVersion.java    | 27 ++++++
 .../java/org/apache/iotdb/db/service/IoTDB.java    | 10 +++
 .../org/apache/iotdb/db/service/DaemonTest.java    | 41 ++++++++++
 7 files changed, 177 insertions(+), 6 deletions(-)

diff --git a/server/src/assembly/resources/sbin/start-server.sh 
b/server/src/assembly/resources/sbin/start-server.sh
index e840f42eb4..18672de81a 100755
--- a/server/src/assembly/resources/sbin/start-server.sh
+++ b/server/src/assembly/resources/sbin/start-server.sh
@@ -41,7 +41,6 @@ elif [ -r "$IOTDB_INCLUDE" ]; then
     . "$IOTDB_INCLUDE"
 fi
 
-
 if [ -z "${IOTDB_HOME}" ]; then
   export IOTDB_HOME="`dirname "$0"`/.."
 fi
@@ -54,6 +53,9 @@ if [ -z "${IOTDB_LOG_DIR}" ]; then
   export IOTDB_LOG_DIR=${IOTDB_HOME}/logs
 fi
 
+if [ -z "${configurationFile}" ]; then
+  IOTDB_LOG_CONFIG="${IOTDB_CONF}/logback.xml"
+fi
 
 is_conf_path=false
 for arg do
@@ -70,10 +72,59 @@ for arg do
   set -- "$@" "$arg"
 done
 
-CONF_PARAMS=$*
+# raw parameters of the command line
+PARAMS=$*
+
+# Parse any command line options.
+args=`getopt vRfhp:bD:H:E: "$@"`
+eval set -- "$args"
+while true; do
+    case "$1" in
+        -p)
+            pidfile="$2"
+            shift 2
+        ;;
+        -f)
+            foreground="yes"
+            shift
+        ;;
+        printgc)
+            PRINT_GC="yes"
+            shift
+        ;;
+        -H)
+            IOTDB_JVM_OPTS="$IOTDB_JVM_OPTS -XX:HeapDumpPath=$2"
+            shift 2
+        ;;
+        -E)
+            IOTDB_JVM_OPTS="$IOTDB_JVM_OPTS -XX:ErrorFile=$2"
+            shift 2
+        ;;
+        -h)
+            echo "Usage: $0 [-v] [-f] [-h] [-p pidfile] [printgc] [-H 
HeapDumpPath] [-E JvmErrorFile]"
+            exit 0
+        ;;
+        -v)
+            SHOW_VERSION="yes"
+            break
+        ;;
+        --)
+            shift
+            if [ "x$*" != "x" ] ; then
+                echo "Error parsing arguments! Unknown argument \"$*\"" >&2
+                exit 1
+            fi
+            break
+        ;;
+        *)
+            echo "Error parsing arguments! Unknown argument \"$1\"" >&2
+            exit 1
+        ;;
+    esac
+done
 
 if [ -f "$IOTDB_CONF/iotdb-env.sh" ]; then
-    if [ "$#" -ge "1" -a "$1" == "printgc" ]; then
+    if [ "x$PRINT_GC" != "x" ]; then
       . "$IOTDB_CONF/iotdb-env.sh" "printgc"
     else
         . "$IOTDB_CONF/iotdb-env.sh"
@@ -86,22 +137,56 @@ CLASSPATH=""
 for f in ${IOTDB_HOME}/lib/*.jar; do
   CLASSPATH=${CLASSPATH}":"$f
 done
+
 classname=org.apache.iotdb.db.service.IoTDB
 
+if [ "x$SHOW_VERSION" != "x" ]; then
+    classname=org.apache.iotdb.db.service.GetVersion
+    IOTDB_LOG_CONFIG="${IOTDB_CONF}/logback-tool.xml"
+fi
+
 launch_service()
 {
        class="$1"
-       iotdb_parms="-Dlogback.configurationFile=${IOTDB_CONF}/logback.xml"
+       iotdb_parms="-Dlogback.configurationFile=${IOTDB_LOG_CONFIG}"
        iotdb_parms="$iotdb_parms -DIOTDB_HOME=${IOTDB_HOME}"
        iotdb_parms="$iotdb_parms -DTSFILE_HOME=${IOTDB_HOME}"
        iotdb_parms="$iotdb_parms -DIOTDB_CONF=${IOTDB_CONF}"
        iotdb_parms="$iotdb_parms -DTSFILE_CONF=${IOTDB_CONF}"
        iotdb_parms="$iotdb_parms -Dname=iotdb\.IoTDB"
-       exec "$JAVA" $illegal_access_params $iotdb_parms $IOTDB_JMX_OPTS -cp 
"$CLASSPATH" "$class" $CONF_PARAMS
+       iotdb_parms="$iotdb_parms -DIOTDB_LOG_DIR=${DIOTDB_LOG_DIR}"
+
+  if [ "x$pidpath" != "x" ]; then
+     iotdb_parms="$iotdb_parms -Diotdb-pidfile=$pidpath"
+  fi
+
+  # The iotdb-foreground option will tell IoTDB not to close stdout/stderr, 
but it's up to us not to background.
+  if [ "x$foreground" != "x" ]; then
+      iotdb_parms="$iotdb_parms -Diotdb-foreground=yes"
+      if [ "x$JVM_ON_OUT_OF_MEMORY_ERROR_OPT" != "x" ]; then
+          exec $NUMACTL "$JAVA" $JVM_OPTS "$JVM_ON_OUT_OF_MEMORY_ERROR_OPT" 
$illegal_access_params $iotdb_parms $IOTDB_JMX_OPTS -cp "$CLASSPATH" 
$IOTDB_JVM_OPTS "$class" $PARAMS
+      else
+          exec $NUMACTL "$JAVA" $JVM_OPTS $illegal_access_params $iotdb_parms 
$IOTDB_JMX_OPTS -cp "$CLASSPATH" $IOTDB_JVM_OPTS "$class" $PARAMS
+      fi
+  # Startup IoTDB, background it, and write the pid.
+  else
+      if [ "x$JVM_ON_OUT_OF_MEMORY_ERROR_OPT" != "x" ]; then
+            exec $NUMACTL "$JAVA" $JVM_OPTS "$JVM_ON_OUT_OF_MEMORY_ERROR_OPT" 
$illegal_access_params $iotdb_parms $IOTDB_JMX_OPTS -cp "$CLASSPATH" 
$IOTDB_JVM_OPTS "$class" $PARAMS 2>&1 > /dev/null  <&- &
+            [ ! -z "$pidfile" ] && printf "%d" $! > "$pidfile"
+            true
+      else
+            exec $NUMACTL "$JAVA" $JVM_OPTS $illegal_access_params 
$iotdb_parms $IOTDB_JMX_OPTS -cp "$CLASSPATH" $IOTDB_JVM_OPTS "$class" $PARAMS 
2>&1 > /dev/null <&- &
+            [ ! -z "$pidfile" ] && printf "%d" $! > "$pidfile"
+            true
+      fi
+  fi
+
        return $?
 }
 
 # Start up the service
 launch_service "$classname"
 
+
+
 exit $?
diff --git a/server/src/assembly/resources/sbin/stop-server.bat 
b/server/src/assembly/resources/sbin/stop-server.bat
index a2f85581fa..4944e7449f 100755
--- a/server/src/assembly/resources/sbin/stop-server.bat
+++ b/server/src/assembly/resources/sbin/stop-server.bat
@@ -27,6 +27,8 @@ for /f  "eol=; tokens=2,2 delims==" %%i in ('findstr /i 
"^rpc_port"
   set rpc_port=%%i
 )
 
+echo "check whether the rpc_port is used..., port is " %rpc_port%
+
 for /f  "eol=; tokens=2,2 delims==" %%i in ('findstr /i "rpc_address"
 %superior_dir%\conf\iotdb-engine.properties') do (
   set rpc_address=%%i
@@ -34,5 +36,6 @@ for /f  "eol=; tokens=2,2 delims==" %%i in ('findstr /i 
"rpc_address"
 
 for /f "tokens=5" %%a in ('netstat /ano ^| findstr %rpc_address%:%rpc_port%') 
do (
   taskkill /f /pid %%a
+  echo "close IoTDB, PID:" %%a
 )
 rem ps ax | grep -i 'iotdb.IoTDB' | grep -v grep | awk '{print $1}' | xargs 
kill -SIGTERM
diff --git a/server/src/assembly/resources/sbin/stop-server.sh 
b/server/src/assembly/resources/sbin/stop-server.sh
index 0cd069a8fb..92f4c4428f 100755
--- a/server/src/assembly/resources/sbin/stop-server.sh
+++ b/server/src/assembly/resources/sbin/stop-server.sh
@@ -22,6 +22,8 @@
 IOTDB_CONF="`dirname "$0"`/../conf"
 rpc_port=`sed '/^rpc_port=/!d;s/.*=//' ${IOTDB_CONF}/iotdb-engine.properties`
 
+echo "check whether the rpc_port is used..., port is" $rpc_port
+
 if  type lsof > /dev/null 2>&1 ; then
   PID=$(lsof -t -i:${rpc_port} -sTCP:LISTEN)
 elif type netstat > /dev/null 2>&1 ; then
@@ -38,5 +40,5 @@ if [ -z "$PID" ]; then
   exit 1
 else
   kill -s TERM $PID
-  echo "close IoTDB"
+  echo "close IoTDB, PID:" $PID
 fi
diff --git a/server/src/main/java/org/apache/iotdb/db/conf/IoTDBConstant.java 
b/server/src/main/java/org/apache/iotdb/db/conf/IoTDBConstant.java
index 337a9e0a6e..7a406386ca 100644
--- a/server/src/main/java/org/apache/iotdb/db/conf/IoTDBConstant.java
+++ b/server/src/main/java/org/apache/iotdb/db/conf/IoTDBConstant.java
@@ -64,6 +64,9 @@ public class IoTDBConstant {
   public static final String LAST_VALUE = "last_value";
   public static final int MIN_SUPPORTED_JDK_VERSION = 8;
 
+  public static final String IOTDB_FOREGROUND = "iotdb-foreground";
+  public static final String IOTDB_PIDFILE = "iotdb-pidfile";
+
   // show info
   public static final String COLUMN_ITEM = "                             item";
   public static final String COLUMN_VALUE = "value";
diff --git a/server/src/main/java/org/apache/iotdb/db/service/GetVersion.java 
b/server/src/main/java/org/apache/iotdb/db/service/GetVersion.java
new file mode 100644
index 0000000000..8b49874460
--- /dev/null
+++ b/server/src/main/java/org/apache/iotdb/db/service/GetVersion.java
@@ -0,0 +1,27 @@
+/*
+ * 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.
+ */
+package org.apache.iotdb.db.service;
+
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+
+public class GetVersion {
+  public static void main(String[] args) {
+    
System.out.println(IoTDBDescriptor.getInstance().getConfig().getIoTDBVersion());
+  }
+}
diff --git a/server/src/main/java/org/apache/iotdb/db/service/IoTDB.java 
b/server/src/main/java/org/apache/iotdb/db/service/IoTDB.java
index 66ffc23ac4..fb10ab2dd3 100644
--- a/server/src/main/java/org/apache/iotdb/db/service/IoTDB.java
+++ b/server/src/main/java/org/apache/iotdb/db/service/IoTDB.java
@@ -51,6 +51,7 @@ import 
org.apache.iotdb.db.writelog.manager.MultiFileLogNodeManager;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.io.File;
 import java.io.IOException;
 
 import static org.apache.iotdb.db.utils.JarLoaderUtil.loadExternLib;
@@ -133,9 +134,18 @@ public class IoTDB implements IoTDBMBean {
     config.setEnablePartialInsert(prevIsEnablePartialInsert);
     activated = true;
 
+    processPid();
+
     logger.info("{} has started.", IoTDBConstant.GLOBAL_DB_NAME);
   }
 
+  void processPid() {
+    String pidFile = System.getProperty(IoTDBConstant.IOTDB_PIDFILE);
+    if (pidFile != null) {
+      new File(pidFile).deleteOnExit();
+    }
+  }
+
   private void setUp() throws StartupException, QueryProcessException {
     logger.info("Setting up IoTDB...");
 
diff --git a/server/src/test/java/org/apache/iotdb/db/service/DaemonTest.java 
b/server/src/test/java/org/apache/iotdb/db/service/DaemonTest.java
new file mode 100644
index 0000000000..5ea7c01104
--- /dev/null
+++ b/server/src/test/java/org/apache/iotdb/db/service/DaemonTest.java
@@ -0,0 +1,41 @@
+/*
+ * 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.
+ */
+package org.apache.iotdb.db.service;
+
+import org.apache.iotdb.db.conf.IoTDBConstant;
+
+import org.junit.Test;
+
+public class DaemonTest {
+
+  @Test
+  public void testPid() {
+    IoTDB ioTDB = IoTDB.getInstance();
+    // no pid set, so there is nothing happens
+    ioTDB.processPid();
+  }
+
+  @Test
+  public void testSetPid() {
+    IoTDB ioTDB = IoTDB.getInstance();
+    System.setProperty(IoTDBConstant.IOTDB_PIDFILE, "./iotdb.pid");
+    // no pid set, so there is nothing happens
+    ioTDB.processPid();
+  }
+}

Reply via email to