Skip to content

Commit d09a5c2

Browse files
authored
Merge pull request #1216 from evoskuil/master
Style, pool updates.
2 parents 26f8262 + a5bf7d1 commit d09a5c2

161 files changed

Lines changed: 1051 additions & 970 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎console/executor.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ void executor::poll_for_stopping()
120120
poller_thread_.emplace(std::thread([]()
121121
{
122122
// Report start progress each second, until running or canceled, as
123-
// the manager otherwise assumes a hang (never entered in console mode).
123+
// manager otherwise assumes hang (never entered in console mode).
124124
for (size_t count{}; !canceled(); ++count)
125125
{
126126
std::this_thread::sleep_for(std::chrono::milliseconds(100));
@@ -130,8 +130,8 @@ void executor::poll_for_stopping()
130130

131131
stopping_.set_value(true);
132132

133-
// The node drain exceeds the manager's patience, so report progress
134-
// until the service reports stopped (never entered in console mode).
133+
// Node drain exceeds manager's patience, so report progress until
134+
// service reports stopped (never entered in console mode).
135135
while (service_ && !exited_.load())
136136
{
137137
std::this_thread::sleep_for(std::chrono::seconds(1));

‎console/executor.hpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ class executor
4848
// Called from main.
4949
bool dispatch();
5050

51-
// Called from main, blocks until stopped if started by the service
52-
// control manager, otherwise returns false (and is a noop on posix).
51+
// Called from main, blocks until stopped if started by service control
52+
// manager, otherwise returns false (noop on posix).
5353
static bool service(parser& metadata, std::istream& input,
5454
std::ostream& error);
5555

‎console/executor_commands.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ bool executor::dispatch()
239239
if (config.help)
240240
return do_help();
241241

242-
// Order below matches help output (alphabetical), so that first option is
243-
// executed in the case where multiple options are parsed.
242+
// Order below matches help output (alphabetical), so first option is
243+
// executed where multiple options are parsed.
244244

245245
if (config.backup)
246246
return do_backup();

‎console/executor_daemon.cpp‎

Lines changed: 54 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ constexpr uint32_t daemon_absent = ENOENT;
6060

6161
#if defined(HAVE_WINDOWS)
6262

63-
// Node drain exceeds the default shutdown window, so preshutdown is required.
64-
// This has no effect until the service accepts SERVICE_ACCEPT_PRESHUTDOWN.
63+
// Node drain exceeds default shutdown window, so preshutdown is required. No
64+
// effect until service accepts SERVICE_ACCEPT_PRESHUTDOWN.
6565
constexpr DWORD preshutdown_milliseconds = 600'000;
6666

6767
// The manager terminates a pending start that does not progress within this.
@@ -77,10 +77,10 @@ parser* executor::service_metadata_{};
7777
// Service runtime.
7878
// ----------------------------------------------------------------------------
7979

80-
// A null status handle implies console mode, in which this is a noop. Pending
81-
// states must advance the checkpoint, as the manager otherwise assumes a hang.
82-
// Reported from the manager, network, and poller threads, so holds no state
83-
// apart from the atomics that the reported status is composed from.
80+
// Null status handle implies console mode, in which this is a noop. Pending
81+
// states must advance checkpoint, as manager otherwise assumes hang. Reported
82+
// from manager, network, and poller threads, so holds no state apart from
83+
// atomics reported status is composed from.
8484
void executor::report_status(DWORD state) NOEXCEPT
8585
{
8686
if (is_null(status_handle_))
@@ -93,19 +93,22 @@ void executor::report_status(DWORD state) NOEXCEPT
9393
const auto failed = failed_.load();
9494
const DWORD code = failed ? ERROR_SERVICE_SPECIFIC_ERROR : NO_ERROR;
9595
const DWORD specific = failed ? 1u : 0u;
96+
const auto running = (state == SERVICE_RUNNING);
97+
const DWORD controls = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PRESHUTDOWN;
98+
const DWORD accepted = running ? controls : 0u;
99+
const DWORD stop_hint = stopping ? preshutdown_milliseconds : 0u;
100+
const DWORD hint = starting ? pending_milliseconds : stop_hint;
96101
state_.store(state);
97102

98103
SERVICE_STATUS status
99104
{
100105
.dwServiceType = SERVICE_WIN32_OWN_PROCESS,
101106
.dwCurrentState = state,
102-
.dwControlsAccepted = (state == SERVICE_RUNNING) ?
103-
(SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PRESHUTDOWN) : 0u,
107+
.dwControlsAccepted = accepted,
104108
.dwWin32ExitCode = code,
105109
.dwServiceSpecificExitCode = specific,
106110
.dwCheckPoint = (starting || stopping) ? ++checkpoint : 0u,
107-
.dwWaitHint = starting ? pending_milliseconds :
108-
(stopping ? preshutdown_milliseconds : 0u)
111+
.dwWaitHint = hint
109112
};
110113

111114
::SetServiceStatus(status_handle_, &status);
@@ -146,13 +149,13 @@ void WINAPI executor::service_main(DWORD, LPWSTR*) NOEXCEPT
146149
if (is_null(status_handle_))
147150
return;
148151

149-
// Suppresses the hidden window and console capture, which have no meaning
150-
// in session zero, and are otherwise created by the executor constructor.
152+
// Suppresses hidden window and console capture, which have no meaning in
153+
// session zero, otherwise created by executor constructor.
151154
service_ = true;
152155
report_status(SERVICE_START_PENDING);
153156

154-
// The service has no console, so console output is discarded. This must
155-
// outlive the executor, which retains a reference to it.
157+
// Service has no console, so console output is discarded. Must outlive
158+
// executor, which retains reference to it.
156159
static std::ostream discard{ nullptr };
157160
auto& host = executor::factory(*service_metadata_, *service_input_,
158161
discard, *service_error_);
@@ -166,9 +169,9 @@ void WINAPI executor::service_main(DWORD, LPWSTR*) NOEXCEPT
166169
report_status(SERVICE_STOPPED);
167170
}
168171

169-
// Grant the account the right to log on as a service, without which the
170-
// service control manager cannot start the service. This is idempotent, and
171-
// is not revoked on removal, as other services may rely upon it.
172+
// Grant account the right to log on as a service, without which service control
173+
// manager cannot start service. Idempotent, and not revoked on removal, as
174+
// other services may rely upon it.
172175
DWORD executor::grant_logon_right(const std::string& account) NOEXCEPT
173176
{
174177
using namespace system;
@@ -265,8 +268,8 @@ uint32_t executor::create_service(const std::filesystem::path& config,
265268
return result;
266269
}
267270

268-
// Delayed start defers the node until boot completes, so that its startup
269-
// does not contend with that of the operating system.
271+
// Delayed start defers node until boot completes, so its startup does not
272+
// contend with that of operating system.
270273
constexpr auto text = SERVICE_CONFIG_DESCRIPTION;
271274
constexpr auto start = SERVICE_CONFIG_DELAYED_AUTO_START_INFO;
272275
constexpr auto info = SERVICE_CONFIG_PRESHUTDOWN_INFO;
@@ -329,8 +332,8 @@ uint32_t executor::delete_service() NOEXCEPT
329332
#if defined(HAVE_POSIX)
330333

331334
#if defined(HAVE_LINUX) || defined(HAVE_APPLE)
332-
// The drain exceeds the default stop timeout on both platforms, and can run
333-
// to minutes, depending on dirty page volume and disk speed.
335+
// Drain exceeds default stop timeout on both platforms, and can run to minutes,
336+
// depending on dirty page volume and disk speed.
334337
constexpr auto stop_timeout_seconds = 600;
335338
#endif
336339

@@ -347,10 +350,10 @@ static std::filesystem::path enabled_path(const std::string& name) NOEXCEPT
347350
return { "/etc/systemd/system/multi-user.target.wants/" + name + ".service" };
348351
}
349352

350-
// Type=notify defers dependent units until the node reports ready, which is
351-
// the posix counterpart to reporting SERVICE_RUNNING to the manager. Restart
352-
// is not configured, as a store that fails to open does so deterministically,
353-
// and retrying only obscures the fault (as does the manager on windows).
353+
// Type=notify defers dependent units until node reports ready, posix
354+
// counterpart to reporting SERVICE_RUNNING to manager. Restart not configured,
355+
// as a store that fails to open does so deterministically, and retrying only
356+
// obscures fault (as does manager on windows).
354357
static std::string unit_text(const std::string&, const std::string& command,
355358
const std::string&, const std::string& account) NOEXCEPT
356359
{
@@ -373,21 +376,21 @@ static std::string unit_text(const std::string&, const std::string& command,
373376

374377
#elif defined(HAVE_APPLE)
375378

376-
// launchd clamps a positive ExitTimeOut to 60 seconds (600 reports as 60), so
377-
// this is the effective drain ceiling on osx. Do not "fix" this with zero,
378-
// which launchd documents as infinite but which kills without draining.
379+
// launchd clamps positive ExitTimeOut to 60 seconds (600 reports as 60), so
380+
// this is effective drain ceiling on osx. Do not "fix" this with zero, which
381+
// launchd documents as infinite but which kills without draining.
379382
constexpr auto exit_timeout_seconds = stop_timeout_seconds;
380383

381384
static std::filesystem::path unit_path(const std::string&) NOEXCEPT
382385
{
383386
return { "/Library/LaunchDaemons/" BS_SERVICE_LABEL ".plist" };
384387
}
385388

386-
// launchd has no readiness protocol, so the job is simply run at load.
387-
// KeepAlive is not configured, as it restarts upon any exit, and a store that
388-
// fails to open does so deterministically (as with the other two managers).
389-
// launchd discards console output, and the log file is buffered, so redirect
390-
// it to obtain the live view that the journal provides on linux.
389+
// launchd has no readiness protocol, so job runs at load. KeepAlive
390+
// not configured, as it restarts on any exit, and a store that fails
391+
// to open does so deterministically (as with other two managers).
392+
// launchd discards console output and log file is buffered, so
393+
// redirect it to obtain live view journal provides on linux.
391394
static std::string unit_text(const std::string&, const std::string& command,
392395
const std::string& console, const std::string& account) NOEXCEPT
393396
{
@@ -430,11 +433,11 @@ static std::filesystem::path enabled_path(const std::string& name) NOEXCEPT
430433
// Set by the rc.d script, as rc.subr exports nothing by which to detect it.
431434
constexpr auto service_variable = "BS_SERVICE";
432435

433-
// rc.subr has no readiness protocol, so daemon(8) detaches the node, records
434-
// its pid for rc.subr, and redirects console output (otherwise discarded).
435-
// Restart is not configured (as with the other managers). rc.subr stops with
436-
// SIGTERM and waits without limit, but rc.shutdown terminates the system after
437-
// rcshutdown_timeout (90 seconds by default), which may not cover the drain.
436+
// rc.subr has no readiness protocol, so daemon(8) detaches node, records its
437+
// pid for rc.subr, and redirects console output (otherwise discarded). Restart
438+
// not configured (as with other managers). rc.subr stops with SIGTERM and waits
439+
// without limit, but rc.shutdown terminates system after rcshutdown_timeout (90
440+
// seconds by default), which may not cover drain.
438441
static std::string unit_text(const std::string& name,
439442
const std::string& command, const std::string& console,
440443
const std::string& account) NOEXCEPT
@@ -470,8 +473,8 @@ static std::string unit_text(const std::string& name,
470473

471474
#endif // HAVE_LINUX
472475

473-
// Console redirection file, within the configured log directory. Distinct
474-
// from the rotated log files, which the node writes and buffers itself.
476+
// Console redirection file, within configured log directory. Distinct from
477+
// rotated log files, which node writes and buffers itself.
475478
constexpr auto console_file = "console.log";
476479

477480
// Returns an errno value, where zero is success.
@@ -506,8 +509,8 @@ uint32_t executor::create_service(const std::filesystem::path& config,
506509
return EIO;
507510

508511
#if defined(HAVE_LINUX)
509-
// This is what enabling a unit does, avoiding a systemctl invocation.
510-
// A reload is required before the unit can be started without a reboot.
512+
// What enabling a unit does, avoiding a systemctl invocation. Reload is
513+
// required before unit can be started without reboot.
511514
file.close();
512515
const auto enabled = enabled_path(name_);
513516
std::filesystem::create_directories(enabled.parent_path(), ec);
@@ -518,8 +521,8 @@ uint32_t executor::create_service(const std::filesystem::path& config,
518521
if (ec)
519522
return EACCES;
520523
#elif defined(HAVE_FREEBSD)
521-
// rc.d requires an executable script, and rc.subr sources the rc.conf.d
522-
// file of the same name, in which the script is enabled.
524+
// rc.d requires an executable script, and rc.subr sources rc.conf.d file of
525+
// same name, in which script is enabled.
523526
file.close();
524527
using perms = std::filesystem::perms;
525528
std::filesystem::permissions(unit, perms::owner_all | perms::group_read |
@@ -605,9 +608,9 @@ std::string executor::command_line(const std::filesystem::path& config) NOEXCEPT
605608

606609
#if defined(HAVE_LINUX)
607610

608-
// Notify the service manager, as defined by sd_notify(3). The socket is unset
609-
// unless the unit is Type=notify, in which case this is a noop. Implemented
610-
// here to avoid a libsystemd dependency for two datagrams.
611+
// Notify service manager, as defined by sd_notify(3). Socket is unset unless
612+
// unit is Type=notify, in which case this is a noop. Implemented here to avoid
613+
// libsystemd dependency for two datagrams.
611614
static void notify_manager(const std::string& state) NOEXCEPT
612615
{
613616
const auto path = std::getenv("NOTIFY_SOCKET");
@@ -686,8 +689,8 @@ bool executor::service(parser& metadata, std::istream& input,
686689
{ NULL, NULL }
687690
};
688691

689-
// Blocks until the service stops, and fails immediately (without any
690-
// connection attempt) when the process was not started by the manager.
692+
// Blocks until service stops, fails immediately (without any connection
693+
// attempt) when process was not started by manager.
691694
if (!is_zero(::StartServiceCtrlDispatcherW(table)))
692695
return true;
693696

@@ -696,8 +699,8 @@ bool executor::service(parser& metadata, std::istream& input,
696699

697700
#else
698701

699-
// A posix daemon runs in the foreground under its manager, so there is no
700-
// dispatcher to connect. This only detects that a manager is supervising.
702+
// Posix daemon runs in foreground under its manager, so there is no dispatcher
703+
// to connect. Only detects that a manager is supervising.
701704
bool executor::service(parser&, std::istream&, std::ostream&)
702705
{
703706
#if defined(HAVE_LINUX)

‎console/executor_events.cpp‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,9 @@ const std::unordered_map<uint8_t, std::string> executor::fired_
8686

8787
// Events.
8888
// ----------------------------------------------------------------------------
89-
// Eventing uses underling std::ostream. It is possible that these may throw
90-
// upon certain unexpected failures, such as a disconnected device. Presently
91-
// the writes are not wrapped in a try block (performance) but this can be
92-
// appplied centrally below if desired.
89+
// Eventing uses underlying std::ostream, which may throw upon unexpected
90+
// failures, such as a disconnected device. Writes are not wrapped in a try
91+
// block (performance), but this can be applied centrally below if desired.
9392

9493
executor::event_sink executor::create_event_sink() const
9594
{

‎console/executor_logging.cpp‎

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,9 @@ const std::unordered_map<uint8_t, bool> executor::defined_
4242

4343
// Logging.
4444
// ----------------------------------------------------------------------------
45-
// Logging uses underling std::ostream. It is possible that these may throw
46-
// upon certain unexpected failures, such as a disconnected device. Presently
47-
// the writes are not wrapped in a try block (performance) but this can be
48-
// appplied centrally below if desired.
45+
// Logging uses underlying std::ostream, which may throw upon unexpected
46+
// failures, such as a disconnected device. Writes are not wrapped in a try
47+
// block (performance), but this can be applied centrally below if desired.
4948

5049
executor::log_sink executor::create_log_sink() const
5150
{
@@ -107,16 +106,16 @@ void executor::subscribe_log(std::ostream* sink)
107106
output_ << prefix << message << std::endl;
108107
output_ << prefix << BS_NODE_FOOTER << std::endl;
109108

110-
// A service has no console to await, and posix captures
111-
// console output to the service log, where this is noise.
109+
// Service has no console to await, and posix captures console
110+
// output to service log, where this is noise.
112111
if (!service_)
113112
output_ << prefix << BS_NODE_TERMINATE << std::endl;
114113

115-
// Release subscription and signal termination of log.
116-
// Log destruct blocks on its own threadpool join, which occurs
117-
// only after all logging work is completed. After the above
118-
// message is queued, no more are accepted, and the executor
119-
// may initiate its own (and thereby this log's) destruction.
114+
// Release subscription and signal termination of log. Log
115+
// destruct blocks on its own threadpool join, which occurs only
116+
// after all logging work completes. After above message is
117+
// queued, no more are accepted, and executor may initiate its
118+
// own (and thereby this log's) destruction.
120119
stopped_.set_value(true);
121120
return false;
122121
}

‎console/executor_options.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,8 @@ void executor::subscribe_capture()
246246
{
247247
using namespace system;
248248

249-
// This is not on a network thread, so the node may call close() while this
250-
// is running a backup (for example), resulting in a try_lock warning loop.
249+
// Not on a network thread, so node may call close() while this is running a
250+
// backup (for example), resulting in try_lock warning loop.
251251
capture_.subscribe(
252252
[&](const code& ec, const std::string& line)
253253
{

‎console/executor_runner.cpp‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ void executor::subscribe_connect()
6262
},
6363
[&](const code&, network::net::object_key)
6464
{
65-
// By not handling it is possible stop could fire before complete.
66-
// But the handler is not required for termination, so this is ok.
67-
// The error code in the handler can be used to differentiate.
65+
// By not handling, stop could fire before complete. But handler is
66+
// not required for termination, so this is ok. Error code in
67+
// handler can be used to differentiate.
6868
}
6969
);
7070
}
@@ -89,9 +89,9 @@ void executor::subscribe_close()
8989
},
9090
[&](const code&, network::net::object_key)
9191
{
92-
// By not handling it is possible stop could fire before complete.
93-
// But the handler is not required for termination, so this is ok.
94-
// The error code in the handler can be used to differentiate.
92+
// By not handling, stop could fire before complete. But handler is
93+
// not required for termination, so this is ok. Error code in
94+
// handler can be used to differentiate.
9595
}
9696
);
9797
}

‎console/executor_scans.cpp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,16 +388,16 @@ void executor::scan_collisions()
388388
query_.outs_buckets(), store_.outs.filter_k(), distributions, filters,
389389
[&](size_t index)
390390
{
391-
return table::outs::key{ query_.get_outs_key(possible_narrow_cast<
392-
outs_link::integer>(index)) };
391+
const auto link = possible_narrow_cast<outs_link::integer>(index);
392+
return table::outs::key{ query_.get_outs_key(link) };
393393
});
394394

395395
scan_collisions<table::wtxid>("wtxid", query_.wtxid_records(),
396396
query_.wtxid_buckets(), store_.wtxid.filter_k(), distributions,
397397
filters, [&](size_t index)
398398
{
399-
return table::wtxid::key{ query_.get_wtxid_key(
400-
possible_narrow_cast<pool_link::integer>(index)) };
399+
const auto link = possible_narrow_cast<pool_link::integer>(index);
400+
return table::wtxid::key{ query_.get_wtxid_key(link) };
401401
});
402402

403403
logger(BS_COLLISIONS_DISTRIBUTION);

0 commit comments

Comments
 (0)