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
61 changes: 60 additions & 1 deletion src/cpp/src/key_directory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace {

constexpr std::int32_t slot_initializing = 1;
constexpr std::int32_t slot_reserved = 2;
constexpr std::int32_t slot_published = 3;
constexpr std::int32_t slot_remove_requested = 4;
constexpr std::int32_t slot_aborting = 5;
constexpr std::int32_t slot_reclaiming = 6;
constexpr std::int32_t slot_retired = 7;
Expand Down Expand Up @@ -1334,7 +1336,64 @@ sms_status KeyDirectory::help_insert(
binding,
control);
const auto reserved_status = publish_reserved(value_slot, binding);
if (reserved_status != SMS_STATUS_SUCCESS) return reserved_status;
if (reserved_status != SMS_STATUS_SUCCESS) {
// Reserved publication can lose to a helper that completes this
// insertion before its owner publishes, cancels, or reuses the
// slot. Classify the failure against the exact source descriptor
// before treating its now-inapplicable state as corruption.
if (MappedAtomic64::load_acquire(value_slot.DirectoryOperation) !=
operation_raw) {
return SMS_STATUS_SUCCESS;
}
const auto revalidated_control =
MappedAtomic64::load_acquire(value_slot.Control);
const auto revalidated_binding =
MappedAtomic64::load_acquire(value_slot.DirectoryBinding);
const auto confirmed_control =
MappedAtomic64::load_acquire(value_slot.Control);
if (MappedAtomic64::load_acquire(value_slot.DirectoryOperation) !=
operation_raw) {
return SMS_STATUS_SUCCESS;
}
if (revalidated_control != confirmed_control) {
return SMS_STATUS_STORE_BUSY;
}
const auto control_status =
classify_control(confirmed_control, decoded.generation);
if (control_status == ControlBindingStatus::stale) {
return SMS_STATUS_SUCCESS;
}
if (control_status == ControlBindingStatus::invalid ||
revalidated_binding != binding) {
return SMS_STATUS_CORRUPT_STORE;
}
SlotControl revalidated{};
(void)SlotControl::try_decode(confirmed_control, revalidated);
if (revalidated.state == slot_aborting ||
revalidated.state == slot_reclaiming) {
// Retry helping from this state so the existing cancellation
// path removes the binding and rejects the reservation.
return SMS_STATUS_SUCCESS;
}
if (revalidated.state != slot_published &&
revalidated.state != slot_remove_requested) {
return reserved_status;
}
// Explicit reservations may return through reserve_core's ordered
// fallback while this descriptor is still BindingChanged. Their
// owner can therefore commit (and a reader can request removal)
// before any helper publishes Complete. Preserve that publication
// and finish the exact descriptor once its reference is confirmed.
if (MappedAtomic64::load_acquire(value_slot.DirectoryLocation) !=
location_raw ||
MappedAtomic64::load_acquire(*target.word) != binding ||
MappedAtomic64::load_acquire(value_slot.Control) !=
confirmed_control ||
MappedAtomic64::load_acquire(value_slot.DirectoryOperation) !=
operation_raw) {
return SMS_STATUS_SUCCESS;
}
}
checkpoint(
DirectoryCheckpoint::after_reserved_publication,
binding,
Expand Down
5 changes: 5 additions & 0 deletions tests/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ function(_sms_add_native_test source_name)
PRIVATE "${PROJECT_SOURCE_DIR}/src/cpp/src/key_directory.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/src/layout_v2.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/src/protocol.cpp")
if(_test_stem STREQUAL "directory_schedule_tests")
target_sources(
${_target}
PRIVATE "${PROJECT_SOURCE_DIR}/src/cpp/src/slot_table.cpp")
endif()
elseif(_test_stem STREQUAL "platform_linux_v2_tests")
# Resource-protocol-2 ownership helpers are hidden platform internals.
# Compile them and canonical resource-name derivation directly into the
Expand Down
202 changes: 202 additions & 0 deletions tests/cpp/directory_schedule_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "directory_test_support.hpp"
#include "slot_table.hpp"
#include "test_support.hpp"

#include <cstdint>
Expand Down Expand Up @@ -159,6 +160,120 @@ void scheduled_reach(
}
}

enum class ReservedRace { publish, cancel, reuse, malformed_control };

struct ReservedRaceContext {
Fixture* fixture{};
ReservedRace race{};
bool armed{true};
sms_status peer_status{SMS_STATUS_STORE_BUSY};
DirectoryLocation peer_location{};
std::uint64_t replacement_binding{};
std::uint64_t replacement_operation{};
};

