feat(svc): add a combined mode that answers without a router hop

This commit is contained in:
Hampus committed 2026-10-24 20:55:23 +02:00
1 parent d3345da6c0
commit 0ffbb77fcd
9 files changed
+408 -58

No files matched your search

+5
View File
@@ -36,5 +36,10 @@ async fn main() -> anyhow::Result<()> {
let shard = GifsShard::new()?;
fluxer_svc::shard::run_shard(&config, shard, transport).await
}
Mode::Combined => {
let router = GifsRouter::new(config.cache_max_entries, config.cache_ttl);
let shard = GifsShard::new()?;
fluxer_svc::combined::run_combined(&config, router, shard, transport).await
}
}
}
+32 -24
View File
@@ -27,31 +27,39 @@ async fn main() -> anyhow::Result<()> {
fluxer_svc::router::run_router(&config, router, transport).await
}
Mode::Shard => {
let shard = match config.database_backend {
DatabaseBackend::Postgres => {
let postgres_config =
fluxer_svc::postgres::PostgresConfig::from_service_config(&config);
let pool = fluxer_svc::postgres::connect(&postgres_config).await?;
let kv = fluxer_svc::postgres::KvClient::new(pool, &postgres_config)?;
MessagesShard::new_postgres(kv, transport.clone())?
}
DatabaseBackend::Cassandra => {
#[cfg(feature = "scylla")]
{
let scylla_config =
fluxer_svc::scylla::ScyllaConfig::from_service_config(&config);
let db = fluxer_svc::scylla::connect(&scylla_config).await?;
MessagesShard::new_scylla(db, transport.clone()).await?
}
#[cfg(not(feature = "scylla"))]
{
anyhow::bail!(
"FLUXER_DATABASE_BACKEND=cassandra requires the scylla feature"
);
}
}
};
let shard = build_shard(&config, &transport).await?;
fluxer_svc::shard::run_shard(&config, shard, transport).await
}
Mode::Combined => {
let shard = build_shard(&config, &transport).await?;
let router = MessagesRouter::new();
fluxer_svc::combined::run_combined(&config, router, shard, transport).await
}
}
}
async fn build_shard(
config: &ServiceConfig,
transport: &NatsTransport,
) -> anyhow::Result<MessagesShard<NatsTransport>> {
match config.database_backend {
DatabaseBackend::Postgres => {
let postgres_config = fluxer_svc::postgres::PostgresConfig::from_service_config(config);
let pool = fluxer_svc::postgres::connect(&postgres_config).await?;
let kv = fluxer_svc::postgres::KvClient::new(pool, &postgres_config)?;
MessagesShard::new_postgres(kv, transport.clone())
}
DatabaseBackend::Cassandra => {
#[cfg(feature = "scylla")]
{
let scylla_config = fluxer_svc::scylla::ScyllaConfig::from_service_config(config);
let db = fluxer_svc::scylla::connect(&scylla_config).await?;
MessagesShard::new_scylla(db, transport.clone()).await
}
#[cfg(not(feature = "scylla"))]
{
anyhow::bail!("FLUXER_DATABASE_BACKEND=cassandra requires the scylla feature");
}
}
}
}
+5
View File
@@ -37,5 +37,10 @@ async fn main() -> anyhow::Result<()> {
let shard = SnowflakesShard::new(config.shard_id)?;
fluxer_svc::shard::run_shard(&config, shard, transport).await
}
Mode::Combined => {
let router = router_impl::SnowflakesRouter::new(config.shard_count);
let shard = SnowflakesShard::new(config.shard_id)?;
fluxer_svc::combined::run_combined(&config, router, shard, transport).await
}
}
}
+310
View File
@@ -0,0 +1,310 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
use crate::config::ServiceConfig;
use crate::metrics::ServiceMetrics;
use crate::router::{
Inflight, MAX_ROUTER_REQUEST_BYTES, RouterService, SHARD_REQUEST_TIMEOUT, coalesce,
};
use crate::shard::{Endpoint, ShardService, serve, shard_metrics};
use crate::transport::{Transport, TransportMessage, reply_bytes, reply_json_error};
use bytes::Bytes;
use std::sync::Arc;
use std::time::Instant;
use tracing::warn;
#[derive(Debug)]
enum CallError {
Timeout,
Handler(anyhow::Error),
Encode(serde_json::Error),
}
impl CallError {
fn code(&self) -> &'static str {
match self {
Self::Timeout => "shard_unavailable",
Self::Handler(_) => "shard_handler_error",
Self::Encode(_) => "encode_error",
}
}
}
struct Combined<R, S> {
router: R,
shard: Arc<S>,
inflight: Inflight<Bytes, CallError>,
}
pub async fn run_combined<R, S>(
config: &ServiceConfig,
router: R,
shard: S,
transport: impl Transport,
) -> anyhow::Result<()>
where
R: RouterService,
R::Request: Clone,
S: ShardService<Request = R::Request, Response = R::Response>,
{
if config.shard_count != 1 {
anyhow::bail!(
"FLUXER_SVC_MODE=combined needs FLUXER_SVC_SHARD_COUNT=1, got {}",
config.shard_count
);
}
let name = router.service_name().to_owned();
let shard = Arc::new(shard);
let metrics = shard_metrics(&shard);
let service = Arc::new(Combined {
router,
shard,
inflight: Inflight::default(),
});
let handler_transport = transport.clone();
let handler_metrics = metrics.clone();
serve(
config,
Endpoint {
subject: format!("svc.{name}"),
queue_group: Some(format!("{name}-router")),
name,
},
metrics,
transport,
move |msg| {
handle_request(
msg,
handler_transport.clone(),
service.clone(),
handler_metrics.clone(),
)
},
)
.await
}
async fn handle_request<R, S, T>(
msg: T::Message,
transport: T,
service: Arc<Combined<R, S>>,
metrics: Arc<ServiceMetrics>,
) where
R: RouterService,
R::Request: Clone,
S: ShardService<Request = R::Request, Response = R::Response>,
T: Transport,
{
let request_start = Instant::now();
metrics.record_request();
if msg.payload().len() > MAX_ROUTER_REQUEST_BYTES {
warn!(
payload_bytes = msg.payload().len(),
max_payload_bytes = MAX_ROUTER_REQUEST_BYTES,
"rejecting oversized request"
);
metrics.record_request_error();
reply_json_error(&msg, &transport, "request_too_large").await;
return;
}
let request: R::Request = match serde_json::from_slice(msg.payload()) {
Ok(request) => request,
Err(err) => {
warn!(error = %err, "failed to decode incoming request");
metrics.record_request_error();
reply_json_error(&msg, &transport, "decode_error").await;
return;
}
};
let broadcast = R::is_broadcast_request(&request);
if R::CACHES_RESPONSES
&& !broadcast
&& let Some(cached) = service.router.l1_lookup(&request)
{
metrics.record_cache_hit();
let result = serde_json::to_vec(&cached)
.map(Bytes::from)
.map_err(|err| Arc::new(CallError::Encode(err)));
metrics.record_request_duration(request_start.elapsed().as_millis() as u64);
reply(&msg, &transport, &metrics, result).await;
return;
}
metrics.record_cache_miss();
let coalesce_key = if broadcast {
None
} else {
R::coalesce_key(&request)
};
let result = match coalesce_key {
Some(coalesce_key) => {
let key = (R::route_key(&request), coalesce_key);
let call_service = service.clone();
coalesce(&service.inflight, key, async move {
call(&call_service, request, broadcast).await
})
.await
}
None => call(&service, request, broadcast).await.map_err(Arc::new),
};
metrics.record_request_duration(request_start.elapsed().as_millis() as u64);
reply(&msg, &transport, &metrics, result).await;
}
async fn call<R, S>(
service: &Combined<R, S>,
request: R::Request,
broadcast: bool,
) -> Result<Bytes, CallError>
where
R: RouterService,
R::Request: Clone,
S: ShardService<Request = R::Request, Response = R::Response>,
{
let cache_request = (R::CACHES_RESPONSES && !broadcast).then(|| request.clone());
let response = tokio::time::timeout(SHARD_REQUEST_TIMEOUT, service.shard.handle(request))
.await
.map_err(|_| CallError::Timeout)?
.map_err(CallError::Handler)?;
if let Some(cache_request) = cache_request {
service.router.l1_insert(&cache_request, &response);
}
serde_json::to_vec(&response)
.map(Bytes::from)
.map_err(CallError::Encode)
}
async fn reply(
msg: &impl TransportMessage,
transport: &impl Transport,
metrics: &ServiceMetrics,
result: Result<Bytes, Arc<CallError>>,
) {
match result {
Ok(payload) => {
if msg.has_reply() {
reply_bytes(msg, transport, &payload).await;
}
}
Err(err) => {
match err.as_ref() {
CallError::Timeout => warn!(subject = msg.subject(), "request timed out"),
CallError::Handler(error) => warn!(error = %error, "handler returned error"),
CallError::Encode(error) => warn!(error = %error, "failed to encode response"),
}
metrics.record_request_error();
reply_json_error(msg, transport, err.code()).await;
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::{DatabaseBackend, Mode};
use crate::transport::InMemoryTransport;
use serde::{Deserialize, Serialize};
use std::time::Duration;
#[derive(Clone, Serialize, Deserialize)]
struct EchoRequest {
key: String,
}
#[derive(Clone, Serialize, Deserialize)]
struct EchoResponse {
key: String,
}
struct EchoRouter;
impl RouterService for EchoRouter {
type Request = EchoRequest;
type Response = EchoResponse;
fn service_name(&self) -> &str {
"echo"
}
fn route_key(request: &EchoRequest) -> String {
request.key.clone()
}
fn coalesce_key(request: &EchoRequest) -> Option<String> {
Some(request.key.clone())
}
}
struct EchoShard;
impl ShardService for EchoShard {
type Request = EchoRequest;
type Response = EchoResponse;
fn service_name(&self) -> &str {
"echo"
}
async fn handle(&self, request: EchoRequest) -> anyhow::Result<EchoResponse> {
if request.key.is_empty() {
anyhow::bail!("empty key");
}
Ok(EchoResponse { key: request.key })
}
}
#[tokio::test]
async fn combined_answers_the_service_subject_from_the_shard_handler() {
let transport = InMemoryTransport::new();
let server_transport = transport.clone();
let server = tokio::spawn(async move {
run_combined(&test_config(), EchoRouter, EchoShard, server_transport).await
});
tokio::time::sleep(Duration::from_millis(25)).await;
let response = transport
.request("svc.echo", br#"{"key":"a"}"#, Duration::from_secs(1))
.await
.unwrap();
assert_eq!(response, br#"{"key":"a"}"#);
let error = transport
.request("svc.echo", br#"{"key":""}"#, Duration::from_secs(1))
.await
.unwrap();
assert_eq!(error, br#"{"error":"shard_handler_error"}"#);
server.abort();
}
fn test_config() -> ServiceConfig {
ServiceConfig {
service_name: "echo".to_owned(),
mode: Mode::Combined,
database_backend: DatabaseBackend::Postgres,
shard_id: 0,
shard_count: 1,
listen_addr: "127.0.0.1:0".parse().unwrap(),
nats_url: "memory".to_owned(),
nats_auth_token: None,
cache_max_entries: 100,
cache_ttl: Duration::from_secs(30),
max_concurrent_requests: 4,
scylla_hosts: Vec::new(),
scylla_keyspace: "fluxer".to_owned(),
scylla_username: None,
scylla_password: None,
postgres_url: None,
postgres_host: "127.0.0.1".to_owned(),
postgres_port: 5432,
postgres_database: "fluxer".to_owned(),
postgres_username: "fluxer".to_owned(),
postgres_password: Some("fluxer".to_owned()),
postgres_ssl: false,
postgres_ssl_ca: None,
postgres_max_connections: 1,
postgres_kv_table: "fluxer_kv".to_owned(),
postgres_prepared_statements: true,
}
}
}
+2
View File
@@ -43,6 +43,7 @@ pub struct ServiceConfig {
pub enum Mode {
Router,
Shard,
Combined,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -77,6 +78,7 @@ impl ServiceConfig {
let mode = match optional_from(&get, "FLUXER_SVC_MODE").as_deref() {
None | Some("router") => Mode::Router,
Some("shard") => Mode::Shard,
Some("combined") => Mode::Combined,
Some(other) => anyhow::bail!("unsupported FLUXER_SVC_MODE: {other}"),
};
+1
View File
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
pub mod combined;
pub mod config;
pub mod hash_ring;
pub mod metrics;
+15 -8
View File
@@ -19,10 +19,10 @@ use tracing::{debug, info, warn};
pub const SHARD_REQUEST_TIMEOUT: Duration = Duration::from_secs(5);
const MAX_BROADCAST_CONCURRENCY: usize = 32;
const MAX_ROUTER_REQUEST_BYTES: usize = 2 * 1024 * 1024;
pub(crate) const MAX_ROUTER_REQUEST_BYTES: usize = 2 * 1024 * 1024;
type InflightKey = (String, String);
type InflightCall = Shared<BoxFuture<'static, Result<Vec<u8>, Arc<anyhow::Error>>>>;
type Inflight = Arc<Mutex<HashMap<InflightKey, InflightCall>>>;
type InflightCall<V, E> = Shared<BoxFuture<'static, Result<V, Arc<E>>>>;
pub(crate) type Inflight<V, E> = Arc<Mutex<HashMap<InflightKey, InflightCall<V, E>>>>;
pub trait RouterService: Send + Sync + 'static {
type Request: serde::Serialize + serde::de::DeserializeOwned + Send + Sync + 'static;
@@ -157,9 +157,15 @@ async fn forward_to_all_shards<S: RouterService>(
}
}
async fn coalesce<F>(inflight: &Inflight, key: InflightKey, call: F) -> anyhow::Result<Vec<u8>>
pub(crate) async fn coalesce<V, E, F>(
inflight: &Inflight<V, E>,
key: InflightKey,
call: F,
) -> Result<V, Arc<E>>
where
F: Future<Output = anyhow::Result<Vec<u8>>> + Send + 'static,
V: Clone + Send + Sync + 'static,
E: Send + Sync + 'static,
F: Future<Output = Result<V, E>> + Send + 'static,
{
let shared = {
let mut calls = inflight.lock().unwrap();
@@ -177,7 +183,7 @@ where
})
.clone()
};
shared.await.map_err(|err| anyhow::anyhow!("{err}"))
shared.await
}
async fn reply_json_response(
@@ -204,7 +210,7 @@ async fn handle_router_request<S, T>(
transport: T,
service: Arc<S>,
ring: Arc<HashRing>,
inflight: Inflight,
inflight: Inflight<Vec<u8>, anyhow::Error>,
metrics: Arc<ServiceMetrics>,
) where
S: RouterService,
@@ -272,6 +278,7 @@ async fn handle_router_request<S, T>(
.await
})
.await
.map_err(|err| anyhow::anyhow!("{err}"))
} else {
dispatch_to_shard::<S>(
&transport,
@@ -337,7 +344,7 @@ where
let metrics = Arc::new(ServiceMetrics::default());
metrics.init();
let inflight = Inflight::default();
let inflight = Inflight::<Vec<u8>, anyhow::Error>::default();
let mut tasks = JoinSet::new();
+5
View File
@@ -51,5 +51,10 @@ async fn main() -> anyhow::Result<()> {
let shard = UnfurlShard::new(config.cache_ttl);
fluxer_svc::shard::run_shard(&config, shard, transport).await
}
Mode::Combined => {
let router = UnfurlRouter::new(config.cache_max_entries, config.cache_ttl);
let shard = UnfurlShard::new(config.cache_ttl);
fluxer_svc::combined::run_combined(&config, router, shard, transport).await
}
}
}
+33 -26
View File
@@ -29,33 +29,40 @@ async fn main() -> anyhow::Result<()> {
match config.mode {
Mode::Router => fluxer_svc::router::run_router(&config, UsersRouter, transport).await,
Mode::Shard => {
pseudonym::configure_from_env()?;
let shard = match config.database_backend {
DatabaseBackend::Postgres => {
let postgres_config =
fluxer_svc::postgres::PostgresConfig::from_service_config(&config);
let pool = fluxer_svc::postgres::connect(&postgres_config).await?;
let kv = fluxer_svc::postgres::KvClient::new(pool, &postgres_config)?;
UsersShard::new_postgres(kv, config.cache_max_entries, config.cache_ttl)
}
DatabaseBackend::Cassandra => {
#[cfg(feature = "scylla")]
{
let scylla_config =
fluxer_svc::scylla::ScyllaConfig::from_service_config(&config);
let db = fluxer_svc::scylla::connect(&scylla_config).await?;
UsersShard::new_scylla(db, config.cache_max_entries, config.cache_ttl)
.await?
}
#[cfg(not(feature = "scylla"))]
{
anyhow::bail!(
"FLUXER_DATABASE_BACKEND=cassandra requires the scylla feature"
);
}
}
};
let shard = build_shard(&config).await?;
fluxer_svc::shard::run_shard(&config, shard, transport).await
}
Mode::Combined => {
let shard = build_shard(&config).await?;
fluxer_svc::combined::run_combined(&config, UsersRouter, shard, transport).await
}
}
}
async fn build_shard(config: &ServiceConfig) -> anyhow::Result<UsersShard> {
pseudonym::configure_from_env()?;
match config.database_backend {
DatabaseBackend::Postgres => {
let postgres_config = fluxer_svc::postgres::PostgresConfig::from_service_config(config);
let pool = fluxer_svc::postgres::connect(&postgres_config).await?;
let kv = fluxer_svc::postgres::KvClient::new(pool, &postgres_config)?;
Ok(UsersShard::new_postgres(
kv,
config.cache_max_entries,
config.cache_ttl,
))
}
DatabaseBackend::Cassandra => {
#[cfg(feature = "scylla")]
{
let scylla_config = fluxer_svc::scylla::ScyllaConfig::from_service_config(config);
let db = fluxer_svc::scylla::connect(&scylla_config).await?;
UsersShard::new_scylla(db, config.cache_max_entries, config.cache_ttl).await
}
#[cfg(not(feature = "scylla"))]
{
anyhow::bail!("FLUXER_DATABASE_BACKEND=cassandra requires the scylla feature");
}
}
}
}