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
2 changes: 2 additions & 0 deletions CHANGELOG.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ English · [简体中文](CHANGELOG.md)

## Unreleased

- Honor explicit whole-Session pending-age checks on device submissions. Add `PendingAgeScope::kDefault` to retain endpoint-local device defaults and whole-Session legacy defaults without changing existing enum values or signatures. Add scope, owned-operation, replacement, cooldown and installed-consumer regressions.

- Add shared runtime channel ownership, device rebind and coordinated recovery, owned-operation close permits, per-device pacing and failure limits, masked routes and bounded discovery observation. Keep legacy C++ defaults and use the existing Session writer. Add lifecycle, allocation rollback and installed-consumer regression coverage.

- Add logical device handles with owned inboxes, generation-bound submission and retryable endpoint drain on the existing Session writer. Add shared admission domains and opt-in endpoint-local age checks; preserve legacy defaults.
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

## 未发布

- 修正设备提交中显式全 Session 队龄检查被改写的问题;新增 `PendingAgeScope::kDefault`,保留设备局部与旧 Session 全局默认行为、既有枚举值和签名,增加作用域、已登记操作、替换、cooldown 及安装消费者回归。

- 补齐 Runtime 共享连接、设备 rebind 与协调恢复、已登记操作收尾、设备节拍与错误阈值、掩码路由及有界发现观察;复用 Session writer,保留旧 C++ 默认行为,增加生命周期、分配失败回滚及安装消费者回归。

- 新增逻辑设备句柄,拥有独立 inbox、绑定 generation 的发送入口及可重试排空,复用原 Session writer;新增共享准入额度域及可选 endpoint 局部队龄检查,保留旧默认行为。
Expand Down
2 changes: 1 addition & 1 deletion docs/en/device-sessions.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ An `AdmissionDomain` is immutable accounting identity, not another queue. Share

`SessionOptions::domain_round_robin` selects an eligible domain by least recent service before ranking its jobs. It is opt-in for mixed deployments. FIFO work retains device ordering. `max_batch_frames` bounds a claimed atomic batch; zero retains the legacy unlimited batch size. Deployment configuration must choose aggregate budgets and batch limits explicitly.

Device pending-age checks default to the device endpoint and include queued, writing and final-cooldown work. `PendingAgeScope::kAdmissionDomain` explicitly checks the shared domain. Legacy Session submissions retain the whole-Session default.
`SendOptions::pending_age_scope` defaults to `PendingAgeScope::kDefault`: it resolves to the device endpoint through DeviceSession and to the whole Session through legacy Session entry points. Explicit `kSession`, `kEndpoint` and `kAdmissionDomain` selections are honored; domain scope requires an admission domain. Checks include queued, writing and final-cooldown work in the selected scope. Existing enum values and default admission behavior are unchanged; the default field value is now the added `kDefault` enumerator. Code that explicitly set `kSession` on a device now checks the whole Session rather than being rewritten to endpoint scope.

Device `send_period` measures each successful frame from dispatch attempt entry. It disables native batch merging, retains the atomic boundary within a batch, and releases the writer during the final cooldown so another device can send. The same Job remains owned until completion; drain and pending-age accounting include it. Device and legacy Session periods cannot be combined.

Expand Down
4 changes: 3 additions & 1 deletion docs/en/session.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@ submitting into a newly recovered connection. It does not verify device identity
the oldest accepted waiting or claimed task has reached that age. This is distinct from a
new task's deadline. Apply it to motion targets when required, rather than automatically to
lifecycle/configuration writes. `health().oldest_pending_age` includes claimed work; the
FIFO/latest pending gauges exclude it.
FIFO/latest pending gauges exclude it. `PendingAgeScope::kDefault` selects the whole Session
for these entry points and the local endpoint for DeviceSession. Explicit scope selections
are preserved; see [device sessions](device-sessions.md) for shared-domain behavior.

`deadline_policy=DeadlinePolicy::kExecution` retains checks while waiting for the physical lock
and between frames. `DeadlinePolicy::kStartOnly` retains queue, lock and initial-delay checks,
Expand Down
2 changes: 1 addition & 1 deletion docs/zh/device-sessions.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

`domain_round_robin` 为混合通道提供显式跨 domain 轮转,再执行域内排序。设备 FIFO 保持顺序。`max_batch_frames` 限制单个原子批次,零保留旧式不限制行为。部署必须明确给出总容量、批次上限和拓扑。

