Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix shutdown in --console mode #14621

Merged
merged 3 commits into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
devel
-----

* Send a keystroke to arangod's stdin when a shutdown command is received via
the REST API `/_admin/shutdown` and the server is started with the
`--console` argument. The keystroke will exit the blocking read loop that
is waiting on console input and that otherwise blocks the shutdown.
The implementation is based on ioctl and is thus only present on Linux and
MacOS.

* Add optimization rule for AqlAnalyzer

* Append physical compaction of log collection to every Raft log
Expand Down
30 changes: 27 additions & 3 deletions arangod/RestServer/ConsoleFeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "ConsoleFeature.h"

#include "ApplicationFeatures/ApplicationServer.h"
#include "Basics/debugging.h"
#include "Basics/messages.h"
#include "FeaturePhases/AgencyFeaturePhase.h"
#include "Logger/LogMacros.h"
Expand All @@ -39,15 +40,19 @@

#include <iostream>

#ifndef _WIN32
#include <sys/ioctl.h>
#include <unistd.h>
#endif

using namespace arangodb::application_features;
using namespace arangodb::options;

namespace arangodb {

ConsoleFeature::ConsoleFeature(application_features::ApplicationServer& server)
: ApplicationFeature(server, "Console"),
_operationMode(OperationMode::MODE_SERVER),
_consoleThread(nullptr) {
_operationMode(OperationMode::MODE_SERVER) {
startsAfter<AgencyFeaturePhase>();
}

Expand All @@ -65,10 +70,29 @@ void ConsoleFeature::start() {
auto& sysDbFeature = server().getFeature<arangodb::SystemDatabaseFeature>();
auto database = sysDbFeature.use();

_consoleThread.reset(new ConsoleThread(server(), database.get()));
_consoleThread = std::make_unique<ConsoleThread>(server(), database.get());
_consoleThread->start();
}

void ConsoleFeature::beginShutdown() {
if (_operationMode != OperationMode::MODE_CONSOLE) {
return;
}

TRI_ASSERT(_consoleThread != nullptr);

_consoleThread->userAbort();

#ifndef _WIN32
if (isatty(STDIN_FILENO)) {
char c = '\n';
// send ourselves a character, so that we can get out of the blocking
// linenoise function that reads a character from the terminal
[[maybe_unused]] int res = ioctl(STDIN_FILENO, TIOCSTI, &c);
}
#endif
}

void ConsoleFeature::unprepare() {
if (_operationMode != OperationMode::MODE_CONSOLE) {
return;
Expand Down
3 changes: 3 additions & 0 deletions arangod/RestServer/ConsoleFeature.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@
#include "GeneralServer/OperationMode.h"
#include "RestServer/ConsoleThread.h"

#include <memory>

namespace arangodb {

class ConsoleFeature final : public application_features::ApplicationFeature {
public:
explicit ConsoleFeature(application_features::ApplicationServer& server);

void start() override final;
void beginShutdown() override final;
void unprepare() override final;

private:
Expand Down