Mads
Multi-Agent Distributed System
Loading...
Searching...
No Matches
Mads::detail Namespace Reference

Namespaces

namespace  fd_limit_impl
 

Classes

struct  FdLimitOutcome
 The result of acting on a plan. More...
 
struct  FdLimitPlan
 
struct  FdLimits
 
struct  SocketOptions
 
struct  WireHeader
 

Enumerations

enum class  Comp : uint8_t { None = 0 , Snappy = 1 }
 

Functions

const std::string & clock_domain_id ()
 Stable identifier for the clock this process reads. Computed once (function-local static, thread-safe init) and cached for the life of the process.
 
uint64_t agent_capacity (uint64_t soft)
 How many agents a given soft limit leaves room for.
 
FdLimits query_fd_limits ()
 Reads the limits currently in force, with hard clamped as described above.
 
std::string describe_fd_limits (const FdLimits &limits)
 Renders e.g. "1024 soft / 1048576 hard, about 492 agents".
 
std::string fd_limit_hint ()
 The hint appended to every message that reports a limit worth raising.
 
FdLimitPlan plan_fd_limit (std::optional< int64_t > requested, const FdLimits &limits)
 
FdLimitOutcome apply_fd_limit (const FdLimitPlan &plan)
 
bool is_fd_exhaustion (int err)
 
mongocxx::instance & mongo_instance ()
 Return the process-wide mongocxx driver instance.
 
uint64_t fnv1a64 (const void *data, size_t len)
 
std::string digest_hex (std::string_view bytes)
 
std::filesystem::path attachment_cache_root ()
 
void sweep_stale (const std::filesystem::path &agent_dir, std::string_view keep_digest, const std::string &name, const std::string &ext)
 
std::filesystem::path store_attachment (const std::string &name, const std::string &ext, const std::string &bytes)
 Persist a broker-served attachment under its content digest and return the path to load it from.
 
template<typename T >
std::optional< T > resolve_setting (toml::node_view< toml::node > fleet_cfg, toml::node_view< toml::node > agent_cfg, std::string_view key)
 
std::string make_wire_header (WireFormat fmt, Comp comp, bool has_blob)
 
bool parse_wire_header (const std::string &part, WireHeader &out)
 
Comp resolve_compression (Compression policy, size_t size)
 
std::string encode_payload (const nlohmann::json &j, WireFormat fmt)
 
bool decode_to_json_text (const std::string &raw, uint8_t format, uint8_t comp, std::string &json_text_out)
 

Variables

constexpr uint64_t FD_PER_AGENT = 2
 
constexpr uint64_t FD_BROKER_OVERHEAD = 64
 
constexpr uint64_t FD_LOW_WATERMARK = 1024
 
constexpr uint64_t FD_ABSOLUTE_CEILING = 1048576
 
constexpr char WIRE_MAGIC [4] = {'M', 'A', 'D', 'S'}
 
constexpr uint8_t WIRE_HDR_VERSION = 1
 
constexpr uint8_t WIRE_FLAG_BLOB = 0x01
 
constexpr size_t WIRE_HEADER_SIZE
 

Enumeration Type Documentation

◆ Comp

enum class Mads::detail::Comp : uint8_t
strong
Enumerator
None 
Snappy 

Definition at line 46 of file wire_format.hpp.

Function Documentation

◆ agent_capacity()

uint64_t Mads::detail::agent_capacity ( uint64_t  soft)
inline

How many agents a given soft limit leaves room for.

Definition at line 76 of file fd_limit.hpp.

References FD_BROKER_OVERHEAD, and FD_PER_AGENT.

Referenced by describe_fd_limits(), and plan_fd_limit().

◆ apply_fd_limit()

FdLimitOutcome Mads::detail::apply_fd_limit ( const FdLimitPlan plan)
inline

Carries out plan. Only ever moves the soft limit: rlim_max is left untouched, since raising it needs CAP_SYS_RESOURCE and would simply fail for an unprivileged broker, while moving the soft limit anywhere at or below the hard one never needs privileges at all – in either direction.

Lowering does not close descriptors that are already open; it only makes further allocations fail. Since the broker applies this before binding anything, that distinction does not arise in practice.

Definition at line 318 of file fd_limit.hpp.

