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
4 changes: 4 additions & 0 deletions services/common/src/hybrid/name_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@

use super::names::{self, NameRecord};

/// The settings index naming every reserved name (`crate::settings_index`):
/// the gateway adds a name when it is claimed, the name check reads it.
pub const NAME_INDEX: &str = "HYBRIDNAME";

/// Wrong answers in a row before a name is parked (15 minutes apart: about
/// 45 minutes).
pub const PARK_AFTER: u32 = 3;
Expand Down
1 change: 1 addition & 0 deletions services/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub mod github_wait;
pub mod live;
/// Log findings and the fix runs started from them.
pub mod log_findings;
pub mod settings_index;
// Which repo a log group's or alarm's code lives in, from evidence.
pub mod log_reader;
pub mod log_repo;
Expand Down
80 changes: 63 additions & 17 deletions services/common/src/log_findings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1244,33 +1244,52 @@ pub async fn load_ticket(
})
}

/// Teams whose automatic fixes are on.
/// The settings index naming every team with an automatic-fix setting
/// ([`crate::settings_index`]); the gateway adds the team when it saves one.
pub const AUTOFIX_INDEX: &str = "LOG_AUTOFIX";

fn autofix_team(i: &Item) -> Option<String> {
let team = s(i, "pk");
(s(i, "sk") == AUTOFIX_SK && !team.is_empty()).then_some(team)
}

/// How a full scan finds the automatic-fix settings (the index's rebuild).
pub fn autofix_index() -> crate::settings_index::Spec<'static> {
crate::settings_index::Spec {
name: AUTOFIX_INDEX,
filter: "sk = :sk",
values: vec![(":sk", attr(AUTOFIX_SK))],
member_of: autofix_team,
}
}

