Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 2m28s
Foundation / checks (tsan) (push) Failing after 18m22s
Foundation / checks (network) (push) Successful in 20m38s
Foundation / checks (client) (push) Failing after 21m6s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (postgres) (push) Canceled after 46m54s
Foundation / checks (checks) (push) Canceled after 47m3s
80 lines
3.4 KiB
C++
80 lines
3.4 KiB
C++
#include "evo/server/lifecycle/cellular_schedule.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <limits>
|
|
|
|
namespace evo::server {
|
|
bool cellular_unattended_tick_needed(const CellularLives& state) noexcept {
|
|
// Historical trial profiles lack the offline freeze. Do not opt them into
|
|
// unattended stepping and accidentally advance disconnected controlled cells.
|
|
if (state.rules_version() < cellular_lives_presence_rules)
|
|
return false;
|
|
return std::ranges::any_of(state.lives(), [](const auto& life) {
|
|
return life.account == LifeAccount{} || life.presence != CellularPresence::suspended;
|
|
});
|
|
}
|
|
|
|
std::expected<CellularScheduleStatus, CellularScheduleError>
|
|
CellularTickSchedule::observe(std::uint64_t now_us, std::uint64_t epoch,
|
|
std::uint64_t committed_tick, bool needed, bool authoritative,
|
|
std::uint64_t previously_admitted_ticks) {
|
|
if (previously_admitted_ticks > 64 ||
|
|
previously_admitted_ticks > std::numeric_limits<std::uint64_t>::max() - committed_tick)
|
|
return std::unexpected(CellularScheduleError::admission);
|
|
if (observed_ && now_us < last_us_)
|
|
return std::unexpected(CellularScheduleError::clock_regression);
|
|
if (authoritative && epoch == 0)
|
|
return std::unexpected(CellularScheduleError::authority);
|
|
if (anchored_ && authoritative && epoch == epoch_ && committed_tick < head_)
|
|
return std::unexpected(CellularScheduleError::head_regression);
|
|
observed_ = true;
|
|
last_us_ = now_us;
|
|
if (authoritative &&
|
|
(!was_authoritative_ || epoch != receipt_epoch_ || committed_tick != receipt_tick_)) {
|
|
have_receipt_ = true;
|
|
receipt_us_ = now_us;
|
|
receipt_tick_ = committed_tick;
|
|
receipt_epoch_ = epoch;
|
|
}
|
|
was_authoritative_ = authoritative;
|
|
status_.head_age_us = have_receipt_ ? now_us - receipt_us_ : 0;
|
|
status_.due = false;
|
|
status_.retry_ready = now_us >= retry_us_;
|
|
status_.debt = 0;
|
|
if (!authoritative || !needed) {
|
|
anchored_ = false;
|
|
retry_us_ = 0;
|
|
status_.retry_ready = true;
|
|
return status_;
|
|
}
|
|
if (!anchored_ || epoch != epoch_) {
|
|
anchored_ = true;
|
|
origin_us_ = now_us;
|
|
origin_tick_ = committed_tick + previously_admitted_ticks;
|
|
epoch_ = epoch;
|
|
retry_us_ = 0;
|
|
}
|
|
head_ = committed_tick;
|
|
const auto elapsed_ticks = (now_us - origin_us_) / biology::cellular_motion_tick_microseconds;
|
|
const auto completed_ticks = committed_tick > origin_tick_ ? committed_tick - origin_tick_ : 0;
|
|
const auto remaining = std::numeric_limits<std::uint64_t>::max() - committed_tick;
|
|
status_.debt =
|
|
elapsed_ticks > completed_ticks ? std::min(elapsed_ticks - completed_ticks, remaining) : 0;
|
|
status_.max_debt = std::max(status_.max_debt, status_.debt);
|
|
status_.retry_ready = now_us >= retry_us_;
|
|
status_.due = status_.debt > 0 && status_.retry_ready;
|
|
return status_;
|
|
}
|
|
void CellularTickSchedule::defer(std::uint64_t now_us) noexcept {
|
|
constexpr auto maximum = std::numeric_limits<std::uint64_t>::max();
|
|
retry_us_ = now_us > maximum - biology::cellular_motion_tick_microseconds
|
|
? maximum
|
|
: now_us + biology::cellular_motion_tick_microseconds;
|
|
status_.due = false;
|
|
status_.retry_ready = false;
|
|
}
|
|
CellularScheduleStatus CellularTickSchedule::status() const noexcept {
|
|
return status_;
|
|
}
|
|
} // namespace evo::server
|