References Mads::detail::FdLimitPlan::action, Mads::detail::FdLimitOutcome::applied, Mads::detail::FdLimitOutcome::error, Mads::detail::FdLimitOutcome::limits, Mads::detail::FdLimitPlan::Lower, query_fd_limits(), Mads::detail::FdLimitPlan::Raise, Mads::detail::FdLimits::supported, and Mads::detail::FdLimitPlan::target.

◆ attachment_cache_root()

std::filesystem::path Mads::detail::attachment_cache_root ( )
inline

Definition at line 86 of file plugin_cache.hpp.

Referenced by store_attachment().

◆ clock_domain_id()

const std::string & Mads::detail::clock_domain_id ( )
inline

Stable identifier for the clock this process reads. Computed once (function-local static, thread-safe init) and cached for the life of the process.

Definition at line 45 of file clock_domain.hpp.

References HOST_NAME_MAX.

◆ decode_to_json_text()

bool Mads::detail::decode_to_json_text ( const std::string &  raw,
uint8_t  format,
uint8_t  comp,
std::string &  json_text_out 
)
inline

Definition at line 115 of file wire_format.hpp.

References Snappy.

◆ describe_fd_limits()

std::string Mads::detail::describe_fd_limits ( const FdLimits limits)
inline

Renders e.g. "1024 soft / 1048576 hard, about 492 agents".

Definition at line 187 of file fd_limit.hpp.

References agent_capacity(), Mads::detail::FdLimits::hard, and Mads::detail::FdLimits::soft.

Referenced by plan_fd_limit().

◆ digest_hex()

std::string Mads::detail::digest_hex ( std::string_view  bytes)
inline

Definition at line 74 of file plugin_cache.hpp.

References fnv1a64().

Referenced by store_attachment().

◆ encode_payload()

std::string Mads::detail::encode_payload ( const nlohmann::json &  j,
WireFormat  fmt 
)
inline

Definition at line 104 of file wire_format.hpp.

◆ fd_limit_hint()

std::string Mads::detail::fd_limit_hint ( )
inline

The hint appended to every message that reports a limit worth raising.

Definition at line 194 of file fd_limit.hpp.

Referenced by plan_fd_limit().

◆ fnv1a64()

uint64_t Mads::detail::fnv1a64 ( const void *  data,
size_t  len 
)
inline

Definition at line 62 of file plugin_cache.hpp.

Referenced by digest_hex().

◆ is_fd_exhaustion()

bool Mads::detail::is_fd_exhaustion ( int  err)
inline

True when err is the errno of a process (or system) descriptor table that has filled up – the condition every explanatory message here exists for.

Definition at line 346 of file fd_limit.hpp.

◆ make_wire_header()

std::string Mads::detail::make_wire_header ( WireFormat  fmt,
Comp  comp,
bool  has_blob 
)
inline

Definition at line 56 of file wire_format.hpp.

References WIRE_FLAG_BLOB, WIRE_HDR_VERSION, WIRE_HEADER_SIZE, and WIRE_MAGIC.

◆ mongo_instance()

mongocxx::instance & Mads::detail::mongo_instance ( )
inline

Return the process-wide mongocxx driver instance.

mongocxx::instance must be constructed exactly once per process and must outlive every other driver object; constructing a second one while the first is alive throws mongocxx::logic_error{k_cannot_recreate_instance}. Holding one as a class member therefore makes it impossible for two driver-using MADS objects to coexist, so every translation unit that touches the driver shares the single function-local static created here.

The static is destroyed at process exit, after all clients built on top of it.

Note
This is inline, so all callers linked into the same binary share one static. MadsCore is a single shared library, so that holds for MADS.

Definition at line 36 of file mongo_instance.hpp.

◆ parse_wire_header()

◆ plan_fd_limit()

◆ query_fd_limits()

FdLimits Mads::detail::query_fd_limits ( )
inline

Reads the limits currently in force, with hard clamped as described above.

Definition at line 132 of file fd_limit.hpp.

References FD_ABSOLUTE_CEILING, Mads::detail::FdLimits::hard, Mads::detail::fd_limit_impl::kernel_fd_ceiling(), Mads::detail::FdLimits::soft, and Mads::detail::FdLimits::supported.

Referenced by apply_fd_limit().

◆ resolve_compression()

Comp Mads::detail::resolve_compression ( Compression  policy,
size_t  size 
)
inline

Definition at line 91 of file wire_format.hpp.

References None, and Snappy.

◆ resolve_setting()