/// Teams whose automatic fixes are on: each indexed team's setting, read by
/// key. This ran hourly as a scan of the whole settings table before.
pub async fn teams_with_autofix(
dynamo: &DynamoClient,
settings_table: &str,
) -> Result<Vec<(String, AutoFix)>, String> {
let teams = crate::settings_index::members(dynamo, settings_table, &autofix_index()).await?;
let mut out = Vec::new();
let mut start: Option<Item> = None;
loop {
let page = dynamo
.scan()
for team in teams {
let got = dynamo
.get_item()
.table_name(settings_table)
.filter_expression("sk = :sk")
.expression_attribute_values(":sk", attr(AUTOFIX_SK))
.set_exclusive_start_key(start.take())
.key("pk", attr(&team))
.key("sk", attr(AUTOFIX_SK))
.send()
.await
.map_err(|e| e.to_string())?;
for i in page.items() {
let cfg = AutoFix::from_item(Some(i));
let team = s(i, "pk");
if cfg.enabled && !team.is_empty() {
out.push((team, cfg));
match got.item() {
None => {
crate::settings_index::remove(dynamo, settings_table, AUTOFIX_INDEX, &team).await
}
Some(i) => {
let cfg = AutoFix::from_item(Some(i));
if cfg.enabled {
out.push((team, cfg));
}
}
}
match page.last_evaluated_key() {
Some(k) if !k.is_empty() => start = Some(k.clone()),
_ => break,
}
}
Ok(out)
Expand Down Expand Up @@ -2505,3 +2524,30 @@ mod tests {
assert_eq!(daily_sk(t(NOW)), "LOG_AUTOFIX_RUNS#2026-10-21");
}
}

#[cfg(test)]
mod autofix_index_tests {
use super::*;

fn item(pk: &str, sk: &str) -> Item {
HashMap::from([("pk".to_string(), attr(pk)), ("sk".to_string(), attr(sk))])
}

#[test]
fn only_automatic_fix_settings_become_index_members() {
assert_eq!(
autofix_team(&item("TEAM#a", AUTOFIX_SK)),
Some("TEAM#a".into())
);
assert_eq!(autofix_team(&item("TEAM#a", "ANALYTICS#x")), None);
assert_eq!(autofix_team(&item("", AUTOFIX_SK)), None);
}

#[test]
fn the_rebuild_scan_matches_the_setting_key() {
let spec = autofix_index();
assert_eq!(spec.name, AUTOFIX_INDEX);
assert_eq!(spec.filter, "sk = :sk");
assert_eq!(spec.values[0].1, attr(AUTOFIX_SK));
}
}
246 changes: 246 additions & 0 deletions services/common/src/settings_index.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
//! A small index partition in the settings table that names every record of
//! one kind, so a sweep reads those records by key instead of scanning the
//! whole table.
//!
//! The settings table holds analytics, markers and every team's settings
//! together; on a busy install it is well over 100,000 items. A sweep that
//! found its records with a filtered scan read all of it on every tick, and a
//! scan with a page cap stopped before reaching them at all.
//!
//! Layout (settings table):
//! - `pk = INDEX#<name>`, `sk = <member>`: one item per member (a team id, a
//! name). Writers add the member when they save the record.
//! - `pk = INDEX#<name>`, `sk = #BUILT`: when the index was last rebuilt
//! from a full scan (`at`, RFC 3339).
//!
//! Readers rebuild the index from one full scan when it was never built or
//! the last rebuild is older than [`REBUILD_AFTER_HOURS`], so records saved
//! before the index existed, or by a writer that failed to add itself, are
//! found within a day. Readers check each member's record by key and drop
//! members whose record is gone.

use std::collections::{BTreeSet, HashMap};

use aws_sdk_dynamodb::types::{AttributeValue, PutRequest, WriteRequest};
use aws_sdk_dynamodb::Client as DynamoClient;
use chrono::{DateTime, Duration, Utc};
use tracing::{info, warn};

pub type Item = HashMap<String, AttributeValue>;

/// How old a rebuild may be before the next reader rebuilds again.
pub const REBUILD_AFTER_HOURS: i64 = 24;
const BUILT_SK: &str = "#BUILT";

/// One index: its name and how a full scan recognises its members.
pub struct Spec<'a> {
/// `INDEX#<name>`.
pub name: &'a str,
/// The scan filter that matches the indexed records.
pub filter: &'a str,
pub values: Vec<(&'a str, AttributeValue)>,
/// The member a matching record stands for (`None` skips it).
pub member_of: fn(&Item) -> Option<String>,
}

fn s(v: &str) -> AttributeValue {
AttributeValue::S(v.to_string())
}

pub fn partition(name: &str) -> String {
format!("INDEX#{name}")
}

/// Pure: whether a reader must rebuild, from the last rebuild time.
pub fn needs_rebuild(built_at: Option<&str>, now: DateTime<Utc>) -> bool {
match built_at.and_then(|t| DateTime::parse_from_rfc3339(t).ok()) {
Some(t) => now - t.with_timezone(&Utc) >= Duration::hours(REBUILD_AFTER_HOURS),
None => true,
}
}

/// Adds `member` to the index (writers call this when they save a record).
pub async fn add(
dynamo: &DynamoClient,
table: &str,
name: &str,
member: &str,
) -> Result<(), String> {
dynamo
.put_item()
.table_name(table)
.item("pk", s(&partition(name)))
.item("sk", s(member))
.send()
.await
.map(|_| ())
.map_err(|e| format!("index {name}: {e}"))
}

/// Drops `member` from the index (its record is gone).
pub async fn remove(dynamo: &DynamoClient, table: &str, name: &str, member: &str) {
if let Err(e) = dynamo
.delete_item()
.table_name(table)
.key("pk", s(&partition(name)))
.key("sk", s(member))
.send()
.await
{
warn!(index = name, member, error = %e, "Could not drop a stale index member");
}
}

/// Every member, rebuilding the index from one full scan when it is due.
pub async fn members(
dynamo: &DynamoClient,
table: &str,
spec: &Spec<'_>,
) -> Result<Vec<String>, String> {
let pk = partition(spec.name);
let built = dynamo
.get_item()
.table_name(table)
.key("pk", s(&pk))
.key("sk", s(BUILT_SK))
.send()
.await
.map_err(|e| format!("index {}: {e}", spec.name))?;
let built_at = built
.item()
.and_then(|i| i.get("at"))
.and_then(|v| v.as_s().ok())
.cloned();
if needs_rebuild(built_at.as_deref(), Utc::now()) {
rebuild(dynamo, table, spec).await?;
}
let mut out = BTreeSet::new();
let mut start: Option<Item> = None;
loop {
let page = dynamo
.query()
.table_name(table)
.key_condition_expression("pk = :p")
.expression_attribute_values(":p", s(&pk))
.projection_expression("sk")
.set_exclusive_start_key(start.take())
.send()
.await
.map_err(|e| format!("index {}: {e}", spec.name))?;
for i in page.items() {
if let Some(m) = i.get("sk").and_then(|v| v.as_s().ok()) {
if m != BUILT_SK {
out.insert(m.clone());
}
}
}
match page.last_evaluated_key() {
Some(k) if !k.is_empty() => start = Some(k.clone()),
_ => break,
}
}
Ok(out.into_iter().collect())
}

/// One full scan for the indexed records, written as members, then the
/// rebuild time. Every page is read: a cap would leave members out.
async fn rebuild(dynamo: &DynamoClient, table: &str, spec: &Spec<'_>) -> Result<(), String> {
let pk = partition(spec.name);
let mut found = BTreeSet::new();
let (mut start, mut pages): (Option<Item>, usize) = (None, 0);
loop {
let mut scan = dynamo
.scan()
.table_name(table)
.filter_expression(spec.filter)
.set_exclusive_start_key(start.take());
for (k, v) in &spec.values {
scan = scan.expression_attribute_values(*k, v.clone());
}
let page = scan
.send()
.await
.map_err(|e| format!("index {} rebuild: {e}", spec.name))?;
pages += 1;
found.extend(page.items().iter().filter_map(spec.member_of));
match page.last_evaluated_key() {
Some(k) if !k.is_empty() => start = Some(k.clone()),
_ => break,
}
}
let members: Vec<String> = found.into_iter().collect();
for chunk in members.chunks(25) {
let writes = chunk
.iter()
.filter_map(|m| {
PutRequest::builder()
.item("pk", s(&pk))
.item("sk", s(m))
.build()
.ok()
.map(|p| WriteRequest::builder().put_request(p).build())
})
.collect::<Vec<_>>();
let mut pending = Some(HashMap::from([(table.to_string(), writes)]));
for _ in 0..5 {
let Some(req) = pending.take().filter(|r| r.values().any(|w| !w.is_empty())) else {
break;
};
let r = dynamo
.batch_write_item()
.set_request_items(Some(req))
.send()
.await
.map_err(|e| format!("index {} rebuild write: {e}", spec.name))?;
pending = r.unprocessed_items().cloned();
}
if pending.is_some_and(|r| r.values().any(|w| !w.is_empty())) {
return Err(format!(
"index {} rebuild: some members were not written",
spec.name
));
}
}
dynamo
.put_item()
.table_name(table)
.item("pk", s(&pk))
.item("sk", s(BUILT_SK))
.item("at", s(&Utc::now().to_rfc3339()))
.item("members", AttributeValue::N(members.len().to_string()))
.send()
.await
.map_err(|e| format!("index {} rebuild mark: {e}", spec.name))?;
info!(
index = spec.name,
pages,
members = members.len(),
"Rebuilt a settings index from a full scan"
);
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn a_never_built_index_is_rebuilt() {
assert!(needs_rebuild(None, Utc::now()));
assert!(needs_rebuild(Some("not a time"), Utc::now()));
}

#[test]
fn a_recent_rebuild_is_kept_and_an_old_one_is_redone() {
let now = DateTime::parse_from_rfc3339("2026-11-07T12:00:00Z")
.unwrap()
.with_timezone(&Utc);
assert!(!needs_rebuild(Some("2026-11-07T00:00:01Z"), now));
assert!(needs_rebuild(Some("2026-11-06T12:00:00Z"), now));
}

#[test]
fn the_index_lives_in_its_own_partition() {
assert_eq!(partition("LOG_AUTOFIX"), "INDEX#LOG_AUTOFIX");
}
}
12 changes: 12 additions & 0 deletions services/gateway/src/routes/hybrid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,18 @@ pub async fn claim_name(
)
});
}
// The 15-minute name check reads reservations from this index, not a
// table scan. A failed add is found by the index's daily rebuild.
if let Err(e) = common::settings_index::add(
&state.dynamo,
&state.config.settings_table_name,
common::hybrid::name_check::NAME_INDEX,
&name,
)
.await
{
tracing::warn!(name = %name, error = %e, "Could not index the name reservation");
}
// The setup page for the name. CREATE: a name with any record already
// in the zone (made outside this API) is refused, never overwritten.
if let Err(e) = apply_dns(&name, &names::plan_claim(&name)).await {
Expand Down
Loading
Loading