Skip to content

Commit

Permalink
efficiently initialize actor ID, remove extra annotation
Browse files Browse the repository at this point in the history
Signed-off-by: Brooks Townsend <brooks@cosmonic.com>
  • Loading branch information
brooksmtownsend committed Apr 12, 2023
1 parent 81df6ab commit 7c980c3
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions src/scaler/spreadscaler.rs
Expand Up @@ -2,6 +2,7 @@ use std::{cmp::Ordering, collections::HashMap};

use anyhow::Result;
use async_trait::async_trait;
use tokio::sync::OnceCell;
use tracing::log::warn;

use crate::{
Expand All @@ -18,7 +19,7 @@ const SCALER_VALUE: &str = "spreadscaler";
const SPREAD_KEY: &str = "wasmcloud/dev/spread_name";

/// Config for an ActorSpreadScaler
struct ActorSpreadConfig {
pub struct ActorSpreadConfig {
/// OCI, Bindle, or File reference for an actor
actor_reference: String,
/// Lattice ID that this SpreadScaler monitors
Expand All @@ -27,14 +28,16 @@ struct ActorSpreadConfig {
model_name: String,
/// Configuration for this SpreadScaler
spread_config: SpreadScalerProperty,
/// Actor ID, stored in a OnceCell to facilitate more efficient fetches
actor_id: OnceCell<String>,
}

/// The ActorSpreadScaler ensures that a certain number of replicas are running,
/// spread across a number of hosts according to a [SpreadScalerProperty](crate::model::SpreadScalerProperty)
///
/// If no [Spreads](crate::model::Spread) are specified, this Scaler simply maintains the number of replicas
/// on an available host
struct ActorSpreadScaler<S: ReadStore + Send + Sync> {
pub struct ActorSpreadScaler<S: ReadStore + Send + Sync> {
pub config: ActorSpreadConfig,
spread_requirements: Vec<(Spread, usize)>,
store: S,
Expand Down Expand Up @@ -168,7 +171,6 @@ impl<S: ReadStore + Send + Sync> Scaler for ActorSpreadScaler<S> {
}

impl<S: ReadStore + Send + Sync> ActorSpreadScaler<S> {
#[allow(unused)]
/// Construct a new ActorSpreadScaler with specified configuration values
pub fn new(
store: S,
Expand All @@ -182,38 +184,40 @@ impl<S: ReadStore + Send + Sync> ActorSpreadScaler<S> {
spread_requirements: compute_spread(&spread_config),
config: ActorSpreadConfig {
actor_reference,
actor_id: OnceCell::new(),
lattice_id,
spread_config,
model_name,
},
}
}

// TODO(brooksmtownsend): Need to consider a better way to grab this. It will fail to find
// the actor ID if it's not running in the lattice currently, which seems brittle
/// Helper function to retrieve the actor ID for the configured actor
async fn actor_id(&self) -> Result<String> {
Ok(self
.store
.list::<Actor>(&self.config.lattice_id)
.await?
.iter()
.find(|(_id, actor)| actor.reference == self.config.actor_reference)
.map(|(id, _actor)| id.to_owned())
// Default here means the below `get` will find zero running actors, which is fine because
// that accurately describes the current lattice having zero instances.
.unwrap_or_default())
.config
.actor_id
.get_or_init(|| async {
self.store
.list::<Actor>(&self.config.lattice_id)
.await
.unwrap_or_default()
.iter()
.find(|(_id, actor)| actor.reference == self.config.actor_reference)
.map(|(id, _actor)| id.to_owned())
// Default here means the below `get` will find zero running actors, which is fine because
// that accurately describes the current lattice having zero instances.
.unwrap_or_default()
})
.await
.to_string())
}

/// Helper function to create a predictable annotations map for a spread
fn annotations(&self, spread_name: &str) -> HashMap<String, String> {
HashMap::from_iter([
(SCALER_KEY.to_string(), SCALER_VALUE.to_string()),
(SPREAD_KEY.to_string(), spread_name.to_string()),
(
crate::MANAGED_BY_ANNOTATION.to_owned(),
crate::MANAGED_BY_IDENTIFIER.to_owned(),
),
])
}
}
Expand Down

0 comments on commit 7c980c3

Please sign in to comment.