-
Notifications
You must be signed in to change notification settings - Fork 25.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Notify systemd when Elasticsearch is ready #44673
Merged
Merged
Changes from 6 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
067fb06
Notify systemd when Elasticsearch is ready
jasontedor 3e0c5c1
Add assertion
jasontedor 9d05d10
Add tests
jasontedor ff20d0f
Add missing invocation
jasontedor b5278e5
Remove void suppression
jasontedor 493c293
Refactoring for simplification
jasontedor c0822af
Update modules/systemd/src/main/java/org/elasticsearch/systemd/System…
jasontedor a4ccc6c
Merge branch 'master' into sd_notify
elasticmachine 4692c3b
Merge remote-tracking branch 'elastic/master' into sd_notify
jasontedor f1e30b8
Move grant
jasontedor c358933
Final cleanup before merge
jasontedor bdfb5e5
Be consistent
jasontedor 1080c52
Unindent the happy path
jasontedor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; | ||
|
||
import java.io.IOException; | ||
|
||
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 = "true".equals(esSDNotify); | ||
} | ||
|
||
int sd_notify(@SuppressWarnings("SameParameterValue") final int unset_environment, final String state) { | ||
return Libsystemd.sd_notify(0, state); | ||
jasontedor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
@Override | ||
public void onNodeStarted() { | ||
if (enabled) { | ||
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() throws IOException { | ||
if (enabled) { | ||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't this fail with the packaged distributions on OS-es that don't support systemd, or does everything in our support matrix support it ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don’t try to load the native library if the environment variable ES_SD_NOTIFY is not set to “true” and in this PR we do that only in the systemd service integration (see the unit file).