diff --git a/CHANGELOG.en.md b/CHANGELOG.en.md index 163d07e..1e70708 100644 --- a/CHANGELOG.en.md +++ b/CHANGELOG.en.md @@ -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. diff --git a/CHANGELOG.md b/CHANGELOG.md index 79532a3..60b1006 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ## 未发布 +- 修正设备提交中显式全 Session 队龄检查被改写的问题;新增 `PendingAgeScope::kDefault`,保留设备局部与旧 Session 全局默认行为、既有枚举值和签名,增加作用域、已登记操作、替换、cooldown 及安装消费者回归。 + - 补齐 Runtime 共享连接、设备 rebind 与协调恢复、已登记操作收尾、设备节拍与错误阈值、掩码路由及有界发现观察;复用 Session writer,保留旧 C++ 默认行为,增加生命周期、分配失败回滚及安装消费者回归。 - 新增逻辑设备句柄,拥有独立 inbox、绑定 generation 的发送入口及可重试排空,复用原 Session writer;新增共享准入额度域及可选 endpoint 局部队龄检查,保留旧默认行为。 diff --git a/docs/en/device-sessions.md b/docs/en/device-sessions.md index 3d22a0c..bd4c84d 100644 --- a/docs/en/device-sessions.md +++ b/docs/en/device-sessions.md @@ -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. diff --git a/docs/en/session.md b/docs/en/session.md index 81a7b7f..154640b 100644 --- a/docs/en/session.md +++ b/docs/en/session.md @@ -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, diff --git a/docs/zh/device-sessions.md b/docs/zh/device-sessions.md index dabbd94..333b277 100644 --- a/docs/zh/device-sessions.md +++ b/docs/zh/device-sessions.md @@ -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 周期同时配置。 diff --git a/docs/zh/session.md b/docs/zh/session.md index f525f4b..1db87bf 100644 --- a/docs/zh/session.md +++ b/docs/zh/session.md @@ -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` 保留排队、锁等待和首帧延迟检查,在第一次进入后端写入后不再 diff --git a/include/linkerbot/transport/session.hpp b/include/linkerbot/transport/session.hpp index 01fa292..e11b3b6 100644 --- a/include/linkerbot/transport/session.hpp +++ b/include/linkerbot/transport/session.hpp @@ -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; @@ -177,7 +179,7 @@ namespace linkerbot::transport { std::optional expected_generation = std::nullopt; std::optional 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 admission_domain = nullptr; }; diff --git a/src/session.cpp b/src/session.cpp index d2a238c..2253e75 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -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) { @@ -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) { diff --git a/tests/test_session_tx_compat.cpp b/tests/test_session_tx_compat.cpp index e27b17b..171aaef 100644 --- a/tests/test_session_tx_compat.cpp +++ b/tests/test_session_tx_compat.cpp @@ -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); @@ -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::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::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(transport::PendingAgeScope::kSession) == 0); + static_assert(static_cast(transport::PendingAgeScope::kEndpoint) == 1); + static_assert(static_cast(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(); diff --git a/validation/installed-consumer/session_compat.cpp b/validation/installed-consumer/session_compat.cpp index 45b6c15..520cbc5 100644 --- a/validation/installed-consumer/session_compat.cpp +++ b/validation/installed-consumer/session_compat.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include namespace { @@ -24,6 +25,35 @@ namespace { } }; + void run_device_age_consumer() { + auto backend = std::make_unique(); + 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(); auto *fake = backend.get(); @@ -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) {