Skip to content
Merged
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
62 changes: 60 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,61 @@

PuppetMaster follows [Semantic Versioning](https://semver.org/). Before 1.0,
each minor release may break the API and the ABI: the shared libraries carry
the minor version in their soname (`libpuppet_master.so.0.2`), and
`find_package(PuppetMaster 0.2)` accepts only 0.2.x.
the minor version in their soname (`libpuppet_master.so.0.3`), and
`find_package(PuppetMaster 0.3)` accepts only 0.3.x.

## Unreleased

Breaking wire change for services: upgrade a service's server and all its
clients together. A 0.3 process and a newer one on the same service do not
see each other's heartbeats, so calls between them fail at once with
`Unavailable`. Topics and their data are unchanged; 0.3 and newer processes
still exchange topic messages, and the shared-memory layout version is the
same.

### Added

- **Per-client reply topics.** Each service client reads its own reply topic,
`<service>/reply/<client id in 16 hex digits>`, instead of every client
reading every reply. The server opens a writer per client on its first
reply and closes it after `ServerOptions::reply_idle_timeout` (30 s) idle.
`service::ReplyEndpointFor` names a client's topic. See
[Reply Topics](docs/services.md#reply-topics).
- **Reader-owned topics.** `core::TopicSpec::lifetime` with
`TopicLifetime::kReaderOwned`: the reader creates the topic, writers only
attach and get `NotFound` once it is gone, and on shared memory the segment
is removed when the reader closes, or by the last writer after the reader
crashed. See [Endpoint Configuration](docs/transport.md#endpoint-configuration).
- `shm::Options::topic_prefixes` sizes every topic under a name. A service's
deployment settings cover its per-client reply topics this way.
- `RuntimeContext::FindTransport(core::TransportKind)`.

### Changed

- Breaking wire: service header version 2, and the presence descriptor is
`service.Presence.v2` version 2.
- Breaking API: `core::TopicSpec` has a new last field, `lifetime`;
`ServerOptions` a new field, `reply_idle_timeout`.
- `ServiceEndpoints::reply` is the base of the per-client reply topics; no
process reads or writes `<service>/reply` itself any more.
- `Client::Close()` also releases the client's reply topic, not only its
destructor.

### Fixed

- Every transport now frees a topic in this process when its last reader and
writer close. Before, in-memory and shared-memory transports kept every
topic they had opened, and FastDDS every DDS topic, until the transport
closed, so a process opening many short-lived topics grew without bound.

## 0.3.0

Rebuild everything that links PuppetMaster: the soname is now
`libpuppet_master.so.0.3`, and `find_package(PuppetMaster 0.2)` no longer
finds this release. The shared-memory layout, the service wire protocol, and
the JSON configuration of 0.2 are unchanged, so 0.2 and 0.3 processes still
talk to each other, and 0.2 configurations still load.

### Added

- **Scheduled servers.** `TriggerKind::kService` runs a component when a
Expand Down Expand Up @@ -54,6 +104,14 @@ the minor version in their soname (`libpuppet_master.so.0.2`), and
- The vehicle pipeline example starts its system with
`puppet_master_launch`.

### Fixed

- With `-DBUILD_SHARED_LIBS=ON` on Linux and macOS, the installed tools
failed to start unless `LD_LIBRARY_PATH` pointed at the install's `lib`
directory (since 0.2.0). Installed tools and libraries now carry a
relative RPATH (`$ORIGIN/../lib` and `$ORIGIN`), so they work from any
prefix, including a moved one. Set `CMAKE_INSTALL_RPATH` to override.

## 0.2.0

### Added
Expand Down
20 changes: 18 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.16)

project(PuppetMaster
VERSION 0.2.0
VERSION 0.3.0
DESCRIPTION "A C++ real-time middleware runtime for message-driven components and deterministic task scheduling."
HOMEPAGE_URL "https://github.com/SoleyRan/PuppetMaster"
LANGUAGES CXX
Expand All @@ -21,7 +21,8 @@ else()
endif()

# Before 1.0, every minor release may break the ABI and the API (0.2 added a
# virtual function to transport::Writer, for example), so the shared library
# virtual function to transport::Writer, 0.3 fields to TriggerSpec and
# NodeManifest, for example), so the shared library
# soname and the CMake package compatibility both include the minor version.
# From 1.0 on, only a major release breaks them.
if(PROJECT_VERSION_MAJOR EQUAL 0)
Expand Down Expand Up @@ -100,6 +101,21 @@ if(PUPPETMASTER_ENABLE_GOODLOG AND NOT TARGET GoodLog::good_log)
endif()
endif()

# Installed shared libraries find each other, and installed tools find them,
# without LD_LIBRARY_PATH, wherever the prefix is moved: libraries look next
# to themselves, tools in the lib directory next to bin. Windows needs
# nothing, because the DLLs are installed next to the tools in bin. Set
# CMAKE_INSTALL_RPATH to override.
if(PUPPETMASTER_IS_TOP_LEVEL AND NOT WIN32 AND NOT DEFINED CMAKE_INSTALL_RPATH)
if(APPLE)
set(PUPPETMASTER_RPATH_ORIGIN "@loader_path")
else()
set(PUPPETMASTER_RPATH_ORIGIN "$ORIGIN")
endif()
file(RELATIVE_PATH PUPPETMASTER_LIB_FROM_BIN "${CMAKE_INSTALL_FULL_BINDIR}" "${CMAKE_INSTALL_FULL_LIBDIR}")
set(CMAKE_INSTALL_RPATH "${PUPPETMASTER_RPATH_ORIGIN}" "${PUPPETMASTER_RPATH_ORIGIN}/${PUPPETMASTER_LIB_FROM_BIN}")
endif()

add_subdirectory(src)

if(PUPPETMASTER_BUILD_DEMOS)
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ shared-memory topics (with the shm adapter too). See
Add `-DBUILD_SHARED_LIBS=ON` for shared libraries.

Before 1.0 each minor release may break the API and the ABI, so the shared
libraries' soname includes the minor version (`libpuppet_master.so.0.2`) and
libraries' soname includes the minor version (`libpuppet_master.so.0.3`) and
the package accepts only the same minor version. Rebuild dependents when
upgrading; see [CHANGELOG](CHANGELOG.md).

Expand All @@ -126,7 +126,7 @@ upgrading; see [CHANGELOG](CHANGELOG.md).
After installation, another CMake project can consume PuppetMaster with:

```cmake
find_package(PuppetMaster 0.2 CONFIG REQUIRED)
find_package(PuppetMaster 0.3 CONFIG REQUIRED)
target_link_libraries(my_component PRIVATE PuppetMaster::PuppetMaster)
```

Expand Down
46 changes: 39 additions & 7 deletions docs/services.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ A service named `/map/query` uses:
| Topic | Writer | Reader | Policy | Carries |
|---|---|---|---|---|
| `/map/query/request` | clients | server | queued | header + request payload |
| `/map/query/reply` | server | clients | queued | header + reply payload |
| `/map/query/reply/<client>` | server | that client | queued | header + reply payload |
| `/map/query/presence` | server | clients | latest | server instance id, heartbeat |

The request and reply descriptors are those of the payload types, with the
Expand All @@ -67,13 +67,35 @@ subscriber of the same type cannot read them by accident. Their versions
carry over, so a client built against another version of the request type is
refused when it is created.

### Reply Topics

Each client has its own reply topic, named after the service with
`/reply/` and its client id in 16 hex digits, for example
`/map/query/reply/3f2a9c0d11e4b706`. `service::ReplyEndpointFor(spec,
client_id)` returns it. A client receives only its own replies, so the cost
of a reply does not grow with the number of clients, and a busy client cannot
push another client's replies out of a shared queue.

The topic is reader-owned (`core::TopicLifetime::kReaderOwned`): the client
creates it, and `Client::Close()` removes it. The server opens a writer for a
client on its first reply to that client and keeps it, then closes it after
`ServerOptions::reply_idle_timeout` (30 s) without a reply. On shared memory,
that close also removes the segment of a client that crashed, so no reply
segments are left in `/dev/shm`.

On FastDDS, a new writer reaches the client's reader only once discovery has
matched them. The server waits for that once per client, at most 1 s or
until the request's deadline. A reply that cannot reach its client, because
the client closed or was never matched, is dropped, and the client times out
with `DeadlineExceeded`.

### Wire Header

Every request and reply starts with a fixed 32-byte header, little-endian:

```text
u32 magic "PMRQ" / "PMRP"
u16 header_version 1
u16 header_version 2 (1 in 0.3 and earlier, with one shared reply topic)
u16 status reply only: core::StatusCode of the handler result
u64 client_id random per client instance
u64 request_id per-client counter, starting at 1
Expand All @@ -83,6 +105,13 @@ u64 budget_ns request only: how long the client waits; 0 = no limit
A reply with a non-OK status carries the handler's error message as its
payload, so the client returns that status with its message.

The presence topic's descriptor is `service.Presence.v2` version 2. A 0.3
process uses `service.Presence` version 1, so processes of the two protocol
versions never see each other's heartbeats: a call between them fails at once
with `Unavailable`, and on shared memory the second endpoint on the presence
topic is refused when it is created. Upgrade a service's server and clients
together.

### Presence

The server writes `{u64 instance_id, u64 sequence}` on the presence topic at
Expand Down Expand Up @@ -291,8 +320,9 @@ returns `FailedPrecondition`.

Interface files may declare `services` next to `topics`, so the team that
serves a service publishes its contract once and every caller imports it.
Deployment files place them by wire name; the settings apply to all three of
the service's topics, and `shm::OptionsFromProject` sizes them:
Deployment files place them by wire name; the settings apply to all of the
service's topics, including every client's reply topic, and
`shm::OptionsFromProject` sizes them:

```json
{"services": [{"name": "/map/query", "transport": "shm",
Expand Down Expand Up @@ -341,9 +371,11 @@ because the map cannot get the worker the planner holds.

- Requests and replies are bounded by the transport's message size; on
shared memory, by the topic's `slot_payload_size`.
- Every client receives every reply on the service and drops the ones that
are not its own, so very many concurrent clients cost extra copies. This is
the price of running on any pub/sub transport without changes.
- Each client's reply topic costs one ring on shared memory (the service's
`slot_payload_size` x `slot_count`), and one DDS topic and writer per client
on FastDDS, while the client is in use and for `reply_idle_timeout` after.
- The first reply to a new client on FastDDS waits for discovery, usually a
few tens of milliseconds.
- A request queue that overflows drops the oldest requests, which their
clients see as `DeadlineExceeded`. Size the request topic's queue depth
for the expected burst.
21 changes: 20 additions & 1 deletion docs/shm-transport.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ namespace shm = puppet_master::transport::shm;
shm::Options options;
options.domain_id = 3; // isolates deployments
options.topics["/lidar/points"] = shm::TopicOptions {4 << 20, 8}; // 4 MiB x 8 slots
options.topic_prefixes["/camera/"] = shm::TopicOptions {8 << 20, 4}; // every topic under /camera/

auto status = shm::RegisterTransport(*runtime, name, options);
```

`ForTopic` takes an exact `topics` entry first, then the longest matching
`topic_prefixes` key, then `default_topic`.

Topics use `core::TransportKind::kIpc`. In configuration files the transport
may be written as `"ipc"` or `"shm"`.

Expand Down Expand Up @@ -67,7 +71,9 @@ options.register_project_transports = [](runtime::RuntimeContext& context, const
```

Because every process reads the same deployment file, they agree on the
geometry of each topic.
geometry of each topic. A service's settings size its request and presence
topics by name and its per-client reply topics by the prefix
`<service>/reply/`.

## Zero-Copy Writes

Expand Down Expand Up @@ -149,6 +155,19 @@ shm::RemoveSegment(domain_id, "/lidar/points");
or remove `/dev/shm/pm.<domain>.<hash>` by hand. On Windows, a segment is freed
when the last process closes it.

Within a process, a topic's segment is unmapped and its participant entry
released when the last reader or writer on it closes.

A reader-owned topic (`core::TopicLifetime::kReaderOwned`, used for service
reply topics) belongs to its reader. The reader creates the segment. Writers
only attach to it, and get `NotFound` if it does not exist. When the reader
closes, it marks the segment retired and removes its name. Writers still
attached then get `NotFound` on every write, and a new reader under the same
name starts a fresh segment. If the reader crashes, the last writer to
close removes the name once no live process is attached, so no segment is
left behind. The retired flag uses a header word that was reserved before,
so the layout version is unchanged.

### Listing And Cleaning Up

`puppet_master_config shm` inspects this host's segments without attaching to
Expand Down
14 changes: 14 additions & 0 deletions docs/transport.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ identifies the payload contract used by a reader or writer: type name,
encoding, and an optional version. Every backend rejects an endpoint whose
descriptor differs from the one already open on that topic.

`TopicSpec::lifetime` says who a topic belongs to:

- `kShared` (default): any endpoint creates the topic.
- `kReaderOwned`: the topic belongs to its one reader, such as a service
client's reply topic. The reader creates it. Writers only attach, and fail
with `NotFound` when the reader has not created it or has closed it. What
the topic leaves outside the process, a shared-memory segment, is removed
when the reader closes. In memory and in shared memory. FastDDS leaves
nothing behind and cannot tell a missing reader from an undiscovered one,
so it treats the topic as shared; check `MatchedReaderCount` there.

Every backend frees a topic, in this process, when its last reader and writer
close, so a process can open many short-lived topics.

## Reader And Writer

`Reader` and `Writer` are pure interfaces:
Expand Down
2 changes: 1 addition & 1 deletion examples/vehicle_pipeline/estimator_node/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ project(EstimatorNode VERSION 1.0.0 LANGUAGES CXX)

include(GNUInstallDirs)

find_package(PuppetMaster 0.2 CONFIG REQUIRED)
find_package(PuppetMaster 0.3 CONFIG REQUIRED)
find_package(EstimationInterfaces 1 CONFIG REQUIRED)

add_executable(estimator_node src/main.cpp)
Expand Down
2 changes: 1 addition & 1 deletion examples/vehicle_pipeline/monitor_node/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ project(MonitorNode VERSION 1.0.0 LANGUAGES CXX)

include(GNUInstallDirs)

find_package(PuppetMaster 0.2 CONFIG REQUIRED)
find_package(PuppetMaster 0.3 CONFIG REQUIRED)
find_package(EstimationInterfaces 1 CONFIG REQUIRED)

add_executable(monitor_node src/main.cpp)
Expand Down
4 changes: 2 additions & 2 deletions examples/vehicle_pipeline/sensor_interfaces/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ include(CMakePackageConfigHelpers)
set(WHEEL_SPEED_VERSION 2 CACHE STRING "vehicle.WheelSpeed wire format version")
set(IMU_SAMPLE_VERSION 1)

find_package(PuppetMaster 0.2 CONFIG REQUIRED)
find_package(PuppetMaster 0.3 CONFIG REQUIRED)

set(GENERATED "${CMAKE_CURRENT_BINARY_DIR}/generated")
configure_file(include/sensor_interfaces/messages.h.in "${GENERATED}/include/sensor_interfaces/messages.h" @ONLY)
Expand All @@ -38,7 +38,7 @@ install(EXPORT SensorInterfacesTargets NAMESPACE SensorInterfaces::
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/SensorInterfaces")
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/SensorInterfacesConfig.cmake" [=[
include(CMakeFindDependencyMacro)
find_dependency(PuppetMaster 0.2 CONFIG)
find_dependency(PuppetMaster 0.3 CONFIG)
include("${CMAKE_CURRENT_LIST_DIR}/SensorInterfacesTargets.cmake")
]=])
write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/SensorInterfacesConfigVersion.cmake"
Expand Down
2 changes: 1 addition & 1 deletion examples/vehicle_pipeline/sensor_node/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ project(SensorNode VERSION 1.0.0 LANGUAGES CXX)

include(GNUInstallDirs)

find_package(PuppetMaster 0.2 CONFIG REQUIRED)
find_package(PuppetMaster 0.3 CONFIG REQUIRED)
find_package(SensorInterfaces 2 CONFIG REQUIRED)

add_executable(sensor_node src/main.cpp)
Expand Down
26 changes: 26 additions & 0 deletions include/puppet_master/core/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ enum class DependencyPolicy : std::uint8_t {
kAny,
};

// Who a topic belongs to.
enum class TopicLifetime : std::uint8_t {
// Any endpoint creates the topic; it outlives them all.
kShared = 0,
// The topic belongs to its one reader, such as a service client's reply
// topic: the reader creates it, writers only attach to it and fail with
// NotFound when the reader is gone, and what the topic leaves outside the
// process (a shared-memory segment) is removed when the reader closes.
// In memory and in shared memory. FastDDS leaves nothing behind and
// cannot tell a missing reader from an undiscovered one, so it treats
// the topic as shared; use Writer::MatchedReaderCount there.
kReaderOwned,
};

namespace detail {

inline std::string CopyStringView(std::string_view value)
Expand Down Expand Up @@ -105,6 +119,17 @@ inline const char* ToString(TriggerKind kind) noexcept
return "unknown";
}

inline const char* ToString(TopicLifetime lifetime) noexcept
{
switch (lifetime) {
case TopicLifetime::kShared:
return "shared";
case TopicLifetime::kReaderOwned:
return "reader_owned";
}
return "unknown";
}

inline Status ValidateName(std::string_view kind, std::string_view value)
{
if (value.empty()) {
Expand Down Expand Up @@ -213,6 +238,7 @@ struct TopicSpec {
TopicName name;
TransportKind transport {TransportKind::kInMemory};
MessagePolicy message_policy;
TopicLifetime lifetime {TopicLifetime::kShared};

Status Validate() const
{
Expand Down
2 changes: 2 additions & 0 deletions include/puppet_master/runtime/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class RuntimeContext final {

core::Status RegisterTransport(transport::TransportPtr transport);
core::Result<transport::TransportPtr> FindTransport(const core::TransportName& name) const;
// The transport CreateReader and CreateWriter use for topics of `kind`.
core::Result<transport::TransportPtr> FindTransport(core::TransportKind kind) const;
std::vector<core::TransportName> ListTransportNames() const;

core::Status RegisterComponent(ComponentSpec spec);
Expand Down
Loading
Loading