Unverified Commit 09b26bf6 authored by mohammedabdulwahhab's avatar mohammedabdulwahhab Committed by GitHub
Browse files

fix: refactor to use service discovery (#4092)


Signed-off-by: default avatarmohammedabdulwahhab <furkhan324@berkeley.edu>
parent 04f7579b
...@@ -10,7 +10,7 @@ use crate::transports::nats::DRTNatsClientPrometheusMetrics; ...@@ -10,7 +10,7 @@ use crate::transports::nats::DRTNatsClientPrometheusMetrics;
use crate::{ use crate::{
ErrorContext, ErrorContext,
component::{self, ComponentBuilder, Endpoint, InstanceSource, Namespace}, component::{self, ComponentBuilder, Endpoint, InstanceSource, Namespace},
discovery::DiscoveryClient, discovery::Discovery,
metrics::PrometheusUpdateCallback, metrics::PrometheusUpdateCallback,
metrics::{MetricsHierarchy, MetricsRegistry}, metrics::{MetricsHierarchy, MetricsRegistry},
service::ServiceClient, service::ServiceClient,
...@@ -92,12 +92,13 @@ impl DistributedRuntime { ...@@ -92,12 +92,13 @@ impl DistributedRuntime {
let nats_client_for_metrics = nats_client.clone(); let nats_client_for_metrics = nats_client.clone();
// Initialize discovery client with mock implementation // Initialize discovery backed by KV store
// TODO: Replace MockDiscoveryClient with KeyValueStoreDiscoveryClient or KubeDiscoveryClient
let discovery_client = { let discovery_client = {
use crate::discovery::{MockDiscoveryClient, SharedMockRegistry}; use crate::discovery::KVStoreDiscovery;
let registry = SharedMockRegistry::new(); Arc::new(KVStoreDiscovery::new(
Arc::new(MockDiscoveryClient::new(None, registry)) as Arc<dyn DiscoveryClient> store.clone(),
runtime.primary_token(),
)) as Arc<dyn Discovery>
}; };
let distributed_runtime = Self { let distributed_runtime = Self {
...@@ -242,9 +243,9 @@ impl DistributedRuntime { ...@@ -242,9 +243,9 @@ impl DistributedRuntime {
Namespace::new(self.clone(), name.into(), self.is_static) Namespace::new(self.clone(), name.into(), self.is_static)
} }
/// TODO: Return discovery client when KeyValueDiscoveryClient or KubeDiscoveryClient is implemented /// Returns the discovery interface for service registration and discovery
pub fn discovery_client(&self) -> Result<Arc<dyn DiscoveryClient>> { pub fn discovery(&self) -> Arc<dyn Discovery> {
Err(error!("Discovery client not implemented!")) self.discovery_client.clone()
} }
pub(crate) fn service_client(&self) -> Option<ServiceClient> { pub(crate) fn service_client(&self) -> Option<ServiceClient> {
......
...@@ -9,25 +9,22 @@ ...@@ -9,25 +9,22 @@
use std::sync::Arc; use std::sync::Arc;
use crate::component::{INSTANCE_ROOT_PATH, Instance}; use crate::component::Instance;
use crate::storage::key_value_store::{KeyValueStore, KeyValueStoreManager}; use crate::discovery::{Discovery, DiscoveryQuery};
use crate::transports::etcd::Client as EtcdClient;
pub async fn list_all_instances(client: &KeyValueStoreManager) -> anyhow::Result<Vec<Instance>> { pub async fn list_all_instances(
let Some(bucket) = client.get_bucket(INSTANCE_ROOT_PATH).await? else { discovery_client: Arc<dyn Discovery>,
return Ok(vec![]); ) -> anyhow::Result<Vec<Instance>> {
}; let discovery_instances = discovery_client.list(DiscoveryQuery::AllEndpoints).await?;
let mut instances: Vec<Instance> = discovery_instances
.into_iter()
.filter_map(|di| match di {
crate::discovery::DiscoveryInstance::Endpoint(instance) => Some(instance),
_ => None, // Ignore all other variants (ModelCard, etc.)
})
.collect();
let entries = bucket.entries().await?;
let mut instances = Vec::with_capacity(entries.len());
for (name, bytes) in entries.into_iter() {
match serde_json::from_slice::<Instance>(&bytes) {
Ok(instance) => instances.push(instance),
Err(err) => {
tracing::warn!(%err, key = name, "Failed to parse instance from storage");
}
}
}
instances.sort(); instances.sort();
Ok(instances) Ok(instances)
......
...@@ -96,8 +96,8 @@ pub struct DistributedRuntime { ...@@ -96,8 +96,8 @@ pub struct DistributedRuntime {
tcp_server: Arc<OnceCell<Arc<transports::tcp::server::TcpStreamServer>>>, tcp_server: Arc<OnceCell<Arc<transports::tcp::server::TcpStreamServer>>>,
system_status_server: Arc<OnceLock<Arc<system_status_server::SystemStatusServerInfo>>>, system_status_server: Arc<OnceLock<Arc<system_status_server::SystemStatusServerInfo>>>,
// Service discovery client // Service discovery interface
discovery_client: Arc<dyn discovery::DiscoveryClient>, discovery_client: Arc<dyn discovery::Discovery>,
// local registry for components // local registry for components
// the registry allows us to use share runtime resources across instances of the same component object. // the registry allows us to use share runtime resources across instances of the same component object.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment