Bug 204988 - net-mgmt/riemann: Startup script doesn't work if more than one java process is running
Summary: net-mgmt/riemann: Startup script doesn't work if more than one java process i...
Status: Closed FIXED
Alias: None
Product: Ports & Packages
Classification: Unclassified
Component: Individual Port(s) (show other bugs)
Version: Latest
Hardware: Any Any
: Normal Affects Some People
Assignee: Kurt Jaeger
URL:
Keywords: patch, patch-ready
Depends on:
Blocks:
 
Reported: 2015-12-03 08:38 UTC by Davide D'Amico
Modified: 2016-02-06 17:12 UTC (History)
3 users (show)

See Also:
dch: maintainer-feedback+
koobs: merge-quarterly?


Attachments
Riemann startup script patch (3.15 KB, patch)
2015-12-03 08:38 UTC, Davide D'Amico
no flags Details | Diff
riemann.in patch (2.19 KB, patch)
2015-12-03 10:28 UTC, Davide D'Amico
koobs: maintainer-approval-
Details | Diff
use daemon and rc.subr better (3.19 KB, patch)
2016-01-15 10:02 UTC, Dave Cottlehuber
dch: maintainer-approval+
Details | Diff
attach the correct patch :/ (3.19 KB, patch)
2016-01-15 10:18 UTC, Dave Cottlehuber
dch: maintainer-approval+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Davide D'Amico 2015-12-03 08:38:52 UTC
Created attachment 163804 [details]
Riemann startup script patch

Hi,
I have an ELK stack running on a fbsd-10.2 (so elasticsearch, kibana and logstash).
Tried to add riemann on top of it refuses to start it because it finds another java (procname) process running and exit (1):

