-
Notifications
You must be signed in to change notification settings - Fork 24.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Notify systemd when Elasticsearch is ready (#44673)
Today our systemd service defaults to a service type of simple. This means that systemd assumes Elasticsearch is ready as soon as the ExecStart (bin/elasticsearch) process is forked off. This means that the service appears ready long before it actually is, so before it is ready to receive requests. It also means that services that want to depend on Elasticsearch being ready to start can not as there is not a reliable mechanism to determine this. This commit changes the service type to notify. This requires that Elasticsearch sends a notification message via libsystemd sd_notify method. This commit does that by using JNA to invoke this native method. Additionally, we use this integration to also notify systemd when we are stopping.
- Loading branch information
1 parent
805d103
commit d558d95
Showing
8 changed files
with
384 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch 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. | ||
*/ | ||
|
||
esplugin { | ||
description 'Integrates Elasticsearch with systemd' | ||
classname 'org.elasticsearch.systemd.SystemdPlugin' | ||
} | ||
|
||
integTest.enabled = false |
49 changes: 49 additions & 0 deletions
49
modules/systemd/src/main/java/org/elasticsearch/systemd/Libsystemd.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch 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.elasticsearch.systemd; | ||
|
||
import com.sun.jna.Native; | ||
|
||
import java.security.AccessController; | ||
import java.security.PrivilegedAction; | ||
|
||
/** | ||
* Provides access to the native method sd_notify from libsystemd. | ||
*/ | ||
class Libsystemd { | ||
|
||
static { | ||
AccessController.doPrivileged((PrivilegedAction<Object>) () -> { | ||
Native.register(Libsystemd.class, "libsystemd.so.0"); | ||
return null; | ||
}); | ||
} | ||
|
||
/** | ||
* Notify systemd of state changes. | ||
* | ||
* @param unset_environment if non-zero, the NOTIFY_SOCKET environment variable will be unset before returning and further calls to | ||
* sd_notify will fail | ||
* @param state a new-line separated list of variable assignments; some assignments are understood directly by systemd | ||
* @return a negative error code on failure, and positive if status was successfully sent | ||
*/ | ||
static native int sd_notify(int unset_environment, String state); | ||
|
||
} |
90 changes: 90 additions & 0 deletions
90
modules/systemd/src/main/java/org/elasticsearch/systemd/SystemdPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch 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.elasticsearch.systemd; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.apache.lucene.util.Constants; | ||
import org.elasticsearch.Assertions; | ||
import org.elasticsearch.Build; | ||
import org.elasticsearch.plugins.ClusterPlugin; | ||
import org.elasticsearch.plugins.Plugin; | ||
|
||
public class SystemdPlugin extends Plugin implements ClusterPlugin { | ||
|
||
private static final Logger logger = LogManager.getLogger(SystemdPlugin.class); | ||
|
||
private final boolean enabled; | ||
|
||
final boolean isEnabled() { | ||
return enabled; | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public SystemdPlugin() { | ||
this(true, Constants.LINUX, System.getenv("ES_SD_NOTIFY")); | ||
} | ||
|
||
SystemdPlugin(final boolean assertIsPackageDistribution, final boolean isLinux, final String esSDNotify) { | ||
if (Assertions.ENABLED && assertIsPackageDistribution) { | ||
// our build is configured to only include this module in the package distributions | ||
assert Build.CURRENT.type() == Build.Type.DEB || Build.CURRENT.type() == Build.Type.RPM : Build.CURRENT.type(); | ||
} | ||
if (isLinux == false || esSDNotify == null) { | ||
enabled = false; | ||
return; | ||
} | ||
if (Boolean.TRUE.toString().equals(esSDNotify) == false && Boolean.FALSE.toString().equals(esSDNotify) == false) { | ||
throw new RuntimeException("ES_SD_NOTIFY set to unexpected value [" + esSDNotify + "]"); | ||
} | ||
enabled = Boolean.TRUE.toString().equals(esSDNotify); | ||
} | ||
|
||
int sd_notify(@SuppressWarnings("SameParameterValue") final int unset_environment, final String state) { | ||
return Libsystemd.sd_notify(unset_environment, state); | ||
} | ||
|
||
@Override | ||
public void onNodeStarted() { | ||
if (enabled == false) { | ||
return; | ||
} | ||
final int rc = sd_notify(0, "READY=1"); | ||
logger.trace("sd_notify returned [{}]", rc); | ||
if (rc < 0) { | ||
// treat failure to notify systemd of readiness as a startup failure | ||
throw new RuntimeException("sd_notify returned error [" + rc + "]"); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
if (enabled == false) { | ||
return; | ||
} | ||
final int rc = sd_notify(0, "STOPPING=1"); | ||
logger.trace("sd_notify returned [{}]", rc); | ||
if (rc < 0) { | ||
// do not treat failure to notify systemd of stopping as a failure | ||
logger.warn("sd_notify returned error [{}]", rc); | ||
} | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
modules/systemd/src/main/plugin-metadata/plugin-security.policy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch 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. | ||
*/ | ||
|
||
grant codeBase "${codebase.systemd}" { | ||
// for registering native methods | ||
permission java.lang.RuntimePermission "accessDeclaredMembers"; | ||
}; |
Oops, something went wrong.