Skip to content

Commit 97958c3

Browse files
committed
Service Wrapper: A Java Service Wrapper integration, closes #154.
1 parent 69df71d commit 97958c3

37 files changed

+1539
-4
lines changed

bin/service/elasticsearch

Lines changed: 1258 additions & 0 deletions
Large diffs are not rendered by default.

bin/service/elasticsearch.bat

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
@echo off
2+
setlocal
3+
4+
rem Java Service Wrapper command based script.
5+
6+
rem -----------------------------------------------------------------------------
7+
rem These settings can be modified to fit the needs of your application
8+
rem Optimized for use with version 3.4.0 of the Wrapper.
9+
10+
rem SET ES_HOME
11+
set ES_HOME=%~dp0\..\..
12+
13+
rem The base name for the Wrapper binary.
14+
set _WRAPPER_BASE=\exec\elasticsearch
15+
16+
rem The name and location of the Wrapper configuration file.
17+
rem (Do not remove quotes.)
18+
set _WRAPPER_CONF="..\elasticsearch.conf"
19+
20+
rem Do not modify anything beyond this point
21+
rem -----------------------------------------------------------------------------
22+
23+
if "%OS%"=="Windows_NT" goto nt
24+
echo This script only works with NT-based versions of Windows.
25+
goto :eof
26+
27+
:nt
28+
rem Find the application home.
29+
rem %~dp0 is location of current script under NT
30+
set _REALPATH=%~dp0
31+
32+
rem
33+
rem Decide on the specific Wrapper binary to use (See delta-pack)
34+
rem
35+
if "%PROCESSOR_ARCHITECTURE%"=="AMD64" goto amd64
36+
if "%PROCESSOR_ARCHITECTURE%"=="IA64" goto ia64
37+
set _WRAPPER_L_EXE=%_REALPATH%%_WRAPPER_BASE%-windows-x86-32.exe
38+
goto search
39+
:amd64
40+
set _WRAPPER_L_EXE=%_REALPATH%%_WRAPPER_BASE%-windows-x86-64.exe
41+
goto search
42+
:ia64
43+
set _WRAPPER_L_EXE=%_REALPATH%%_WRAPPER_BASE%-windows-ia-64.exe
44+
goto search
45+
:search
46+
set _WRAPPER_EXE=%_WRAPPER_L_EXE%
47+
if exist "%_WRAPPER_EXE%" goto validate
48+
set _WRAPPER_EXE=%_REALPATH%%_WRAPPER_BASE%.exe
49+
if exist "%_WRAPPER_EXE%" goto validate
50+
echo Unable to locate a Wrapper executable using any of the following names:
51+
echo %_WRAPPER_L_EXE%
52+
echo %_WRAPPER_EXE%
53+
pause
54+
goto :eof
55+
56+
:validate
57+
rem
58+
rem Find the requested command.
59+
rem
60+
for /F %%v in ('echo %1^|findstr "^console$ ^start$ ^pause$ ^resume$ ^stop$ ^restart$ ^install$ ^remove$"') do call :exec set COMMAND=%%v
61+
62+
if "%COMMAND%" == "" (
63+
echo Usage: %0 { console : start : pause : resume : stop : restart : install : remove }
64+
pause
65+
goto :eof
66+
) else (
67+
shift
68+
)
69+
70+
rem
71+
rem Run the application.
72+
rem At runtime, the current directory will be that of wrapper.exe
73+
rem
74+
call :%COMMAND%
75+
if errorlevel 1 goto callerror
76+
goto :eof
77+
78+
:callerror
79+
echo An error occurred in the process.
80+
pause
81+
goto :eof
82+
83+
:console
84+
"%_WRAPPER_EXE%" -c %_WRAPPER_CONF%
85+
goto :eof
86+
87+
:start
88+
"%_WRAPPER_EXE%" -t %_WRAPPER_CONF%
89+
goto :eof
90+
91+
:pause
92+
"%_WRAPPER_EXE%" -a %_WRAPPER_CONF%
93+
goto :eof
94+
95+
:resume
96+
"%_WRAPPER_EXE%" -e %_WRAPPER_CONF%
97+
goto :eof
98+
99+
:stop
100+
"%_WRAPPER_EXE%" -p %_WRAPPER_CONF%
101+
goto :eof
102+
103+
:install
104+
"%_WRAPPER_EXE%" -i %_WRAPPER_CONF%
105+
goto :eof
106+
107+
:remove
108+
"%_WRAPPER_EXE%" -r %_WRAPPER_CONF%
109+
goto :eof
110+
111+
:restart
112+
call :stop
113+
call :start
114+
goto :eof
115+
116+
:exec
117+
%*
118+
goto :eof

