Skip to content

armgit / sie

Public Imported from GitHub
0

Open-source inference server and production cluster for all the models your agent needs.

feat(gateway): publish load-only model readiness work (#541)

Krisztian Gajdarbd945d6c4ce7
diff --git a/packages/sie_gateway/src/queue/dispatch.rs b/packages/sie_gateway/src/queue/dispatch.rs
index ea914138d..ab9a33c3b 100644
--- a/packages/sie_gateway/src/queue/dispatch.rs
+++ b/packages/sie_gateway/src/queue/dispatch.rs
@@ -174,6 +174,20 @@ impl From<String> for DispatchError {
/// full behavioural documentation.
#[async_trait]
pub trait WorkDispatcher: Send + Sync {
+ /// Request readiness without inference. Only transports whose worker
+ /// settlement supports no-result load items may implement this operation.
+ #[allow(dead_code)] // Called by the subsequent cluster fallback routing delivery.
+ async fn publish_model_load(
+ &self,
+ _target: PublishTarget,
+ _engine: &str,
+ _bundle_config_hash: &str,
+ ) -> Result<(String, DispatchDurability), DispatchError> {
+ Err(DispatchError::Other(
+ "load-only dispatch is not supported by this transport".to_string(),
+ ))
+ }
+
#[allow(clippy::too_many_arguments)]
/// `model` is the route the work runs on. `display_model` is the model id
/// the caller asked for, which logs and accounting downstream report;
@@ -345,6 +359,17 @@ pub trait WorkDispatcher: Send + Sync {
#[async_trait]
impl WorkDispatcher for WorkPublisher {
+ async fn publish_model_load(
+ &self,
+ target: PublishTarget,
+ engine: &str,
+ bundle_config_hash: &str,
+ ) -> Result<(String, DispatchDurability), DispatchError> {
+ WorkPublisher::publish_model_load(self, target, engine, bundle_config_hash)
+ .await
+ .map_err(DispatchError::from)
+ }
+
async fn publish_work(
self: Arc<Self>,
target: PublishTarget,
@@ -778,6 +803,23 @@ mod performance_tests {
}
}
+ #[tokio::test]
+ async fn load_only_dispatch_refuses_transports_without_no_result_settlement() {
+ let dispatcher = AckCompletingDispatcher::default();
+ let target = PublishTarget::Pool {
+ pool: "local".into(),
+ machine_profile: "l4".into(),
+ bundle: "default".into(),
+ model: "acme/warm".into(),
+ };
+ let result = dispatcher
+ .publish_model_load(target, "pytorch", "exact-hash")
+ .await;
+ assert!(matches!(result, Err(DispatchError::Other(message))
+ if message == "load-only dispatch is not supported by this transport"));
+ assert_eq!(dispatcher.cancel_calls.load(Ordering::SeqCst), 0);
+ }
+
fn demand_tracker_with_lanes(
lanes: &[PhysicalLane],
) -> (Arc<DemandTracker>, Arc<AckCompletingDispatcher>) {
diff --git a/packages/sie_gateway/src/queue/publisher.rs b/packages/sie_gateway/src/queue/publisher.rs
index 827bc3bcd..572b60b69 100644
--- a/packages/sie_gateway/src/queue/publisher.rs
+++ b/packages/sie_gateway/src/queue/publisher.rs
@@ -2923,6 +2923,85 @@ impl WorkPublisher {
});
}
+ /// Queue a model load without input, inference or a result collector.
+ ///
+ /// The sidecar ACKs `load` after readiness and emits no result. Durability
+ /// here confirms only broker acceptance; worker health remains the source
+ /// of readiness. No local-ingest transport is involved.
+ #[allow(dead_code)] // Called by the subsequent cluster fallback routing delivery.
+ pub async fn publish_model_load(
+ &self,
+ target: PublishTarget,
+ engine: &str,
+ bundle_config_hash: &str,
+ ) -> Result<(String, DispatchDurability), String> {
+ self.ensure_stream(target.pool()).await?;
+ self.check_backpressure(target.pool())?;
+ let request_id = uuid::Uuid::now_v7().to_string();
+ let work_item_id = canonical_work_item_id(&request_id, 0);
+ let reply_subject = format!("_INBOX.{}.{}", self.router_id, request_id);
+ let timestamp = std::time::SystemTime::now()
+ .duration_since(std::time::UNIX_EPOCH)
+ .unwrap_or_default()
+ .as_secs_f64();
+ let item = WorkItemRef {
+ work_item_id: &work_item_id,
+ request_id: &request_id,
+ item_index: 0,
+ total_items: 1,
+ operation: "load",
+ model_id: target.model(),
+ profile_id: "default",
+ display_model: None,
+ engine,
+ pool_name: target.pool(),
+ admission_pool: "",
+ machine_profile: target.machine_profile(),
+ item: None,
+ payload_ref: None,
+ output_types: None,
+ instruction: None,
+ is_query: false,
+ options: None,
+ query_item: None,
+ query_payload_ref: None,
+ score_items: None,
+ labels: None,
+ output_schema: None,
+ generate: None,
+ routing_key: None,
+ prompt_cache_key: None,
+ bundle_config_hash,
+ router_id: &self.router_id,
+ reply_subject: &reply_subject,
+ timestamp,
+ deadline: Some(timestamp + self.result_timeout.as_secs_f64()),
+ accepts_result_chunks: false,
+ traceparent: None,
+ tracestate: None,
+ };
+ let encoded = rmp_serde::to_vec_named(&item).map_err(|error| {
+ warn!(error = %error, "failed to encode model load envelope");
+ "model load envelope could not be encoded".to_string()
+ })?;
+ let ack = self
+ .jetstream
+ .publish(target.subject(), encoded.into())
+ .await
+ .map_err(|error| {
+ warn!(error = %error.kind(), "failed to publish model load envelope");
+ "model load could not be published".to_string()
+ })?;
+ let durability = DispatchDurability::from_future(async move {
+ tokio::time::timeout(PUBLISH_ACK_COMPLETION_TIMEOUT, ack)
+ .await
+ .map_err(|_| "model load publish acknowledgement timed out".to_string())?
+ .map(|_| ())
+ .map_err(|_| "model load publish acknowledgement failed".to_string())
+ });
+ Ok((request_id, durability))
+ }
+
/// Decompose a request into work items and publish to JetStream.
#[allow(clippy::too_many_arguments)]
pub async fn publish_work(
@@ -9038,6 +9117,179 @@ mod tests {
);
}
+ #[tokio::test]
+ async fn load_only_publish_is_durable_without_inputs_or_result_collectors() {
+ let Ok(url) = std::env::var("NATS_URL") else {
+ assert_ne!(
+ std::env::var("SIE_RUN_NATS_PUBLISHER_TEST").as_deref(),
+ Ok("1"),
+ "mandatory publisher tests require NATS_URL"
+ );
+ return;
+ };
+ let client = async_nats::connect(url)
+ .await
+ .expect("test NATS connection");
+ let pool = format!("itload{}", uuid::Uuid::now_v7().simple());
+ let publisher = Arc::new(WorkPublisher::new(
+ jetstream::new(client.clone()),
+ "load-gateway".to_string(),
+ Arc::new(crate::queue::payload_store::DisabledPayloadStore),
+ Duration::from_secs(60),
+ 1024,
+ WorkStreamConfig {
+ max_age: Duration::from_secs(300),
+ storage: jetstream::stream::StorageType::Memory,
+ num_replicas: 1,
+ },
+ ));
+ let context = jetstream::new(client.clone());
+ let mut stream = context
+ .get_or_create_stream(jetstream::stream::Config {
+ name: stream_name(&pool),
+ subjects: vec![format!("sie.work.{pool}.*.*.*")],
+ retention: jetstream::stream::RetentionPolicy::WorkQueue,
+ storage: jetstream::stream::StorageType::Memory,
+ max_age: Duration::from_secs(300),
+ max_messages: 100_000,
+ discard: jetstream::stream::DiscardPolicy::New,
+ ..Default::default()
+ })
+ .await
+ .unwrap();
+ stream
+ .create_consumer(jetstream::consumer::pull::Config {
+ durable_name: Some("warm-worker".into()),
+ filter_subject: format!("sie.work.{pool}.*.*.*"),
+ ..Default::default()
+ })
+ .await
+ .unwrap();
+ // Direct work belongs to the worker stream, never the pool stream.
+ let worker_stream_name = format!("WORK_WORKER_{pool}");
+ let worker_stream = context
+ .get_or_create_stream(jetstream::stream::Config {
+ name: worker_stream_name.clone(),
+ subjects: vec![format!("sie.work.{pool}.l4.default.*.warm-worker")],
+ retention: jetstream::stream::RetentionPolicy::WorkQueue,
+ storage: jetstream::stream::StorageType::Memory,
+ max_age: Duration::from_secs(300),
+ discard: jetstream::stream::DiscardPolicy::New,
+ ..Default::default()
+ })
+ .await
+ .unwrap();
+ worker_stream
+ .create_consumer(jetstream::consumer::pull::Config {
+ durable_name: Some("warm-worker".into()),
+ ..Default::default()
+ })
+ .await
+ .unwrap();
+ let mut subscriber = client
+ .subscribe(format!("sie.work.{pool}.>"))
+ .await
+ .unwrap();
+ client.flush().await.unwrap();
+ for worker in [false, true] {
+ let pool_target = PublishTarget::Pool {
+ pool: pool.clone(),
+ machine_profile: "l4".into(),
+ bundle: "default".into(),
+ model: "acme/warm".into(),
+ };
+ let target = if worker {
+ PublishTarget::Worker {
+ pool: pool.clone(),
+ machine_profile: "l4".into(),
+ bundle: "default".into(),
+ model: "acme/warm".into(),
+ worker_id: "warm-worker".into(),
+ }
+ } else {
+ pool_target
+ };
+ let expected_subject = target.subject();
+ let (request_id, durability) =
+ crate::queue::dispatch::WorkDispatcher::publish_model_load(
+ publisher.as_ref(),
+ target,
+ "pytorch",
+ "exact-config-hash",
+ )
+ .await
+ .unwrap();
+ durability
+ .wait()
+ .await
+ .expect("load publish acknowledgement");
+ let message = tokio::time::timeout(Duration::from_secs(2), subscriber.next())
+ .await
+ .unwrap()
+ .unwrap();
+ assert_eq!(message.subject.as_str(), expected_subject);
+ let item: WorkItem = rmp_serde::from_slice(&message.payload).unwrap();
+ assert_eq!(item.operation, "load");
+ assert_eq!(item.model_id, "acme/warm");
+ assert_eq!(item.profile_id, "default");
+ assert_eq!(item.request_id, request_id);
+ assert_eq!(item.work_item_id, canonical_work_item_id(&request_id, 0));
+ assert_eq!((item.item_index, item.total_items), (0, 1));
+ assert_eq!(item.bundle_config_hash, "exact-config-hash");
+ assert_eq!(item.engine, "pytorch");
+ assert_eq!(item.machine_profile, "l4");
+ assert_eq!(item.pool_name, pool);
+ assert_eq!(item.router_id, "load-gateway");
+ assert!(item.reply_subject.starts_with("_INBOX.load-gateway."));
+ assert_eq!(item.deadline.unwrap() - item.timestamp, 60.0);
+ assert!(item.item.is_none() && item.payload_ref.is_none());
+ assert!(item.query_item.is_none() && item.query_payload_ref.is_none());
+ assert!(item.score_items.is_none() && item.generate.is_none());
+ assert!(item.options.is_none() && item.output_types.is_none());
+ assert!(!item.accepts_result_chunks);
+ assert!(publisher.pending_results.is_empty() && publisher.pending_streams.is_empty());
+ assert!(publisher.offloaded_payload_keys.is_empty());
+ }
+ // A broker rejection must fail durability with fixed text and must
+ // not leave caller-result or payload state behind.
+ let mut bounded = stream.info().await.unwrap().config.clone();
+ bounded.max_message_size = 1;
+ context.update_stream(bounded).await.unwrap();
+ let target = PublishTarget::Pool {
+ pool: pool.clone(),
+ machine_profile: "l4".into(),
+ bundle: "default".into(),
+ model: "acme/warm".into(),
+ };
+ let (_, durability) = publisher
+ .publish_model_load(target, "pytorch", "exact-config-hash")
+ .await
+ .unwrap();
+ assert_eq!(
+ durability.wait().await.unwrap_err(),
+ "model load publish acknowledgement failed"
+ );
+ assert_eq!(stream.info().await.unwrap().state.messages, 1);
+ assert_eq!(
+ context
+ .get_stream(&worker_stream_name)
+ .await
+ .unwrap()
+ .cached_info()
+ .state
+ .messages,
+ 1
+ );
+ assert!(publisher.pending_results.is_empty() && publisher.pending_streams.is_empty());
+ assert!(publisher.offloaded_payload_keys.is_empty());
+
+ context.delete_stream(worker_stream_name).await.unwrap();
+ jetstream::new(client)
+ .delete_stream(stream_name(&pool))
+ .await
+ .unwrap();
+ }
+
/// NATS-gated: a work item carries `display_model` only when the caller
/// asked for a model other than the route the work runs on, on the batch
/// and on the generation publish path alike.