karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbono...@apache.org
Subject karaf git commit: [KARAF-4188] Add support for Systemd's watchdog
Date Mon, 22 Aug 2016 15:31:47 GMT
Repository: karaf
Updated Branches:
  refs/heads/karaf-4.0.x 5cca72ffa -> 020d255ca


[KARAF-4188] Add support for Systemd's watchdog


Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/020d255c
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/020d255c
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/020d255c

Branch: refs/heads/karaf-4.0.x
Commit: 020d255ca4dc4da3b94651db45c1a85fffd8a6e9
Parents: 5cca72f
Author: lburgazzoli <lburgazzoli@gmail.com>
Authored: Thu Dec 3 11:09:42 2015 +0100
Committer: Jean-Baptiste Onofré <jbonofre@apache.org>
Committed: Mon Aug 22 16:22:02 2016 +0200

----------------------------------------------------------------------
 assemblies/apache-karaf/pom.xml                 |  3 +
 .../resources/etc/config.properties             |  5 ++
 main/pom.xml                                    |  8 +++
 .../main/java/org/apache/karaf/main/Main.java   | 35 ++++++++++-
 .../org/apache/karaf/main/internal/Systemd.java | 64 ++++++++++++++++++++
 .../karaf/main/internal/SystemdDaemon.java      | 34 +++++++++++
 pom.xml                                         | 14 ++++-
 7 files changed, 161 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/020d255c/assemblies/apache-karaf/pom.xml
----------------------------------------------------------------------
diff --git a/assemblies/apache-karaf/pom.xml b/assemblies/apache-karaf/pom.xml
index 84c54ec..b5fdf07 100644
--- a/assemblies/apache-karaf/pom.xml
+++ b/assemblies/apache-karaf/pom.xml
@@ -205,6 +205,9 @@
 
                         <library>mvn:org.apache.servicemix.specs/org.apache.servicemix.specs.activator/${servicemix.specs.version};type:=default;export:=true</library>
                         <library>mvn:org.apache.servicemix.specs/org.apache.servicemix.specs.locator/${servicemix.specs.version};type:=default;export:=true</library>
+
+                        <library>mvn:net.java.dev.jna/jna/${jna.version};type:=boot;export:=false</library>
+                        <library>mvn:net.java.dev.jna/jna-platform/${jna.version};type:=boot;export:=false</library>
                     </libraries>
                 </configuration>
             </plugin>

http://git-wip-us.apache.org/repos/asf/karaf/blob/020d255c/assemblies/features/base/src/main/filtered-resources/resources/etc/config.properties
----------------------------------------------------------------------
diff --git a/assemblies/features/base/src/main/filtered-resources/resources/etc/config.properties
b/assemblies/features/base/src/main/filtered-resources/resources/etc/config.properties
index 5869113..a4e4b4d 100644
--- a/assemblies/features/base/src/main/filtered-resources/resources/etc/config.properties
+++ b/assemblies/features/base/src/main/filtered-resources/resources/etc/config.properties
@@ -205,3 +205,8 @@ org.ops4j.pax.url.mvn.requireConfigAdminConfig=true
 # Don't delay the console startup. Set to true if you want the console to start after all
other bundles
 #
 karaf.delay.console=false
+
+#
+# Enable native Karaf support for systemd's watchdog.
+#
+# karkaraf.systemd.enabled=false

http://git-wip-us.apache.org/repos/asf/karaf/blob/020d255c/main/pom.xml
----------------------------------------------------------------------
diff --git a/main/pom.xml b/main/pom.xml
index 679c34d..92dd573 100644
--- a/main/pom.xml
+++ b/main/pom.xml
@@ -54,6 +54,14 @@
             <groupId>org.apache.karaf</groupId>
             <artifactId>org.apache.karaf.util</artifactId>
         </dependency>
+        <dependency>
+            <groupId>net.java.dev.jna</groupId>
+            <artifactId>jna</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>net.java.dev.jna</groupId>
+            <artifactId>jna-platform</artifactId>
+        </dependency>
     </dependencies>
 
     <build>

http://git-wip-us.apache.org/repos/asf/karaf/blob/020d255c/main/src/main/java/org/apache/karaf/main/Main.java
----------------------------------------------------------------------
diff --git a/main/src/main/java/org/apache/karaf/main/Main.java b/main/src/main/java/org/apache/karaf/main/Main.java
index 9498082..a760a66 100644
--- a/main/src/main/java/org/apache/karaf/main/Main.java
+++ b/main/src/main/java/org/apache/karaf/main/Main.java
@@ -34,11 +34,13 @@ import java.security.Security;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.StringTokenizer;
+import java.util.concurrent.TimeUnit;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import org.apache.felix.utils.properties.Properties;
 import org.apache.karaf.info.ServerInfo;
+import org.apache.karaf.main.internal.Systemd;
 import org.apache.karaf.main.lock.Lock;
 import org.apache.karaf.main.lock.LockCallBack;
 import org.apache.karaf.main.lock.NoLock;
@@ -323,6 +325,7 @@ public class Main {
         }
         monitor();
         registerSignalHandler();
+        watchdog();
     }
 
     private void registerSignalHandler() {
@@ -404,7 +407,37 @@ public class Main {
     Lock getLock() {
         return lock;
     }
-    
+
+    private void watchdog() {
+        if(Boolean.getBoolean("karaf.systemd.enabled")) {
+            new Thread("Karaf Systemd Watchdog Thread") {
+                public void run() {
+                    try {
+                        doWatchdog();
+                    } catch (Exception e) {
+                        e.printStackTrace();
+                    }
+                }
+            }.start();
+        }
+    }
+
+    private void doWatchdog() throws Exception {
+        Systemd systemd = new Systemd();
+        long timeout = systemd.getWatchdogTimeout(TimeUnit.MILLISECONDS);
+
+        int code;
+        while (!exiting && timeout > 0) {
+            code = systemd.notifyWatchdog();
+            if(code < 0) {
+                System.err.println("Systemd sd_notify failed with error code: " + code);
+                break;
+            }
+
+            Thread.sleep(timeout / 2);
+        }
+    }
+
     private ClassLoader createClassLoader(ArtifactResolver resolver) throws Exception {
         List<URL> urls = new ArrayList<URL>();
         urls.add(resolver.resolve(config.frameworkBundle).toURL());

http://git-wip-us.apache.org/repos/asf/karaf/blob/020d255c/main/src/main/java/org/apache/karaf/main/internal/Systemd.java
----------------------------------------------------------------------
diff --git a/main/src/main/java/org/apache/karaf/main/internal/Systemd.java b/main/src/main/java/org/apache/karaf/main/internal/Systemd.java
new file mode 100644
index 0000000..888bc8b
--- /dev/null
+++ b/main/src/main/java/org/apache/karaf/main/internal/Systemd.java
@@ -0,0 +1,64 @@
+/*
+ * 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.karaf.main.internal;
+
+
+import java.util.concurrent.TimeUnit;
+
+
+public class Systemd {
+    public static final String ENV_WATCHDOG_USEC = "WATCHDOG_USEC";
+    public static final String ENV_MAIN_PID = "SYSTEMD_MAIN_PID";
+
+    private final String mainPid;
+
+    public Systemd() {
+        this.mainPid = System.getProperty("karaf.systemd.main.pid", System.getenv(ENV_MAIN_PID));
+    }
+
+    public int notifyWatchdog() {
+        int rc = -1;
+
+        // WATCHDOG : tells the service manager to update the watchdog timestamp.
+        //            This is the keep-alive ping that services need to issue in
+        //            regular intervals if WatchdogSec= is enabled for it.
+        // MAINPID  : the main process ID (PID) of the service, in case the service
+        //            manager did not fork off the process itself. Example: "MAINPID=4711"
+        //            This does not seem to work reliably so used only if system
+        //            property karaf.systemd.main.pid or env variable SYSTEMD_MAIN_PID
+        //            are set (system property takes the precedence)
+        if(SystemdDaemon.INSTANCE != null) {
+            rc = SystemdDaemon.INSTANCE.sd_notify(
+                0,
+                (mainPid == null) ? "WATCHDOG=1" : ("MAINPID=" + mainPid + "\nWATCHDOG=1"));
+        }
+
+        return rc;
+    }
+
+    public long getWatchdogTimeout(TimeUnit timeUnit) {
+        String timeouts = System.getenv(ENV_WATCHDOG_USEC);
+        if(timeouts != null) {
+            long micros = Long.parseLong(timeouts);
+            return timeUnit.convert(micros, TimeUnit.MICROSECONDS);
+        }
+
+        return -1;
+    }
+}

http://git-wip-us.apache.org/repos/asf/karaf/blob/020d255c/main/src/main/java/org/apache/karaf/main/internal/SystemdDaemon.java
----------------------------------------------------------------------
diff --git a/main/src/main/java/org/apache/karaf/main/internal/SystemdDaemon.java b/main/src/main/java/org/apache/karaf/main/internal/SystemdDaemon.java
new file mode 100644
index 0000000..076cf1c
--- /dev/null
+++ b/main/src/main/java/org/apache/karaf/main/internal/SystemdDaemon.java
@@ -0,0 +1,34 @@
+/*
+ * 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.karaf.main.internal;
+
+import com.sun.jna.Library;
+import com.sun.jna.Native;
+
+
+public interface SystemdDaemon extends Library {
+    SystemdDaemon INSTANCE = (SystemdDaemon) Native.loadLibrary("systemd-daemon", SystemdDaemon.class);
+
+    int sd_notify(int unset_environment, String state);
+    int sd_notifyf(int unset_environment, String format, Object... args);
+
+    // Not available in all systemd version, should replace used of MAINPID
+    // int sd_pid_notify(int pid, int unset_environment, String state);
+    // int sd_pid_notifyff(int pid, int unset_environment, String format, Object... args);
+}

http://git-wip-us.apache.org/repos/asf/karaf/blob/020d255c/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 6cb57d2..7f9002d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -144,7 +144,9 @@
         <asm.springsource.version>1.5.3</asm.springsource.version>
         <cglib.bundle.version>3.2.2_1</cglib.bundle.version>
         <cglib2.version>2.2.0</cglib2.version>
-
+        
+        <jna.version>4.2.2</jna.version>
+        
         <commons-beanutils.version>1.9.2</commons-beanutils.version>
         <commons-codec.version>1.10</commons-codec.version>
         <commons-compress.version>1.11</commons-compress.version>
@@ -1036,6 +1038,16 @@
                 <version>${felix.connect.version}</version>
             </dependency>
 
+            <dependency>
+                <groupId>net.java.dev.jna</groupId>
+                <artifactId>jna</artifactId>
+                <version>${jna.version}</version>
+            </dependency>
+            <dependency>
+                <groupId>net.java.dev.jna</groupId>
+                <artifactId>jna-platform</artifactId>
+                <version>${jna.version}</version>
+            </dependency>
 
             <dependency>
                 <groupId>org.ow2.asm</groupId>


Mime
View raw message