Skip to content

Move systemd notification into cli #108865

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

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions distribution/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ xpack.subprojects.findAll { it.parent == xpack }.each { Project xpackModule ->
}
}

distro.copyModule(processSystemdOutputsTaskProvider, project(':modules:systemd'))
//distro.copyModule(processSystemdOutputsTaskProvider, project(':modules:systemd'))

project(':test:external-modules').subprojects.each { Project testModule ->
distro.copyModule(processExternalTestOutputsTaskProvider, testModule)
Expand Down Expand Up @@ -372,9 +372,9 @@ configure(subprojects.findAll { ['archives', 'packages'].contains(it.name) }) {
if (BuildParams.isSnapshotBuild()) {
from(buildExternalTestModulesTaskProvider)
}
if (project.path.startsWith(':distribution:packages')) {
/*if (project.path.startsWith(':distribution:packages')) {
from(systemdModuleFiles)
}
}*/
}
}

Expand Down
7 changes: 7 additions & 0 deletions distribution/tools/server-cli/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@ apply plugin: 'elasticsearch.build'
dependencies {
compileOnly project(":server")
compileOnly project(":libs:elasticsearch-cli")
compileOnly project(":libs:elasticsearch-native")

testImplementation project(":test:framework")
}

tasks.named("compileJava").configure {
// nativeaccess only has qualified exports. Even though the cli is not run modular, compilation is done with the module path
// so nativeaccess needs to be exported here, just at compile time
options.compilerArgs << '--add-exports' << 'org.elasticsearch.nativeaccess/org.elasticsearch.nativeaccess=ALL-UNNAMED'
}

tasks.named("test").configure {
systemProperty "tests.security.manager", "false"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
import org.elasticsearch.common.cli.EnvironmentAwareCommand;
import org.elasticsearch.common.settings.SecureSettings;
import org.elasticsearch.common.settings.SecureString;
import org.elasticsearch.core.Booleans;
import org.elasticsearch.env.Environment;
import org.elasticsearch.monitor.jvm.JvmInfo;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicBoolean;
Expand All @@ -48,6 +50,7 @@ class ServerCli extends EnvironmentAwareCommand {
// flag for indicating shutdown has begun. we use an AtomicBoolean to double as a synchronization object
private final AtomicBoolean shuttingDown = new AtomicBoolean(false);
private volatile ServerProcess server;
private volatile SystemdNotifier notifier;

// visible for testing
ServerCli() {
Expand Down Expand Up @@ -249,6 +252,9 @@ public void close() throws IOException {
server.stop();
}
}
if (notifier != null) {
notifier.close();
}
}

// allow subclasses to access the started process
Expand All @@ -270,9 +276,21 @@ protected ServerProcess startServer(Terminal terminal, ProcessInfo processInfo,
.withServerArgs(args)
.withTempDir(tempDir)
.withJvmOptions(jvmOptions);
this.notifier = maybeStartSystemdNotifier(processInfo);
if (notifier != null) {
serverProcessBuilder.withListener(notifier);
}
return serverProcessBuilder.start();
}

private static SystemdNotifier maybeStartSystemdNotifier(ProcessInfo processInfo) {
String notifyFlag = processInfo.envVars().get("ES_SD_NOTIFY");
if (Booleans.parseBoolean(notifyFlag, false)) {
return new SystemdNotifier();
}
return null;
}

// protected to allow tests to override
protected SecureSettingsLoader secureSettingsLoader(Environment env) {
// TODO: Use the environment configuration to decide what kind of secrets store to load
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class ServerProcessBuilder {
private ProcessInfo processInfo;
private List<String> jvmOptions;
private Terminal terminal;
private ServerProcessListener listener;

// this allows mocking the process building by tests
interface ProcessStarter {
Expand Down Expand Up @@ -89,6 +90,11 @@ public ServerProcessBuilder withTerminal(Terminal terminal) {
return this;
}

public ServerProcessBuilder withListener(ServerProcessListener listener) {
this.listener = listener;
return this;
}

private Map<String, String> getEnvironment() {
Map<String, String> envVars = new HashMap<>(processInfo.envVars());

Expand Down Expand Up @@ -175,6 +181,10 @@ ServerProcess start(ProcessStarter processStarter) throws UserException {
}
}

if (listener != null) {
listener.onReady();
}

return new ServerProcess(jvmProcess, errorPump);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.server.cli;

/**
* A listener for Elasticsearch process events.
*/
public interface ServerProcessListener {

/**
* Called when the server process has successfully started and is ready.
*/
void onReady();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.server.cli;

import org.elasticsearch.core.TimeValue;
import org.elasticsearch.logging.LogManager;
import org.elasticsearch.logging.Logger;
import org.elasticsearch.nativeaccess.NativeAccess;
import org.elasticsearch.nativeaccess.Systemd;

import java.io.Closeable;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

class SystemdNotifier implements ServerProcessListener, Closeable {

private static final Logger logger = LogManager.getLogger(SystemdNotifier.class);
private static final Systemd systemd = NativeAccess.instance().systemd();
private final Timer timer;

SystemdNotifier() {
this.timer = new Timer("systemd-notifier", true);
long timeBetweenExtend = TimeValue.timeValueSeconds(15).millis();
TimerTask extendTask = new TimerTask() {
@Override
public void run() {
systemd.notify_extend_timeout(30);
}
};
timer.scheduleAtFixedRate(extendTask, timeBetweenExtend, timeBetweenExtend);
}

@Override
public void onReady() {
timer.cancel();
systemd.notify_ready();
}

@Override
public void close() throws IOException {
timer.cancel();
systemd.notify_stopping();
}
}