From 3544b07848e5dbf231d72be5a0f5a07fed7321a5 Mon Sep 17 00:00:00 2001 From: Austin Barrington Date: Tue, 10 Nov 2026 17:06:22 +0000 Subject: [PATCH 1/4] Golden test of replicated cluster mutations --- .../tests/compat/cluster_replication_tests.rs | 171 +++++++++ hyperbytedb/tests/compat/main.rs | 1 + .../compat/testdata/replicated_mutations.json | 355 ++++++++++++++++++ 3 files changed, 527 insertions(+) create mode 100644 hyperbytedb/tests/compat/cluster_replication_tests.rs create mode 100644 hyperbytedb/tests/compat/testdata/replicated_mutations.json diff --git a/hyperbytedb/tests/compat/cluster_replication_tests.rs b/hyperbytedb/tests/compat/cluster_replication_tests.rs new file mode 100644 index 0000000..7432ce2 --- /dev/null +++ b/hyperbytedb/tests/compat/cluster_replication_tests.rs @@ -0,0 +1,171 @@ +//! Golden record of what each cluster statement replicates (non-Raft +//! cluster): the mutation `PeerQueryService` sends for every statement +//! kind, and the statement's own result. Regenerate the fixture with +//! `UPDATE_GOLDEN=1` only when a change to it is intended. + +use std::sync::Arc; +use std::time::Duration; + +use async_trait::async_trait; +use hyperbytedb::adapters::http::router::QueryService; +use hyperbytedb::application::peer_query_service::PeerQueryService; +use hyperbytedb::domain::cluster::types::MutationRequest; +use hyperbytedb::error::HyperbytedbError; +use hyperbytedb::ports::replication::{OutboundReplicationBatch, ReplicationPort}; +use serial_test::serial; + +use super::TestContext; + +/// Records every mutation it is asked to replicate. +#[derive(Default)] +struct RecordingReplication { + seen: parking_lot::Mutex>, +} + +#[async_trait] +impl ReplicationPort for RecordingReplication { + fn replicate_write( + self: Arc, + _batch: OutboundReplicationBatch, + ) -> Result<(), HyperbytedbError> { + Ok(()) + } + + async fn replicate_write_sync( + self: Arc, + _batch: OutboundReplicationBatch, + _required_acks: usize, + _timeout: Duration, + ) -> Result<(), HyperbytedbError> { + Ok(()) + } + + fn replicate_mutation(self: Arc, req: MutationRequest, _targets: Option>) { + self.seen.lock().push(req); + } + + async fn replicate_mutation_sync( + self: Arc, + req: MutationRequest, + ) -> Result<(), HyperbytedbError> { + self.seen.lock().push(req); + Ok(()) + } + + async fn replication_peer_count(&self, _self_node_id: u64) -> usize { + 0 + } +} + +/// One statement per replicating arm, plus the cross-database CREATE +/// RETENTION POLICY and an invalid SET. Each is run against `mydb`. +const STATEMENTS: &[&str] = &[ + "CREATE DATABASE mydb", + "CREATE DATABASE otherdb", + "CREATE RETENTION POLICY week ON mydb DURATION 7d REPLICATION 1", + "CREATE RETENTION POLICY r ON otherdb DURATION 1d REPLICATION 1", + "ALTER RETENTION POLICY week ON mydb DURATION 14d", + "CREATE USER bob WITH PASSWORD 'secret-one'", + "SET PASSWORD FOR bob = 'secret-two'", + "GRANT READ ON mydb TO bob", + "REVOKE READ ON mydb FROM bob", + "CREATE CONTINUOUS QUERY cq1 ON mydb BEGIN SELECT mean(v) INTO cpu_1m FROM cpu GROUP BY time(1m) END", + "DROP CONTINUOUS QUERY cq1 ON mydb", + r#"CREATE MATERIALIZED VIEW "mv1" ON "mydb" AS SELECT mean("v") AS "mean_v" INTO "cpu_5m" FROM "cpu" GROUP BY time(5m), *"#, + r#"DROP MATERIALIZED VIEW "mv1" ON "mydb""#, + "DELETE FROM cpu WHERE host = 'a'", + "DROP SERIES FROM cpu WHERE host = 'b'", + "DROP MEASUREMENT cpu", + "SET query_pool_size = 5", + "SET query_pool_size = 0", + "DROP USER bob", + "DROP RETENTION POLICY week ON mydb", + "DROP DATABASE otherdb", +]; + +/// Replace each `password_hash` with a placeholder, after checking it is an +/// argon2 hash and not the plain password, and each `created_at` (wall +/// clock) with another. +fn mask_hashes(v: &mut serde_json::Value) { + match v { + serde_json::Value::Object(map) => { + for (k, val) in map.iter_mut() { + if k == "password_hash" { + let h = val.as_str().expect("password_hash is a string"); + assert!(h.starts_with("$argon2"), "not an argon2 hash: {h}"); + assert!(!h.contains("secret"), "plain password replicated"); + *val = serde_json::Value::String("".into()); + } else if k == "created_at" { + *val = serde_json::Value::String("