Skip to content

Commit ccd5ede

Browse files
mhitzajanhoy
andauthored
SOLR-14410: switch from SysV to systemd service (#428)
* Support Rocky Linux * Upgrade notes. * Print warning if init.d script exists * Review comment: restart on failure Also add RestartSec, StartLimitIntervalSec and StartLimitBurst Co-authored-by: Jan Høydahl <[email protected]>
1 parent 146f9b0 commit ccd5ede

File tree

7 files changed

+102
-140
lines changed

7 files changed

+102
-140
lines changed

solr/CHANGES.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ Other Changes
8282

8383
* SOLR-16995: Add a ReplicaCount class to keep track of replicas per type (Vincent Primault)
8484

85+
* SOLR-14410: Switch from SysV init script to systemd service definition (Marius Ghita via janhoy)
86+
8587
================== 9.6.0 ==================
8688
New Features
8789
---------------------

solr/bin/init.d/solr

Lines changed: 0 additions & 78 deletions
This file was deleted.

solr/bin/install_solr_service.sh

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ print_usage() {
2929
echo ""
3030
echo "Usage: install_solr_service.sh <path_to_solr_distribution_archive> [OPTIONS]"
3131
echo ""
32-
echo " The first argument to the script must be a path to a Solr distribution archive, such as solr-5.0.0.tgz"
32+
echo " The first argument to the script must be a path to a Solr distribution archive, such as solr-10.0.0.tgz"
3333
echo " (only .tgz is supported format for the archive)"
3434
echo ""
3535
echo " Supported OPTIONS include:"
@@ -73,6 +73,8 @@ for command in "grep -E \"^NAME=\" /etc/os-release" \
7373
distro=RedHat
7474
elif [[ ${distro_string,,} == *"centos"* ]]; then
7575
distro=CentOS
76+
elif [[ ${distro_string,,} == *"rocky"* ]]; then
77+
distro=CentOS
7678
elif [[ ${distro_string,,} == *"ubuntu"* ]]; then
7779
distro=Ubuntu
7880
elif [[ ${distro_string,,} == *"suse"* ]]; then
@@ -100,6 +102,10 @@ if [ ! -f "$SOLR_ARCHIVE" ]; then
100102
exit 1
101103
fi
102104

105+
if [ -f "/etc/init.d/${SOLR_SERVICE}" ]; then
106+
echo "WARNING: Found pre-existing /etc/init.d/${SOLR_SERVICE}, please remove!"
107+
fi
108+
103109
# strip off path info
104110
SOLR_INSTALL_FILE=${SOLR_ARCHIVE##*/}
105111
if [ ${SOLR_INSTALL_FILE: -4} == ".tgz" ]; then
@@ -185,8 +191,8 @@ fi
185191
# Test for availability of needed tools
186192
tar --version &>/dev/null || print_error "Script requires the 'tar' command"
187193
if [[ $SOLR_START == "true" ]] ; then
188-
service --version &>/dev/null || service --help &>/dev/null || print_error "Script requires the 'service' command"
189-
java -version &>/dev/null || print_error "Solr requires java, please install or set JAVA_HOME properly"
194+
systemctl --version &>/dev/null || print_error "Script requires the 'systemctl' command"
195+
java -version &>/dev/null || print_error "Solr requires java, please install or set JAVA_HOME properly"
190196
fi
191197
lsof -h &>/dev/null || echo "We recommend installing the 'lsof' command for more stable start/stop of Solr"
192198

@@ -221,21 +227,21 @@ if [ -z "$SOLR_UPGRADE" ]; then
221227
fi
222228

223229
if [ ! "$SOLR_UPGRADE" = "YES" ]; then
224-
if [ -f "/etc/init.d/$SOLR_SERVICE" ]; then
225-
print_usage "/etc/init.d/$SOLR_SERVICE already exists! Perhaps Solr is already setup as a service on this host? To upgrade Solr use the -f option."
230+
if [ -f "/etc/systemd/system/$SOLR_SERVICE.service" ]; then
231+
print_usage "/etc/systemd/system/$SOLR_SERVICE.service already exists! Perhaps Solr is already setup as a service on this host? To upgrade Solr use the -f option."
226232
exit 1
227233
fi
228234

229-
if [ -e "$SOLR_EXTRACT_DIR/$SOLR_SERVICE" ]; then
230-
print_usage "$SOLR_EXTRACT_DIR/$SOLR_SERVICE already exists! Please move this directory / link or choose a different service name using the -s option."
235+
if [ -e "$SOLR_EXTRACT_DIR/$SOLR_SERVICE.service" ]; then
236+
print_usage "$SOLR_EXTRACT_DIR/$SOLR_SERVICE.service already exists! Please move this directory / link or choose a different service name using the -s option."
231237
exit 1
232238
fi
233239
fi
234240

235241
# stop running instance
236-
if [ -f "/etc/init.d/$SOLR_SERVICE" ]; then
242+
if [ -f "/etc/systemd/system/$SOLR_SERVICE.service" ]; then
237243
echo -e "\nStopping Solr instance if exists ...\n"
238-
service "$SOLR_SERVICE" stop
244+
systemctl stop "$SOLR_SERVICE.service"
239245
fi
240246

241247
# create user if not exists
@@ -284,17 +290,22 @@ else
284290
ln -s "$SOLR_INSTALL_DIR" "$SOLR_EXTRACT_DIR/$SOLR_SERVICE"
285291
fi
286292

287-
# install init.d script
288-
echo -e "\nInstalling /etc/init.d/$SOLR_SERVICE script ...\n"
289-
cp "$SOLR_INSTALL_DIR/bin/init.d/solr" "/etc/init.d/$SOLR_SERVICE"
290-
chmod 0744 "/etc/init.d/$SOLR_SERVICE"
291-
chown root: "/etc/init.d/$SOLR_SERVICE"
292-
# do some basic variable substitution on the init.d script
293-
sed_expr1="s#SOLR_INSTALL_DIR=.*#SOLR_INSTALL_DIR=\"$SOLR_EXTRACT_DIR/$SOLR_SERVICE\"#"
294-
sed_expr2="s#SOLR_ENV=.*#SOLR_ENV=\"/etc/default/$SOLR_SERVICE.in.sh\"#"
295-
sed_expr3="s#RUNAS=.*#RUNAS=\"$SOLR_USER\"#"
296-
sed_expr4="s#Provides:.*#Provides: $SOLR_SERVICE#"
297-
sed -i -e "$sed_expr1" -e "$sed_expr2" -e "$sed_expr3" -e "$sed_expr4" "/etc/init.d/$SOLR_SERVICE"
293+
# install systemd service file
294+
echo -e "\nInstalling /etc/systemd/system/$SOLR_SERVICE.service ...\n"
295+
cp "$SOLR_INSTALL_DIR/bin/systemd/solr.service" "/etc/systemd/system/$SOLR_SERVICE.service"
296+
chmod 0644 "/etc/systemd/system/$SOLR_SERVICE.service"
297+
chown root "/etc/systemd/system/$SOLR_SERVICE.service"
298+
299+
# do some basic variable substitution on the service file
300+
SAFE_SOLR_INSTALL_DIR="$SOLR_EXTRACT_DIR/$SOLR_SERVICE"
301+
SAFE_SOLR_SERVICE=$(systemd-escape --path "$SOLR_SERVICE")
302+
sed_expr1="s@{{SOLR_INSTALL_DIR}}@$SAFE_SOLR_INSTALL_DIR@g"
303+
sed_expr2="s/{{SOLR_SERVICE}}/$SAFE_SOLR_SERVICE/g"
304+
sed_expr3="s/{{SOLR_USER}}/$SOLR_USER/g"
305+
sed -i -e "$sed_expr1" -e "$sed_expr2" -e "$sed_expr3" "/etc/systemd/system/$SOLR_SERVICE.service"
306+
307+
# we need to reload systemd after changing service files
308+
systemctl daemon-reload
298309

299310
# install/move configuration
300311
if [ ! -d /etc/default ]; then
@@ -336,19 +347,13 @@ find "$SOLR_VAR_DIR" -type d -print0 | xargs -0 chmod 0750
336347
find "$SOLR_VAR_DIR" -type f -print0 | xargs -0 chmod 0640
337348

338349
# configure autostart of service
339-
if [[ "$distro" == "RedHat" || "$distro" == "CentOS" || "$distro" == "SUSE" ]]; then
340-
chkconfig "$SOLR_SERVICE" on
341-
else
342-
update-rc.d "$SOLR_SERVICE" defaults
343-
fi
350+
systemctl enable "$SOLR_SERVICE.service"
344351
echo "Service $SOLR_SERVICE installed."
345352
echo "Customize Solr startup configuration in /etc/default/$SOLR_SERVICE.in.sh"
346353

347354
# start service
348355
if [[ $SOLR_START == "true" ]] ; then
349-
service "$SOLR_SERVICE" start
350-
sleep 5
351-
service "$SOLR_SERVICE" status
356+
systemctl start "$SOLR_SERVICE.service"
352357
else
353-
echo "Not starting Solr service (option -n given). Start manually with 'service $SOLR_SERVICE start'"
358+
echo "Not starting Solr service (option -n given). Start manually with 'systemctl start $SOLR_SERVICE'"
354359
fi

solr/bin/systemd/solr.service

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one or more
2+
# contributor license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright ownership.
4+
# The ASF licenses this file to You under the Apache License, Version 2.0
5+
# (the "License"); you may not use this file except in compliance with
6+
# the License. You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
# This unit file generated by install_solr_service.sh script
17+
18+
[Unit]
19+
Description=Apache Solr
20+
After=network.target
21+
StartLimitIntervalSec=300
22+
StartLimitBurst=5
23+
24+
[Service]
25+
Type=forking
26+
User={{SOLR_USER}}
27+
Environment=SOLR_INCLUDE=/etc/default/{{SOLR_SERVICE}}.in.sh
28+
ExecStart={{SOLR_INSTALL_DIR}}/bin/solr start
29+
ExecStop={{SOLR_INSTALL_DIR}}/bin/solr stop
30+
Restart=on-failure
31+
RestartSec=3s
32+
TimeoutSec=180s
33+
PrivateTmp=true
34+
LimitNOFILE=65000
35+
LimitNPROC=65000
36+
37+
[Install]
38+
WantedBy=multi-user.target

solr/packaging/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ distributions {
122122
filesMatching([
123123
"**/*.sh",
124124
"**/bin/solr",
125-
"**/bin/init.d/solr",
125+
"**/bin/systemd/solr.service",
126126
]) { copy ->
127127
copy.setMode(0755)
128128
}

solr/solr-ref-guide/modules/deployment-guide/pages/taking-solr-to-production.adoc

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,9 @@ SOLR_LOGS_DIR=/var/solr/logs
152152

153153
For more information about Log4J configuration, please see: xref:configuring-logging.adoc[].
154154

155-
==== init.d Script
155+
==== systemd Service
156156

157-
When running a service like Solr on Linux, it’s common to setup an init.d script so that system administrators can control Solr using the service tool, such as: `service solr start`.
158-
The installation script creates a very basic init.d script to help you get started.
159-
Take a moment to inspect the `/etc/init.d/solr` file, which is the default script name setup by the installation script.
160-
If you used the `-s` option on the install script to change the name of the service, then the filename will be different.
161-
Notice that the following variables are setup for your environment based on the parameters passed to the installation script:
157+
The installation script creates a very basic systemd service to help you get started. Take a moment to inspect the `/etc/systemd/system/solr.service` file, which is the default service file setup by the installation script. If you used the `-s` option on the install script to change the name of the service, then the filename will be different. Notice that the following variables are setup for your environment based on the parameters passed to the installation script:
162158

163159
[source,bash]
164160
----
@@ -167,44 +163,42 @@ SOLR_ENV=/etc/default/solr.in.sh
167163
RUNAS=solr
168164
----
169165

170-
The `SOLR_INSTALL_DIR` and `SOLR_ENV` variables should be self-explanatory.
171-
The `RUNAS` variable sets the owner of the Solr process, such as `solr`; if you don’t set this value, the script will run Solr as **root**, which is not recommended for production.
172-
You can use the `/etc/init.d/solr` script to start Solr by doing the following as root:
166+
The `SOLR_INSTALL_DIR` and `SOLR_ENV` variables should be self-explanatory. The `RUNAS` variable sets the owner of the Solr process, such as `solr`; if you don’t set this value, the script will run Solr as **root**, which is not recommended for production. You can start Solr by doing the following as root:
173167

174168
[source,bash]
175169
----
176-
service solr start
170+
systemctl start solr
177171
----
178172

179-
The `/etc/init.d/solr` script also supports the **stop**, **restart**, and *status* commands.
180-
Please keep in mind that the init script that ships with Solr is very basic and is intended to show you how to setup Solr as a service.
181-
However, it’s also common to use more advanced tools like *supervisord* or *upstart* to control Solr as a service on Linux.
182-
While showing how to integrate Solr with tools like supervisord is beyond the scope of this guide, the `init.d/solr` script should provide enough guidance to help you get started.
183173
Also, the installation script sets the Solr service to start automatically when the host machine initializes.
184174

185175
=== Progress Check
186176

187-
In the next section, we cover some additional environment settings to help you fine-tune your production setup.
188-
However, before we move on, let's review what we've achieved thus far.
189-
Specifically, you should be able to control Solr using `/etc/init.d/solr`.
190-
Please verify the following commands work with your setup:
177+
In the next section, we cover some additional environment settings to help you fine-tune your production setup. However, before we move on, let's review what we've achieved thus far. Specifically, you should be able to control Solr using `systemctl`. Please verify the following commands work with your setup:
191178

192179
[source,bash]
193180
----
194-
sudo service solr restart
195-
sudo service solr status
181+
sudo systemctl restart solr
182+
sudo systemctl status solr
196183
----
197184

198185
The status command should give some basic information about the running Solr node that looks similar to:
199186

200187
[source,text]
201188
----
202-
Solr process PID running on port 8983
203-
{
204-
"version":"5.0.0 - ubuntu - 2014-12-17 19:36:58",
205-
"startTime":"2014-12-19T19:25:46.853Z",
206-
"uptime":"0 days, 0 hours, 0 minutes, 8 seconds",
207-
"memory":"85.4 MB (%17.4) of 490.7 MB"}
189+
● solr.service - Apache Solr
190+
Loaded: loaded (/etc/systemd/system/solr.service; enabled; vendor preset: disabled)
191+
Active: active (running) since Thu 2020-04-16 20:42:01 UTC; 53s ago
192+
Main PID: 3708 (java)
193+
Tasks: 41 (limit: 25056)
194+
Memory: 517.1M
195+
CGroup: /system.slice/solr.service
196+
└─3708 java -server -Xms256M -Xmx512M -XX:+UseG1GC -XX:+PerfDisableSharedMem -XX:+ParallelRefProcEnabled -XX:MaxGCPauseMillis=250 -XX:+UseLargePages -XX:+AlwaysPreTouch -Xlog:gc*:file=/var/solr/logs/solr_gc.log:time,...
197+
Apr 16 20:41:47 localhost.localdomain systemd[1]: Starting Apache Solr...
198+
Apr 16 20:42:01 localhost.localdomain solr[3661]: [326B blob data]
199+
Apr 16 20:42:01 localhost.localdomain solr[3661]: Started Solr server on port 8983 (pid=3708). Happy searching!
200+
Apr 16 20:42:01 localhost.localdomain solr[3661]: [14B blob data]
201+
Apr 16 20:42:01 localhost.localdomain systemd[1]: Started Apache Solr.
208202
----
209203

210204
If the `status` command is not successful, look for error messages in `/var/solr/logs/solr.log`.
@@ -469,10 +463,7 @@ Below 32GB, Java is able to use compressed pointers, but above that point, large
469463
If your use case needs over 31GB of heap, then consider multiple nodes since they typically will perform better than one node with >32GB of heap.
470464
====
471465

472-
If your use case requires multiple instances, at a minimum you will need unique Solr home directories for each node you want to run; ideally, each home should be on a different physical disk so that multiple Solr nodes don’t have to compete with each other when accessing files on disk.
473-
Having different Solr home directories implies that you’ll need a different include file for each node.
474-
Moreover, if using the `/etc/init.d/solr` script to control Solr as a service, then you’ll need a separate script for each node.
475-
The easiest approach is to use the service installation script to add multiple services on the same host, such as:
466+
If your use case requires multiple instances, at a minimum you will need unique Solr home directories for each node you want to run; ideally, each home should be on a different physical disk so that multiple Solr nodes don’t have to compete with each other when accessing files on disk. Having different Solr home directories implies that you’ll need a different include file for each node. Moreover, if using `systemctl` to control Solr, then you’ll need a separate service for each node. The easiest approach is to use the service installation script to add multiple services on the same host, such as:
476467

477468
[source,bash,subs="attributes"]
478469
----
@@ -484,6 +475,6 @@ After installing the solr2 service, verify it works correctly by doing:
484475

485476
[source,bash]
486477
----
487-
sudo service solr2 restart
488-
sudo service solr2 status
478+
sudo systemctl start solr2
479+
sudo systemctl status solr2
489480
----

solr/solr-ref-guide/modules/upgrade-notes/pages/major-changes-in-solr-10.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ Before starting an upgrade to this version of Solr, please take the time to revi
3333
* `SolrClient` implementations that rely on "base URL" strings now only accept "root" URL paths (i.e. URLs that end in "/solr").
3434
Users who previously relied on collection-specific URLs to avoid including the collection name with each request can instead achieve this by specifying a "default collection" using the `withDefaultCollection` method available on most `SolrClient` Builders.
3535

36+
=== Service installer
37+
38+
The service installer now installs a `systemd` startup script instead of an `init.d` startup script. It is up to the user to uninstall any existing `init.d` script when upgrading.
39+
3640
=== Deprecation removals
3741

3842
* The `jaegertracer-configurator` module, which was deprecated in 9.2, is removed. Users should migrate to the `opentelemetry` module.

0 commit comments

Comments
 (0)