template<typename T >
std::optional< T > Mads::detail::resolve_setting ( toml::node_view< toml::node >  fleet_cfg,
toml::node_view< toml::node >  agent_cfg,
std::string_view  key 
)

Definition at line 24 of file socket_options.hpp.

◆ store_attachment()

std::filesystem::path Mads::detail::store_attachment ( const std::string &  name,
const std::string &  ext,
const std::string &  bytes 
)
inline

Persist a broker-served attachment under its content digest and return the path to load it from.

Safe to call concurrently from any number of processes and threads: the bytes at the returned path are never mutated in place, so a copy another process has already dlopen()'d stays exactly as that process mapped it.

Parameters
nameSettings section name; also the returned file's stem.
extExtension without the leading dot (e.g. "plugin").
bytesThe attachment as received from the broker.
Returns
Absolute path to the cached file.
Exceptions
std::runtime_errorif the attachment cannot be persisted.

Definition at line 134 of file plugin_cache.hpp.

References attachment_cache_root(), digest_hex(), and sweep_stale().

◆ sweep_stale()

void Mads::detail::sweep_stale ( const std::filesystem::path &  agent_dir,
std::string_view  keep_digest,
const std::string &  name,
const std::string &  ext 
)
inline

Definition at line 96 of file plugin_cache.hpp.

Referenced by store_attachment().

Variable Documentation

◆ FD_ABSOLUTE_CEILING

constexpr uint64_t Mads::detail::FD_ABSOLUTE_CEILING = 1048576
inlineconstexpr

Last-resort ceiling when the OS reports an unbounded hard limit and no kernel cap could be read. Matches Linux's own default fs.nr_open.

Definition at line 73 of file fd_limit.hpp.

Referenced by query_fd_limits().

◆ FD_BROKER_OVERHEAD

constexpr uint64_t Mads::detail::FD_BROKER_OVERHEAD = 64
inlineconstexpr

The broker's own descriptor footprint, independent of fleet size: three listening sockets, ~10 libzmq socket mailboxes, the context reaper's and each I/O thread's poller, stdio, and the settings-file watch.

Platform-dependent, because the mailboxes dominate it and their cost is not the same everywhere: libzmq's signaler uses eventfd() where it exists (Linux), costing one descriptor per mailbox, and falls back to a socketpair – two descriptors – everywhere else. Measured at idle: ~34 on Linux, 49 on macOS. Rounded up in both cases, so the capacity estimate errs toward understating how many agents will fit rather than overstating.

Definition at line 63 of file fd_limit.hpp.

Referenced by agent_capacity(), and plan_fd_limit().

◆ FD_LOW_WATERMARK

constexpr uint64_t Mads::detail::FD_LOW_WATERMARK = 1024
inlineconstexpr

Soft limit at or below which raising is worth suggesting – the default on essentially every Linux distribution, and the value systemd hands a unit that does not set LimitNOFILE=.

Definition at line 69 of file fd_limit.hpp.

Referenced by plan_fd_limit().

◆ FD_PER_AGENT

constexpr uint64_t Mads::detail::FD_PER_AGENT = 2
inlineconstexpr

Descriptors a single connected agent costs the broker for as long as it stays connected: one for its publisher, one for its subscriber. (The settings request needs a third, but only while it is in flight.)

Definition at line 48 of file fd_limit.hpp.

Referenced by agent_capacity().

◆ WIRE_FLAG_BLOB

constexpr uint8_t Mads::detail::WIRE_FLAG_BLOB = 0x01
constexpr

Definition at line 41 of file wire_format.hpp.

Referenced by make_wire_header(), and parse_wire_header().

◆ WIRE_HDR_VERSION

constexpr uint8_t Mads::detail::WIRE_HDR_VERSION = 1
constexpr

Definition at line 40 of file wire_format.hpp.

Referenced by make_wire_header().

◆ WIRE_HEADER_SIZE

constexpr size_t Mads::detail::WIRE_HEADER_SIZE
constexpr
Initial value:
= 4 + 1 + 1 +
1 + 1 +
4

Definition at line 42 of file wire_format.hpp.

Referenced by make_wire_header(), and parse_wire_header().

◆ WIRE_MAGIC

constexpr char Mads::detail::WIRE_MAGIC[4] = {'M', 'A', 'D', 'S'}
constexpr

Definition at line 39 of file wire_format.hpp.

Referenced by make_wire_header(), and parse_wire_header().