void advance_before_reserved(
void* raw_context,
DirectoryCheckpoint checkpoint,
std::uint64_t binding,
std::uint64_t) noexcept {
auto& context = *static_cast<ReservedRaceContext*>(raw_context);
if (!context.armed ||
checkpoint != DirectoryCheckpoint::before_reserved_publication) {
return;
}
context.armed = false;
auto& fixture = *context.fixture;
if (context.race == ReservedRace::cancel) {
fixture.set_slot_state(0, 1, 5);
return;
}
if (context.race == ReservedRace::malformed_control) {
// An occupied terminal generation with an owner is structurally invalid.
MappedAtomic64::store_release(fixture.slot(0).Control, UINT64_MAX);
return;
}
context.peer_status = fixture.directory().try_insert(
bytes("reserved-race"), 101, binding,
OperationBudget::unbounded_scan(), context.peer_location);
fixture.set_slot_state(0, 1, 3);
if (context.race == ReservedRace::reuse) {
fixture.set_slot_state(0, 1, 6);
context.peer_status = fixture.directory().try_unlink(
binding, OperationBudget::unbounded_scan());
context.replacement_binding = fixture.seed_slot(
0, "replacement", 101, 2, 1);
context.peer_status = fixture.directory().try_insert(
bytes("replacement"), 101, context.replacement_binding,
OperationBudget::unbounded_scan(), context.peer_location);
fixture.set_slot_state(0, 2, 3);
context.replacement_operation = MappedAtomic64::load_acquire(
fixture.slot(0).DirectoryOperation);
}
}

struct ReservationFallbackContext {
Fixture* fixture{};
sms::detail::SlotTable* owner{};
sms::detail::ReservationToken reservation{};
bool remove_requested{};
bool armed{true};
bool pending{};
bool ordered{};
sms_status canceled_insert{SMS_STATUS_SUCCESS};
sms_status contains_status{SMS_STATUS_NOT_FOUND};
sms_status commit_status{SMS_STATUS_INVALID_RESERVATION};
sms_status helper_status{SMS_STATUS_STORE_BUSY};
std::int32_t helper_phase{};
};

void commit_before_insert_completion(
void* raw_context,
DirectoryCheckpoint checkpoint,
std::uint64_t binding,
std::uint64_t) noexcept {
auto& context = *static_cast<ReservationFallbackContext*>(raw_context);
if (!context.armed ||
checkpoint != DirectoryCheckpoint::after_reserved_publication) {
return;
}
context.armed = false;
auto& fixture = *context.fixture;
// Keep the publishing helper paused after Initializing -> Reserved, with
// its descriptor still BindingChanged. The owner encounters cancellation
// and applies reserve_core's pending-plus-exact-reference fallback before
// committing through the real SlotTable operation.
sms::detail::CancellationFlag canceled;
canceled.cancel();
DirectoryLocation location{};
context.canceled_insert = fixture.directory().try_insert(
bytes("reservation-fallback"), 101, binding,
OperationBudget::unbounded_scan(&canceled), location);
context.pending = context.owner->reservation_pending(context.reservation);
context.contains_status = fixture.directory().contains_exact_reference(
binding, OperationBudget::structural_attempt(), context.ordered);
if (context.canceled_insert != SMS_STATUS_SUCCESS && context.pending &&
context.contains_status == SMS_STATUS_SUCCESS && context.ordered) {
context.commit_status = context.owner->commit_reservation(
context.reservation, 1);
}
if (context.commit_status != SMS_STATUS_SUCCESS) return;
if (context.remove_requested) fixture.set_slot_state(0, 1, 4);
std::int32_t canonical{};
std::int32_t alternate{};
fixture.directory().buckets_for_hash(101, canonical, alternate);
// A different helper must finish this still-current descriptor even though
// the exact slot has legally moved beyond Reserved.
context.helper_status = fixture.directory().help_mutation(
canonical, OperationBudget::unbounded_scan(), 2);
DirectoryOperation operation{};
if (DirectoryOperation::try_decode(
MappedAtomic64::load_acquire(fixture.slot(0).DirectoryOperation),
operation)) {
context.helper_phase = operation.phase;
}
}