bin/service/elasticsearch.conf

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
set.default.ES_HOME=<Path to ElasticSearch Home>
2+
3+
#********************************************************************
4+
# Wrapper Timeout Properties
5+
#********************************************************************
6+
# How long to wait for the JVM to start (in seconds)
7+
wrapper.startup.timeout=300
8+
# How long to wait for the JVM to stop (in seconds)
9+
wrapper.shutdown.timeout=300
10+
# When a ping will timeout to consider the JVM hung (in seconds)
11+
wrapper.ping.timeout=300
12+
13+
#********************************************************************
14+
# Wrapper Java Properties
15+
#********************************************************************
16+
# Java Application
17+
wrapper.java.command=java
18+
19+
# Tell the Wrapper to log the full generated Java command line.
20+
#wrapper.java.command.loglevel=INFO
21+
22+
wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperSimpleApp
23+
24+
wrapper.working.dir=%ES_HOME%
25+
26+
# Java Classpath (include wrapper.jar) Add class path elements as
27+
# needed starting from 1
28+
wrapper.java.classpath.1=%ES_HOME%/bin/service/lib/wrapper.jar
29+
wrapper.java.classpath.2=%ES_HOME%/lib/*.jar
30+
31+
# Java Library Path (location of Wrapper.DLL or libwrapper.so)
32+
wrapper.java.library.path.1=%ES_HOME%/bin/service/lib
33+
34+
# Java Bits. On applicable platforms, tells the JVM to run in 32 or 64-bit mode.
35+
wrapper.java.additional.auto_bits=TRUE
36+
37+
# Java Additional Parameters
38+
wrapper.java.additional.1=-Delasticsearch-service
39+
wrapper.java.additional.2=-Des-foreground=yes
40+
wrapper.java.additional.3=-Des.path.home=%ES_HOME%
41+
wrapper.java.additional.4=-Djline.enabled=true
42+
wrapper.java.additional.5=-XX:+AggressiveOpts
43+
wrapper.java.additional.6=-XX:+UseParNewGC
44+
wrapper.java.additional.7=-XX:+UseConcMarkSweepGC
45+
wrapper.java.additional.8=-XX:+CMSParallelRemarkEnabled
46+
wrapper.java.additional.9=-XX:+HeapDumpOnOutOfMemoryError
47+
48+
# Initial Java Heap Size (in MB)
49+
wrapper.java.initmemory=128
50+
51+
# Maximum Java Heap Size (in MB)
52+
wrapper.java.maxmemory=1024
53+
54+
# Application parameters. Add parameters as needed starting from 1
55+
wrapper.app.parameter.1=org.elasticsearch.bootstrap.Bootstrap
56+
57+
#********************************************************************
58+
# Wrapper Logging Properties
59+
#********************************************************************
60+
# Enables Debug output from the Wrapper.
61+
# wrapper.debug=TRUE
62+
63+
# Format of output for the console. (See docs for formats)
64+
wrapper.console.format=PM
65+
66+
# Log Level for console output. (See docs for log levels)
67+
wrapper.console.loglevel=INFO
68+
69+
# Log file to use for wrapper output logging.
70+
wrapper.logfile=%ES_HOME%/work/logs/service.log
71+
72+
# Format of output for the log file. (See docs for formats)
73+
wrapper.logfile.format=LPTM
74+
75+
# Log Level for log file output. (See docs for log levels)
76+
wrapper.logfile.loglevel=INFO
77+
78+
# Maximum size that the log file will be allowed to grow to before
79+
# the log is rolled. Size is specified in bytes. The default value
80+
# of 0, disables log rolling. May abbreviate with the 'k' (kb) or
81+
# 'm' (mb) suffix. For example: 10m = 10 megabytes.
82+
wrapper.logfile.maxsize=0
83+
84+
# Maximum number of rolled log files which will be allowed before old
85+
# files are deleted. The default value of 0 implies no limit.
86+
wrapper.logfile.maxfiles=0
87+
88+
# Log Level for sys/event log output. (See docs for log levels)
89+
wrapper.syslog.loglevel=NONE
90+
91+
#********************************************************************
92+
# Wrapper General Properties
93+
#********************************************************************
94+
# Allow for the use of non-contiguous numbered properties
95+
wrapper.ignore_sequence_gaps=TRUE
96+
97+
# Title to use when running as a console
98+
wrapper.console.title=ElasticSearch
99+
100+
#********************************************************************
101+
# Wrapper Windows NT/2000/XP Service Properties
102+
#********************************************************************
103+
# WARNING - Do not modify any of these properties when an application
104+
# using this configuration file has been installed as a service.
105+
# Please uninstall the service before modifying this section. The
106+
# service can then be reinstalled.
107+
108+
# Name of the service
109+
wrapper.name=ElasticSearch
110+
111+
# Display name of the service
112+
wrapper.displayname=ElasticSearch
113+
114+
# Description of the service
115+
wrapper.description=Open Source, Distributed, RESTful Search Engine
116+
117+
# Service dependencies. Add dependencies as needed starting from 1
118+
wrapper.ntservice.dependency.1=
119+
120+
# Mode in which the service is installed. AUTO_START, DELAY_START or DEMAND_START
121+
wrapper.ntservice.starttype=AUTO_START
122+
123+
# Allow the service to interact with the desktop.
124+
wrapper.ntservice.interactive=false
125+

bin/service/elasticsearch32

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#! /bin/sh
2+
3+
export DIST_BITS="32"
4+
`dirname $0`/elasticsearch $1

bin/service/elasticsearch64

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#! /bin/sh
2+
3+
export DIST_BITS="64"
4+
`dirname $0`/elasticsearch $1
125 KB
Binary file not shown.
163 KB
Binary file not shown.
244 KB
Binary file not shown.
134 KB
Binary file not shown.
154 KB
Binary file not shown.
135 KB
Binary file not shown.
144 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
142 KB
Binary file not shown.
172 KB
Binary file not shown.
Binary file not shown.
19.2 KB
Binary file not shown.
25.4 KB
Binary file not shown.
36.2 KB
Binary file not shown.
28.4 KB
Binary file not shown.
30 KB
Binary file not shown.
19.9 KB
Binary file not shown.
26.4 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
23.2 KB
Binary file not shown.
37.3 KB
Binary file not shown.
20 KB
Binary file not shown.
27 KB
Binary file not shown.
203 KB
Binary file not shown.

bin/service/lib/wrapper.jar

104 KB
Binary file not shown.

build.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,18 @@ task zip(type: Zip, dependsOn: ['explodedDist']) {
8787
from(explodedDistDir) {
8888
into zipRootFolder
8989
exclude 'bin/elasticsearch'
90+
exclude 'bin/service/elasticsearch'
91+
exclude 'bin/service/elasticsearch32'
92+
exclude 'bin/service/elasticsearch64'
93+
exclude 'bin/service/exec/**'
9094
}
9195
from(explodedDistDir) {
9296
into zipRootFolder
9397
include 'bin/elasticsearch'
98+
include 'bin/service/elasticsearch'
99+
include 'bin/service/elasticsearch32'
100+
include 'bin/service/elasticsearch64'
101+
include 'bin/service/exec/**'
94102
fileMode = 0755
95103
}
96104
}

modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/cluster/node/shutdown/TransportNodesShutdown.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,21 @@ public class TransportNodesShutdown extends TransportNodesOperationAction<NodesS
102102
logger.info("Shutting down in [{}]", request.delay);
103103
threadPool.schedule(new Runnable() {
104104
@Override public void run() {
105-
node.close();
105+
boolean shutdownWithWrapper = false;
106+
if (System.getProperty("elasticsearch-service") != null) {
107+
try {
108+
Class wrapperManager = settings.getClassLoader().loadClass("org.tanukisoftware.wrapper.WrapperManager");
109+
logger.info("Initiating requested shutdown (using service)");
110+
wrapperManager.getMethod("stopAndReturn", int.class).invoke(null, 0);
111+
shutdownWithWrapper = true;
112+
} catch (Throwable e) {
113+
e.printStackTrace();
114+
}
115+
}
116+
if (!shutdownWithWrapper) {
117+
logger.info("Initiating requested shutdown");
118+
node.close();
119+
}
106120
}
107121
}, request.delay.millis(), TimeUnit.MILLISECONDS);
108122
return new NodesShutdownResponse.NodeShutdownResponse(clusterService.state().nodes().localNode());

modules/elasticsearch/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
package org.elasticsearch.bootstrap;
2121

22-
import org.elasticsearch.util.guice.inject.CreationException;
23-
import org.elasticsearch.util.guice.inject.spi.Message;
2422
import org.elasticsearch.ExceptionsHelper;
2523
import org.elasticsearch.Version;
2624
import org.elasticsearch.env.Environment;
@@ -30,6 +28,8 @@
3028
import org.elasticsearch.node.internal.InternalSettingsPerparer;
3129
import org.elasticsearch.util.Classes;
3230
import org.elasticsearch.util.Tuple;
31+
import org.elasticsearch.util.guice.inject.CreationException;
32+
import org.elasticsearch.util.guice.inject.spi.Message;
3333
import org.elasticsearch.util.jline.ANSI;
3434
import org.elasticsearch.util.logging.ESLogger;
3535
import org.elasticsearch.util.logging.Loggers;
@@ -39,8 +39,8 @@
3939
import java.io.File;
4040
import java.util.Set;
4141

42-
import static org.elasticsearch.util.gcommon.collect.Sets.*;
4342
import static jline.ANSIBuffer.ANSICodes.*;
43+
import static org.elasticsearch.util.gcommon.collect.Sets.*;
4444
import static org.elasticsearch.util.settings.ImmutableSettings.Builder.*;
4545
import static org.elasticsearch.util.settings.ImmutableSettings.*;
4646

@@ -131,6 +131,10 @@ public static void main(String[] args) {
131131
String pidFile = System.getProperty("es-pidfile");
132132

133133
boolean foreground = System.getProperty("es-foreground") != null;
134+
// handle the wrapper system property, if its a service, don't run as a service
135+
if (System.getProperty("wrapper.service", "XXX").equalsIgnoreCase("true")) {
136+
foreground = false;
137+
}
134138

135139
Tuple<Settings, Environment> tuple = null;
136140
try {

0 commit comments

Comments
 (0)