Skip to content

Commit 67aad88

Browse files
committed
Merge commit '9ee1cc7d5e53568279d882900f96c21f5ddde055' into perf/placement-storage
2 parents d25e8d2 + 9ee1cc7 commit 67aad88

2 files changed

Lines changed: 110 additions & 1 deletion

File tree

‎src/selected_capacity_script_bars.cpp‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
#include "selected_capacity_internal.hpp"
22

3+
#include <pineforge/run_failure.hpp>
4+
5+
#include <stdexcept>
6+
37
namespace pineforge::detail::selected_capacity {
48

59
void observe_script_bars(State& state, const Limits& limits, const RecordEvent& event) {
6-
if (event.role != Role::Primary || !event.has_script_bucket) return;
10+
if (event.role != Role::Primary) return;
11+
if (!event.has_script_bucket)
12+
throw coded<std::logic_error>(
13+
RunFailureCode::engine_invariant, {},
14+
"script-bar observer got a Primary row without a script bucket");
715
if (state.has_last_primary_bucket
816
&& state.last_primary_bucket == event.script_bucket_key) return;
917

‎tests/test_selected_capacity_script_bars.cpp‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// A bucket opens when its native script-bucket key differs from the preceding Primary row's key.
55
// Pre-roll, rows later trimmed and an unsealed final bucket all count. Source and Auxiliary rows do not.
66
// The product guard under test is observe_script_bars, called through this test's run_driver observer table.
7+
// S14 calls the observer directly to check its invariant refusal.
78
// A row opening bucket limit + 1 is refused before it reaches the materialization sink. Equality passes;
89
// an absent bound imposes no limit, while explicit zero refuses the first bucket.
910
//
@@ -29,6 +30,7 @@
2930
// S11 preserves the last bucket across two Primary streams.
3031
// S12 checks strict-feed hourly buckets.
3132
// S13 checks strict-feed buckets with equal input and script timeframes.
33+
// S14 checks typed refusal of a Primary row without a script bucket and unchanged State.
3234
// Each counting scenario admits its opened count, refuses one below it, accepts an absent bound and
3335
// admits a larger finite bound. S3 and S10 also refuse at the planner's smaller fed count.
3436
//
@@ -660,6 +662,104 @@ void s13_strict_feed_equal_timeframes() {
660662
ladder(c);
661663
}
662664

665+
void s14_primary_row_without_a_script_bucket_is_refused() {
666+
sc::Limits finite;
667+
finite.script_bars = sc::Bound{true, 8};
668+
sc::Limits absent;
669+
sc::State before;
670+
before.source_rows = 2;
671+
before.primary_rows = 5;
672+
before.script_bars = 3;
673+
before.auxiliary_rows = 7;
674+
before.bytes = 11;
675+
before.has_last_primary_bucket = true;
676+
before.last_primary_bucket = kBase;
677+
sc::RecordEvent event;
678+
event.role = sc::Role::Primary;
679+
event.timestamp_ms = kBase + 5 * kMin;
680+
event.has_script_bucket = false;
681+
event.script_bucket_key = kBase + kHour;
682+
683+
const auto check_unchanged = [](const sc::State& actual, const sc::State& expected) {
684+
CHECK(actual.source_rows == expected.source_rows);
685+
CHECK(actual.primary_rows == expected.primary_rows);
686+
CHECK(actual.script_bars == expected.script_bars);
687+
CHECK(actual.auxiliary_rows == expected.auxiliary_rows);
688+
CHECK(actual.bytes == expected.bytes);
689+
CHECK(actual.has_last_primary_bucket == expected.has_last_primary_bucket);
690+
CHECK(actual.last_primary_bucket == expected.last_primary_bucket);
691+
};
692+
const auto expect_invariant = [&](sc::State& state, const sc::Limits& limits) {
693+
const sc::State initial = state;
694+
bool refused = false;
695+
try {
696+
sc::observe_script_bars(state, limits, event);
697+
} catch (const pineforge::coded<std::logic_error>& error) {
698+
refused = true;
699+
const pineforge::RunFailureValue& failure = error.run_failure();
700+
CHECK(std::string(pineforge::run_failure_code_name(failure.code)) == "engine_invariant");
701+
CHECK(pineforge::run_failure_code_class(failure.code)
702+
== pineforge::RunFailureClass::engine_fault);
703+
CHECK(!pineforge::run_failure_code_retryable(failure.code));
704+
CHECK((failure.args ? *failure.args : std::string("{}")) == "{}");
705+
CHECK(std::string(error.what())
706+
== "script-bar observer got a Primary row without a script bucket");
707+
}
708+
CHECK(refused && "Primary row without a script bucket must throw a coded logic_error");
709+
check_unchanged(state, initial);
710+
};
711+
712+
scenario = "S14 Primary without a script bucket, finite bound";
713+
sc::State finite_state = before;
714+
expect_invariant(finite_state, finite);
715+
716+
scenario = "S14 Primary without a script bucket, absent bound";
717+
sc::State absent_state = before;
718+
expect_invariant(absent_state, absent);
719+
720+
scenario = "S14 Primary with a script bucket counts once";
721+
sc::RecordEvent bucketful = event;
722+
bucketful.has_script_bucket = true;
723+
sc::State counted = before;
724+
sc::observe_script_bars(counted, finite, bucketful);
725+
CHECK(counted.script_bars == 4);
726+
CHECK(counted.last_primary_bucket == bucketful.script_bucket_key);
727+
CHECK(counted.has_last_primary_bucket);
728+
CHECK(counted.source_rows == before.source_rows);
729+
CHECK(counted.primary_rows == before.primary_rows);
730+
CHECK(counted.auxiliary_rows == before.auxiliary_rows);
731+
CHECK(counted.bytes == before.bytes);
732+
733+
scenario = "S14 Primary with a script bucket at saturation";
734+
sc::State saturated_before = before;
735+
saturated_before.script_bars = 8;
736+
sc::State saturated = saturated_before;
737+
bool capacity_refused = false;
738+
try {
739+
sc::observe_script_bars(saturated, finite, bucketful);
740+
} catch (const pineforge::coded<std::runtime_error>& error) {
741+
capacity_refused = true;
742+
const pineforge::RunFailureValue& failure = error.run_failure();
743+
CHECK(std::string(pineforge::run_failure_code_name(failure.code))
744+
== "selected_script_bars_limit_exceeded");
745+
}
746+
CHECK(capacity_refused && "A new script bucket at the bound must throw a coded capacity failure");
747+
check_unchanged(saturated, saturated_before);
748+
749+
scenario = "S14 Primary without a script bucket at saturation";
750+
expect_invariant(saturated, finite);
751+
752+
for (const sc::Role role : {sc::Role::Source, sc::Role::Auxiliary}) {
753+
scenario = role == sc::Role::Source ? "S14 Source without a script bucket"
754+
: "S14 Auxiliary without a script bucket";
755+
sc::RecordEvent other = event;
756+
other.role = role;
757+
sc::State unchanged = before;
758+
sc::observe_script_bars(unchanged, finite, other);
759+
check_unchanged(unchanged, before);
760+
}
761+
}
762+
663763
} // namespace
664764

665765
int main() {
@@ -676,6 +776,7 @@ int main() {
676776
s11_one_hour_split_over_two_primary_streams();
677777
s12_strict_feed_hourly();
678778
s13_strict_feed_equal_timeframes();
779+
s14_primary_row_without_a_script_bucket_is_refused();
679780
if (setup_failures != 0) {
680781
std::fprintf(stderr, "NEEDS setup %d NotAdmitted answer(s)\n", setup_failures);
681782
return 2;

0 commit comments

Comments
 (0)