root@fbsd:~ # service riemann start
riemann already running?  (pid=74112 604).
root@fbsd:~ # ps axwww | grep -E '(74112|604)'
74111  -  Is      0:00.00 daemon: /usr/local/logstash/bin/logstash[74112] (daemon)
74112  -  S       5:27.99 /usr/local/openjdk7/bin/java -Des.path.data=/var/db/logstash -Xmx1g -Xss2048k -Djffi.boot.library.path=/usr/local/logstash/vendor/jruby/lib/jni -Des.path.data=/var/db/logstash -Xbootclasspath/a:/usr/local/logstash/vendor/jruby/lib/jruby.jar -classpath : -Djruby.home=/usr/local/logstash/vendor/jruby -Djruby.lib=/usr/local/logstash/vendor/jruby/lib -Djruby.script=jruby -Djruby.shell=/bin/sh org.jruby.Main --1.9 /usr/local/logstash/lib/bootstrap/environment.rb logstash/runner.rb agent -f /usr/local/etc/logstash/logstash.conf --log /var/log/logstash.log
  604 u0- I      16:32.84 /usr/local/openjdk7/bin/java -Des.path.conf=/usr/local/etc/elasticsearch -Des.path.scripts=/usr/local/libexec/elasticsearch -Xms256m -Xmx1g -Djava.awt.headless=true -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+HeapDumpOnOutOfMemoryError -XX:+DisableExplicitGC -Dfile.encoding=UTF-8 -Djna.nosys=true -Des.path.home=/usr/local/lib/elasticsearch -cp /usr/local/lib/elasticsearch/lib/elasticsearch-2.1.0.jar:/usr/local/lib/elasticsearch/lib/* org.elasticsearch.bootstrap.Elasticsearch start -d --pidfile=/var/run/elasticsearch.pid

So I patched the startup script with the attached patch to make it work (it uses jps).
Comment 1 Davide D'Amico 2015-12-03 10:27:41 UTC
Comment on attachment 163804 [details]
Riemann startup script patch

>diff -ruh riemann.orig/files/riemann.in riemann/files/riemann.in
>--- riemann.orig/files/riemann.in       2015-02-27 14:35:09.000000000 -0500
>+++ riemann/files/riemann.in    2015-12-03 03:36:46.418837000 -0500
>@@ -28,11 +28,11 @@
> load_rc_config $name
>
> : ${riemann_enable="NO"}
>-: ${riemann_user:="%%RIEMANN_USER%%"}
>-: ${riemann_group:="%%RIEMANN_GROUP%%"}
>-: ${riemann_config:="%%PREFIX%%/etc/riemann/riemann.config.sample"}
>-: ${riemann_jarfile:="%%JAVAJARDIR%%/riemann.jar"}
>-: ${riemann_java_home:="%%JAVA_HOME%%"}
>+: ${riemann_user:="riemann"}
>+: ${riemann_group:="riemann"}
>+: ${riemann_config:="/usr/local/etc/riemann/riemann.config.sample"}
>+: ${riemann_jarfile:="/usr/local/share/java/classes/riemann.jar"}
>+: ${riemann_java_home:="/usr/local/openjdk7"}
> : ${riemann_min_mem:="256m"}
> : ${riemann_max_mem:="1g"}
> : ${riemann_java_opts:=" -server \
>@@ -53,12 +53,15 @@
>         -XX:+UseParNewGC \
>         -XX:CMSInitiatingOccupancyFraction=75 "}
>
>-riemann_pidfile="%%RIEMANN_PIDDIR%%${name}.pid"
>+riemann_pidfile="/var/run/riemann/${name}.pid"
> command="/usr/sbin/daemon"
> procname="${riemann_java_home}/bin/java"
> command_args="-f -c -p ${riemann_pidfile} ${procname} ${riemann_java_opts} \
>     -jar ${riemann_jarfile} ${riemann_config}"
> required_files="${java_cmd} ${riemann_config}"
>+status_cmd="riemann_status"
>+start_cmd="riemann_start"
>+stop_cmd="riemann_stop"
>
> riemann_prestart()
> {
>@@ -66,4 +69,72 @@
> }
> start_precmd=${name}_prestart
>
>+riemann_start()
>+{
>+    check_if_running=$(/usr/local/bin/jps | grep ${name} | awk '{print $1}')
>+    if [ ! -z "${check_if_running}" ]; then
>+        echo "${name} seems running (pid: ${check_if_running})."
>+        return 1
>+    fi
>+
>+    rc_pid=$(riemann_check_pidfile $riemann_pidfile)
>+    if [ -z "$rc_pid" ]; then
>+        echo "Starting ${name}."
>+        ${command} ${command_args}
>+    else
>+        echo "${name} seems running ($rc_pid)."
>+        return 1
>+    fi
>+
>+}
>+
>+riemann_status()
>+{
>+        rc_pid=$(riemann_check_pidfile $riemann_pidfile)
>+
>+        if [ -z "$rc_pid" ]; then
>+                [ -n "$rc_fast" ] && return 0
>+                echo "${name} not running? (check $riemann_pidfile)."
>+                return 1
>+        fi
>+        echo "${name} is running as pid ${rc_pid}."
>+}
>+
>+riemann_check_pidfile()
>+{
>+        _pidfile=$1
>+        if [ -z "$_pidfile" ]; then
>+                err 3 'USAGE: riemann_check_pidfile pidfile'
>+        fi
>+        if [ ! -f $_pidfile ]; then
>+                debug "pid file ($_pidfile): not readable."
>+                return
>+        fi
>+        read _pid _junk < $_pidfile
>+        if [ -z "$_pid" ]; then
>+                debug "pid file ($_pidfile): no pid in file."
>+                return
>+        fi
>+        if [ -n "`/usr/local/bin/jps -l | grep -e "^$_pid"`" ]; then
>+                echo -n $_pid
>+        fi
>+}
>+
>+riemann_stop()
>+{
>+        rc_pid=$(riemann_check_pidfile $riemann_pidfile)
>+
>+        if [ -z "$rc_pid" ]; then
>+                [ -n "$rc_fast" ] && return 0
>+                echo "${name} not running? (check $riemann_pidfile)."
>+                return 1
>+        fi
>+
>+        echo "Stopping ${name}."
>+        kill ${rc_pid} 2> /dev/null
>+}
>+
>+
>+
>+
> run_rc_command "$1"
Comment 2 Davide D'Amico 2015-12-03 10:28:10 UTC
Created attachment 163807 [details]
riemann.in patch
Comment 3 Davide D'Amico 2015-12-07 12:21:48 UTC
ping
Comment 4 Davide D'Amico 2015-12-10 16:47:33 UTC
Well, nevermind.
Comment 5 Dave Cottlehuber freebsd_committer freebsd_triage 2015-12-10 21:01:21 UTC
Thanks for supplying the patch & adding the issue.

I've taken a look at your patch and was comparing it to the tomcat one https://github.com/freebsd/freebsd-ports/blob/master/www/tomcat6/files/tomcat6.in , did you base it off a specific port or is this from scratch?

AFAICT jps is bundled in openjdk, even though it's not explicitly part of the pkg-list. I filed #204989 for that.
Comment 6 Roman Bogorodskiy freebsd_committer freebsd_triage 2015-12-10 21:15:11 UTC
Maintainer is active actually, so re-open.
Comment 7 Kubilay Kocak freebsd_committer freebsd_triage 2015-12-11 07:54:00 UTC
@Dave Do you approve attachment 163807 [details] or do you have alternative instructions on how to proceed to resolution?
Comment 8 Dave Cottlehuber freebsd_committer freebsd_triage 2015-12-11 09:30:13 UTC
The patch doesn't work consistently with jps so I'm fiddling with alternatives. I've tried a jsvc approach like tomcat but its not quite working yet :/.
Comment 9 Kubilay Kocak freebsd_committer freebsd_triage 2015-12-11 09:31:19 UTC
So not approved, for now, yes?
Comment 10 Dave Cottlehuber freebsd_committer freebsd_triage 2015-12-11 10:25:11 UTC
jsvc requires code changes to riemann, so that's a no-go either atm.
Comment 11 Davide D'Amico 2015-12-11 14:30:47 UTC
I see why /usr/local/bin/jps (that is a symlink to /usr/local/bin/javavm in the case of openjdk) is not a good approach: there is a sysutils/jps project that installs a /usr/local/bin/jps and that's bad.
Comment 12 Dave Cottlehuber freebsd_committer freebsd_triage 2016-01-15 10:02:51 UTC
Created attachment 165624 [details]
use daemon and rc.subr better

This came from a couple of good suggestions from @hirojin, traded in kind for miso soup.

David - here's a link to the updated /usr/local/etc/rc.d/riemann script, you can just drop this in instead of needing to build the port from source if you want to check it -- I know you're not using riemann any more so no problem if you can't. I used `lien repl` here as a duplicate and that was sufficient.

# checks

- portlint -C is ok
- poudriere is ok

# diff

https://github.com/dch/freebsd-ports/commit/9f006ed
Comment 13 Dave Cottlehuber freebsd_committer freebsd_triage 2016-01-15 10:18:20 UTC
Created attachment 165625 [details]
attach the correct patch :/

# changes

PR:		204988
Submitted by:   Dave Cottlehuber <dch@skunkwerks.at> (maintainer)
Assisted by:    Igor Galić <i.galic@brainsware.org>

- use a simple pidfile instead of a whole piddir
- add a -Dapp=riemann parameter to java invocation so ps | grep can find riemann easily
- remove procname and rely on daemon to handle this
- use daemon's pid and not riemanns' pid (-P parent flag change)
- add daemon's -r restart flag to keep java running at all costs

# checks

- poudriere      OK (10.2 amd64)
- portlint -C    OK
- start, kill -9, status, stop, start of riemann while running `lein repl` suggests we got this fixed now :-)

# questions

I changed SUB_LIST to include the pidfile (RIEMANN_PIDFILE=${RIEMANN_PIDFILE} )
The pidfile is not listed in pkg-list either.
Comment 14 Dave Cottlehuber freebsd_committer freebsd_triage 2016-01-15 10:20:29 UTC
David: built version of /usr/local/etc/riemann if needed https://dpaste.de/v5qT
Comment 15 Davide D'Amico 2016-01-15 11:27:29 UTC
Like the -Dapp.name. This should be propagated to all other java apps, imho.

Thanks,
d.
Comment 16 Kurt Jaeger freebsd_committer freebsd_triage 2016-02-05 18:40:15 UTC
Testbuilds@work
Comment 17 Kurt Jaeger freebsd_committer freebsd_triage 2016-02-06 17:07:04 UTC
Testbuilds are fine.
Comment 18 commit-hook freebsd_committer freebsd_triage 2016-02-06 17:11:54 UTC
A commit references this bug:

Author: pi
Date: Sat Feb  6 17:10:58 UTC 2016
New revision: 408306
URL: https://svnweb.freebsd.org/changeset/ports/408306

Log:
  net-mgmt/riemann: fix startup script

  - use a simple pidfile instead of a whole piddir
  - add a -Dapp=riemann parameter to java invocation so
    ps | grep can find riemann easily
  - remove procname and rely on daemon to handle this
  - use daemon's pid and not riemanns' pid (-P parent flag change)
  - add daemon's -r restart flag to keep java running at all costs

  PR:		204988
  Submitted by:	David D'Amico <davide.damico@gmail.com>
  Approved by:	Dave Cottlehuber <dch@skunkwerks.at> (maintainer)

Changes:
  head/net-mgmt/riemann/Makefile
  head/net-mgmt/riemann/files/riemann.in
  head/net-mgmt/riemann/pkg-plist
Comment 19 Kurt Jaeger freebsd_committer freebsd_triage 2016-02-06 17:12:33 UTC
Committed, thanks.