设备默认队龄检查只包含自身 queued、writing 和最终 cooldown;显式 `kAdmissionDomain` 检查共享额度域。旧 Session 提交保留全 Session 默认作用域。
`SendOptions::pending_age_scope` 默认值为新增的 `PendingAgeScope::kDefault`:通过 DeviceSession 提交时解析为本设备 endpoint,通过旧 Session 入口提交时解析为整个 Session。显式 `kSession`、`kEndpoint` 和 `kAdmissionDomain` 按指定范围检查,domain 作用域要求提供额度域。检查包含相应范围的 queued、writing 和最终 cooldown。既有枚举数值与默认准入行为不变;字段默认枚举值改为 `kDefault`。设备调用方显式设置的 `kSession` 现在检查整个 Session,不再被改写为 endpoint。

设备 `send_period` 从每帧 dispatch attempt 入口计时,禁用原生批次合并,批次内部保持原子边界。最终帧 cooldown 释放 writer,使其他设备可发送;同一个 Job 仍由原生层保留,排空和队龄统计继续计入。设备周期不能与旧 Session 周期同时配置。

Expand Down
2 changes: 2 additions & 0 deletions docs/zh/session.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ priority 加等待老化、较早 deadline、较久未服务的 endpoint 和队
`max_pending_age` 是可选的正时长。等待或已领取任务中最早接受的任务达到该年龄时,准入抛
`TimeoutError`。这与新任务自身的 deadline 不同。按需要用于运动目标,不应自动套用到所有
生命周期或配置写入。`health().oldest_pending_age` 包括已领取任务;FIFO/latest 等待量不包括它。
`PendingAgeScope::kDefault` 在这些入口解析为整个 Session,在 DeviceSession 解析为设备 endpoint。
显式作用域按指定范围执行;共享额度域行为见[设备会话](device-sessions.md)。

`deadline_policy=DeadlinePolicy::kExecution` 保留物理发送锁等待和帧间等待期间的检查。
`DeadlinePolicy::kStartOnly` 保留排队、锁等待和首帧延迟检查,在第一次进入后端写入后不再
Expand Down
6 changes: 4 additions & 2 deletions include/linkerbot/transport/session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ namespace linkerbot::transport {

enum class CapacityUnit { kBatches, kFrames };
enum class DeadlinePolicy { kExecution, kStartOnly, kDispatchStart };
enum class PendingAgeScope { kSession, kEndpoint, kAdmissionDomain };
// Keep existing numeric values. Resolve kDefault at the submission boundary:
// whole Session for legacy callers, device endpoint for DeviceSession callers.
enum class PendingAgeScope { kSession, kEndpoint, kAdmissionDomain, kDefault };

struct AdmissionLimits {
size_t fifo_capacity = 2000;
Expand Down Expand Up @@ -177,7 +179,7 @@ namespace linkerbot::transport {
std::optional<uint64_t> expected_generation = std::nullopt;
std::optional<std::chrono::microseconds> max_pending_age = std::nullopt;
DeadlinePolicy deadline_policy = DeadlinePolicy::kExecution;
PendingAgeScope pending_age_scope = PendingAgeScope::kSession;
PendingAgeScope pending_age_scope = PendingAgeScope::kDefault;
std::shared_ptr<const AdmissionDomain> admission_domain = nullptr;
};

Expand Down
5 changes: 4 additions & 1 deletion src/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1605,6 +1605,9 @@ namespace linkerbot::transport {
if (options.max_pending_age && options.max_pending_age->count() <= 0) {
throw can::ValidationError("pending age must be positive");
}
if (options.pending_age_scope == PendingAgeScope::kDefault) {
options.pending_age_scope = PendingAgeScope::kSession;
}
if (options.pending_age_scope != PendingAgeScope::kSession &&
options.pending_age_scope != PendingAgeScope::kEndpoint &&
options.pending_age_scope != PendingAgeScope::kAdmissionDomain) {
Expand Down Expand Up @@ -1808,7 +1811,7 @@ namespace linkerbot::transport {
}
options.endpoint = state_->endpoint;
options.expected_generation = state_->generation;
if (options.pending_age_scope == PendingAgeScope::kSession) {
if (options.pending_age_scope == PendingAgeScope::kDefault) {
options.pending_age_scope = PendingAgeScope::kEndpoint;
}
if (state_->options.admission_domain) {
Expand Down
157 changes: 157 additions & 0 deletions tests/test_session_tx_compat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ namespace {
return receipt;
}
};
struct ReleaseTxGate {
can::FakeTransport *backend;
~ReleaseTxGate() {
backend->release_tx_gate();
}
};
transport::MessageBatch batch(size_t count) {
transport::MessageBatch result;
result.frames.resize(count);
Expand Down Expand Up @@ -310,6 +316,157 @@ TEST_CASE("Session endpoint age includes its own claimed work", "[session]") {
accepted.wait();
}

TEST_CASE("Device pending age distinguishes its default from explicit Session scope", "[session]") {
TxRig rig;
auto peer = rig.session->attach_device("peer");
auto device = rig.session->attach_device("device");
const ReleaseTxGate release{rig.fake};
rig.fake->arm_tx_gate();
auto active = peer->submit(batch(1));
REQUIRE(rig.fake->wait_until_tx_entered(2s));
auto queued = peer->submit(batch(1));
std::this_thread::sleep_for(2ms);
transport::SendOptions options;
options.max_pending_age = 1us;
SECTION("omitted scope retains the device default") {
auto accepted = device->submit(batch(1), options);
REQUIRE_FALSE(accepted.completed());
// The default still includes this device's own waiting work.
std::this_thread::sleep_for(2ms);
REQUIRE_THROWS_AS(device->submit(batch(1), options), can::TimeoutError);
}
SECTION("explicit default resolves to the device endpoint") {
options.pending_age_scope = transport::PendingAgeScope::kDefault;
auto accepted = device->submit(batch(1), options);
REQUIRE_FALSE(accepted.completed());
}
SECTION("explicit endpoint excludes the peer") {
options.pending_age_scope = transport::PendingAgeScope::kEndpoint;
auto accepted = device->submit(batch(1), options);
REQUIRE_FALSE(accepted.completed());
}
SECTION("explicit Session includes the peer") {
options.pending_age_scope = transport::PendingAgeScope::kSession;
REQUIRE_THROWS_AS(device->submit(batch(1), options), can::TimeoutError);
REQUIRE(rig.session->health().accepted == 2);
}
rig.fake->release_tx_gate();
active.wait();
queued.wait();
}

TEST_CASE("Explicit device domain age includes only the selected domain", "[session]") {
TxRig rig;
transport::DeviceOptions shared;
shared.admission_domain =
std::make_shared<transport::AdmissionDomain>(transport::AdmissionLimits{});
auto peer = rig.session->attach_device("peer", shared);
auto same_domain = rig.session->attach_device("same", shared);
transport::DeviceOptions separate;
separate.admission_domain =
std::make_shared<transport::AdmissionDomain>(transport::AdmissionLimits{});
auto other_domain = rig.session->attach_device("other", separate);
const ReleaseTxGate release{rig.fake};
rig.fake->arm_tx_gate();
auto active = peer->submit(batch(1));
REQUIRE(rig.fake->wait_until_tx_entered(2s));
std::this_thread::sleep_for(2ms);
transport::SendOptions options;
options.max_pending_age = 1us;
options.pending_age_scope = transport::PendingAgeScope::kAdmissionDomain;
REQUIRE_THROWS_AS(same_domain->submit(batch(1), options), can::TimeoutError);
auto accepted = other_domain->submit(batch(1), options);
REQUIRE_FALSE(accepted.completed());
rig.fake->release_tx_gate();
active.wait();
accepted.wait();
}

TEST_CASE("Device scope selection applies to owned work and conditional replacement", "[session]") {
TxRig rig;
auto peer = rig.session->attach_device("peer");
auto device = rig.session->attach_device("device");
const ReleaseTxGate release{rig.fake};
rig.fake->arm_tx_gate();
auto active = peer->submit(batch(1));
REQUIRE(rig.fake->wait_until_tx_entered(2s));
// Leave room for local submissions while making the peer older than the budget.
std::this_thread::sleep_for(200ms);
auto options = latest("ignored");
options.max_pending_age = 100ms;
auto old = device->submit(batch(1), options);
options.pending_age_scope = transport::PendingAgeScope::kSession;
REQUIRE_THROWS_AS(device->replace_latest_if_pending(batch(2), options), can::TimeoutError);
REQUIRE_FALSE(old.completed());
auto operation = device->begin_operation();
REQUIRE_THROWS_AS(operation.submit(batch(1), options), can::TimeoutError);
options.pending_age_scope = transport::PendingAgeScope::kDefault;
auto replacement = device->replace_latest_if_pending(batch(2), options);
REQUIRE(replacement.has_value());
REQUIRE(old.result().outcome == transport::SendOutcome::superseded);
options.policy = transport::SendPolicy::fifo;
auto owned = operation.submit(batch(1), options);
rig.fake->release_tx_gate();
active.wait();
test_support::require_value(replacement).wait();
owned.wait();
}

TEST_CASE("Explicit device Session age includes a peer's final cooldown", "[session]") {
TxRig rig;
transport::DeviceOptions paced;
paced.send_period = 1h;
auto peer = rig.session->attach_device("peer", paced);
auto device = rig.session->attach_device("device");
auto cooling = peer->submit(batch(1));
const auto signal = peer->notification();
const auto deadline = transport::Clock::now() + 2s;
while (cooling.result().sent_frames == 0) {
const auto revision = signal->revision();
if (cooling.result().sent_frames != 0) {
break;
}
REQUIRE(signal->wait_until(revision, deadline));
}
REQUIRE_FALSE(cooling.completed());
std::this_thread::sleep_for(2ms);
transport::SendOptions options;
options.max_pending_age = 1us;
options.pending_age_scope = transport::PendingAgeScope::kSession;
REQUIRE_THROWS_AS(device->submit(batch(1), options), can::TimeoutError);
options.pending_age_scope = transport::PendingAgeScope::kDefault;
device->submit(batch(1), options).wait();
REQUIRE_FALSE(cooling.completed());
peer->close_until(deadline);
cooling.wait();
}

TEST_CASE("Session default age remains whole Session with stable explicit scope values",
"[session]") {
static_assert(static_cast<int>(transport::PendingAgeScope::kSession) == 0);
static_assert(static_cast<int>(transport::PendingAgeScope::kEndpoint) == 1);
static_assert(static_cast<int>(transport::PendingAgeScope::kAdmissionDomain) == 2);
TxRig rig;
const ReleaseTxGate release{rig.fake};
auto active = rig.block_writer("peer");
std::this_thread::sleep_for(2ms);
auto options = endpoint("device");
options.max_pending_age = 1us;
SECTION("default options") {
REQUIRE_THROWS_AS(rig.session->submit(batch(1), options), can::TimeoutError);
}
SECTION("explicit default") {
options.pending_age_scope = transport::PendingAgeScope::kDefault;
REQUIRE_THROWS_AS(rig.session->submit(batch(1), options), can::TimeoutError);
}
SECTION("explicit Session") {
options.pending_age_scope = transport::PendingAgeScope::kSession;
REQUIRE_THROWS_AS(rig.session->submit(batch(1), options), can::TimeoutError);
}
rig.fake->release_tx_gate();
active.wait();
}

TEST_CASE("Session admission domains share request limits and isolate frame budgets", "[session]") {
TxRig rig;
auto blocker = rig.block_writer();
Expand Down
31 changes: 31 additions & 0 deletions validation/installed-consumer/session_compat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <linkerbot/transport/session.hpp>
#include <memory>
#include <stdexcept>
#include <thread>
#include <utility>

namespace {
Expand All @@ -24,6 +25,35 @@ namespace {
}
};

void run_device_age_consumer() {
auto backend = std::make_unique<can::FakeTransport>();
auto *fake = backend.get();
auto session = transport::Session::own(std::move(backend));
auto peer = session->attach_device("peer");
auto device = session->attach_device("device");
const ReleaseGate release{fake};
fake->arm_tx_gate();
const transport::MessageBatch single{{can::Message{}}};
auto active = peer->submit(single);
check(fake->wait_until_tx_entered(2s), "installed device writer did not enter gate");
std::this_thread::sleep_for(2ms);
transport::SendOptions options;
options.max_pending_age = 1us;
options.pending_age_scope = transport::PendingAgeScope::kSession;
bool rejected = false;
try {
(void)device->submit(single, options);
} catch (const can::TimeoutError &) {
rejected = true;
}
check(rejected, "installed device ignored explicit Session age scope");
options.pending_age_scope = transport::PendingAgeScope::kDefault;
auto accepted = device->submit(single, options);
fake->release_tx_gate();
active.wait();
accepted.wait();
}

void run_consumer() {
auto backend = std::make_unique<can::FakeTransport>();
auto *fake = backend.get();
Expand Down Expand Up @@ -100,6 +130,7 @@ namespace {
int main() {
try {
run_consumer();
run_device_age_consumer();
std::cout << "Installed Session compatibility APIs passed\n";
return 0;
} catch (const std::exception &error) {
Expand Down
Loading