Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 6194A200B6A for ; Mon, 22 Aug 2016 16:18:19 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 60649160AB3; Mon, 22 Aug 2016 14:18:19 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 5CDFD160A91 for ; Mon, 22 Aug 2016 16:18:18 +0200 (CEST) Received: (qmail 30116 invoked by uid 500); 22 Aug 2016 14:18:17 -0000 Mailing-List: contact commits-help@karaf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@karaf.apache.org Delivered-To: mailing list commits@karaf.apache.org Received: (qmail 30103 invoked by uid 99); 22 Aug 2016 14:18:17 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 22 Aug 2016 14:18:17 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 78822E00D6; Mon, 22 Aug 2016 14:18:17 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: jbonofre@apache.org To: commits@karaf.apache.org Date: Mon, 22 Aug 2016 14:18:17 -0000 Message-Id: <7d9fa2b6586a46dab817c9743db2369a@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [1/2] karaf git commit: [KARAF-4188] Add support for Systemd's watchdog archived-at: Mon, 22 Aug 2016 14:18:19 -0000 Repository: karaf Updated Branches: refs/heads/master fa09cdef1 -> 03d465b58 [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/12fdb85f Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/12fdb85f Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/12fdb85f Branch: refs/heads/master Commit: 12fdb85f3afb34b9cbd3207aa405a3036ce27913 Parents: fa09cde Author: lburgazzoli Authored: Thu Dec 3 11:09:42 2015 +0100 Committer: Jean-Baptiste Onofré Committed: Mon Aug 22 15:43:17 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/12fdb85f/assemblies/apache-karaf/pom.xml ---------------------------------------------------------------------- diff --git a/assemblies/apache-karaf/pom.xml b/assemblies/apache-karaf/pom.xml index a07d58b..596303c 100644 --- a/assemblies/apache-karaf/pom.xml +++ b/assemblies/apache-karaf/pom.xml @@ -203,6 +203,9 @@ mvn:org.apache.servicemix.specs/org.apache.servicemix.specs.activator/${servicemix.specs.version};type:=default;export:=true mvn:org.apache.servicemix.specs/org.apache.servicemix.specs.locator/${servicemix.specs.version};type:=default;export:=true + + mvn:net.java.dev.jna/jna/${jna.version};type:=boot;export:=false + mvn:net.java.dev.jna/jna-platform/${jna.version};type:=boot;export:=false 1.8 http://git-wip-us.apache.org/repos/asf/karaf/blob/12fdb85f/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 e72b694..8970e0c 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 @@ -207,3 +207,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/12fdb85f/main/pom.xml ---------------------------------------------------------------------- diff --git a/main/pom.xml b/main/pom.xml index c204582..86ef05d 100644 --- a/main/pom.xml +++ b/main/pom.xml @@ -54,6 +54,14 @@ org.apache.karaf org.apache.karaf.util + + net.java.dev.jna + jna + + + net.java.dev.jna + jna-platform + http://git-wip-us.apache.org/repos/asf/karaf/blob/12fdb85f/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 c370b30..31cd758 100644 --- a/main/src/main/java/org/apache/karaf/main/Main.java +++ b/main/src/main/java/org/apache/karaf/main/Main.java @@ -35,11 +35,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; @@ -283,6 +285,7 @@ public class Main { } monitor(); registerSignalHandler(); + watchdog(); } /* @@ -406,7 +409,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 urls = new ArrayList(); urls.add(resolver.resolve(config.frameworkBundle).toURL()); http://git-wip-us.apache.org/repos/asf/karaf/blob/12fdb85f/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/12fdb85f/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/12fdb85f/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index 0a4e5e9..16578dd 100644 --- a/pom.xml +++ b/pom.xml @@ -145,7 +145,9 @@ 1.5.3 3.2.2_1 2.2.0 - + + 4.2.2 + 1.9.2 1.10 1.11 @@ -1048,6 +1050,16 @@ ${felix.connect.version} + + net.java.dev.jna + jna + ${jna.version} + + + net.java.dev.jna + jna-platform + ${jna.version} + org.ow2.asm