std::int32_t slot_state(Fixture& fixture, std::int32_t index) {
SlotControl control{};
if (!SlotControl::try_decode(
Expand Down Expand Up @@ -413,6 +528,93 @@ int main() {
SMS_STATUS_CORRUPT_STORE);
}

for (const auto race : {ReservedRace::publish, ReservedRace::cancel,
ReservedRace::reuse, ReservedRace::malformed_control}) {
ReservedRaceContext context{};
context.race = race;
Fixture fixture(
32, 128,
sms::detail::DirectoryHooks{&context, &advance_before_reserved});
context.fixture = &fixture;
const auto binding = fixture.seed_slot(0, "reserved-race", 101, 1, 1);
DirectoryLocation location{};
const auto status = fixture.directory().try_insert(
bytes("reserved-race"), 101, binding,
OperationBudget::unbounded_scan(), location);
SMS_CHECK(!context.armed);
if (race == ReservedRace::malformed_control) {
SMS_CHECK(status == SMS_STATUS_CORRUPT_STORE);
} else if (race == ReservedRace::cancel) {
SMS_CHECK(status == SMS_STATUS_INVALID_RESERVATION);
bool remains{};
SMS_CHECK(fixture.directory().contains_exact_reference(
binding, OperationBudget::unbounded_scan(), remains) ==
SMS_STATUS_SUCCESS);
SMS_CHECK(!remains);
SMS_CHECK(slot_state(fixture, 0) == 5);
} else {
SMS_CHECK(context.peer_status == SMS_STATUS_SUCCESS);
SMS_CHECK(status == (race == ReservedRace::publish
? SMS_STATUS_SUCCESS
: SMS_STATUS_INVALID_RESERVATION));
DirectoryEntry found{};
SMS_CHECK(fixture.directory().try_lookup(
bytes(race == ReservedRace::publish
? "reserved-race" : "replacement"),
101, OperationBudget::unbounded_scan(), found) ==
SMS_STATUS_SUCCESS);
SMS_CHECK(found.binding == (race == ReservedRace::publish
? binding
: context.replacement_binding));
SMS_CHECK(slot_state(fixture, 0) == 3);
if (race == ReservedRace::reuse) {
SMS_CHECK(MappedAtomic64::load_acquire(
fixture.slot(0).DirectoryOperation) ==
context.replacement_operation);
}
}
}

for (const auto remove_requested : {false, true}) {
ReservationFallbackContext context{};
context.remove_requested = remove_requested;
Fixture fixture(
32, 128,
sms::detail::DirectoryHooks{&context, &commit_before_insert_completion});
context.fixture = &fixture;
const auto binding = fixture.seed_slot(0, "reservation-fallback", 101, 1, 1);
fixture.slot(0).PublicationIntent = 1;
std::uint64_t token{};
std::uint64_t active{};
SMS_CHECK(sms::detail::ParticipantToken::try_encode(
0, 1, fixture.layout().participant_record_count, token));
SMS_CHECK(sms::detail::ParticipantControl::try_encode(2, 1, 1001, active));
auto* participant = reinterpret_cast<sms::detail::ParticipantRecordV2*>(
fixture.base() + fixture.layout().participant_offset);
MappedAtomic64::store_release(participant->Control, active);
constexpr std::uint64_t store_id = 7;
sms::detail::SlotTable owner(
fixture.base(), static_cast<std::size_t>(fixture.layout().required_bytes),
fixture.layout(), store_id,
{static_cast<std::uint32_t>(token), active});
SMS_CHECK(owner.valid());
context.owner = &owner;
context.reservation = {store_id, static_cast<std::uint32_t>(token), binding, 0};
DirectoryLocation location{};
SMS_CHECK(fixture.directory().try_insert(
bytes("reservation-fallback"), 101, binding,
OperationBudget::unbounded_scan(), location) ==
SMS_STATUS_SUCCESS);
SMS_CHECK(!context.armed);
SMS_CHECK(context.canceled_insert == SMS_STATUS_OPERATION_CANCELED);
SMS_CHECK(context.pending && context.ordered);
SMS_CHECK(context.contains_status == SMS_STATUS_SUCCESS);
SMS_CHECK(context.commit_status == SMS_STATUS_SUCCESS);
SMS_CHECK(context.helper_status == SMS_STATUS_SUCCESS);
SMS_CHECK(context.helper_phase == sms::detail::directory_phase_complete);
SMS_CHECK(slot_state(fixture, 0) == (remove_requested ? 4 : 3));
}

{
Fixture fixture;
constexpr std::uint64_t hash = 808;
Expand Down
Loading