Skip to content

Commit

Permalink
part of evaluator
Browse files Browse the repository at this point in the history
  • Loading branch information
leoromanovsky committed Nov 28, 2024
1 parent b1253ef commit b9ccbfe
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 39 deletions.
81 changes: 72 additions & 9 deletions eppo_core/src/eval/eval_precomputed_assignments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,41 @@ use std::sync::Arc;
use chrono::{DateTime, Utc};

use crate::eval::get_assignment;
use crate::ufc::{AssignmentFormat, Environment};
use crate::{error::EvaluationFailure, ufc::Assignment};
use crate::{Attributes, Configuration, Str};
use crate::{Attributes, Configuration, EvaluationError, Str};

#[derive(Debug)]
pub struct PrecomputedConfiguration {
pub created_at: DateTime<Utc>,
pub format: AssignmentFormat,
pub environment: Environment,
pub flags: HashMap<String, Result<Assignment, EvaluationFailure>>,
}

impl PrecomputedConfiguration {
pub fn empty(environment_name: &str) -> Self {
Self {
created_at: Utc::now(),
format: AssignmentFormat::Precomputed,
environment: Environment {
name: environment_name.into(),
},
flags: HashMap::new(),
}
}
}

pub fn get_precomputed_assignments(
configuration: Option<&Configuration>,
subject_key: &Str,
subject_attributes: &Arc<Attributes>,
early_exit: bool,
now: DateTime<Utc>,
) -> PrecomputedConfiguration {
let mut flags = HashMap::new();
) -> Result<PrecomputedConfiguration, EvaluationError> {
let result = if let Some(config) = configuration {
let mut flags = HashMap::new();

if let Some(config) = configuration {
for key in config.flags.compiled.flags.keys() {
match get_assignment(
Some(config),
Expand All @@ -43,17 +60,61 @@ pub fn get_precomputed_assignments(
}
}
}
}

PrecomputedConfiguration { flags }
Ok(PrecomputedConfiguration {
created_at: now,
format: AssignmentFormat::Precomputed,
environment: {
Environment {
name: config.flags.compiled.environment.name.clone(),
}
},
flags,
})
} else {
Err(EvaluationFailure::ConfigurationMissing)
};

match result {
Ok(config) => {
log::trace!(target: "eppo",
subject_key,
assignments:serde = config.flags;
"evaluated precomputed assignments");
Ok(config)
}
Err(EvaluationFailure::ConfigurationMissing) => {
log::warn!(target: "eppo",
subject_key;
"evaluating a flag before Eppo configuration has been fetched");
Ok(PrecomputedConfiguration::empty("unknown"))
}
Err(EvaluationFailure::Error(err)) => {
log::warn!(target: "eppo",
subject_key;
"error occurred while evaluating a flag: {err}",
);
Err(err)
}
// Non-Error failures are considered normal conditions and usually don't need extra
// attention, so we remap them to Ok(None) before returning to the user.
Err(err) => {
log::trace!(target: "eppo",
subject_key;
"returning default assignment because of: {err}");
Ok(PrecomputedConfiguration::empty("unknown"))
}
}
}

#[cfg(test)]
mod tests {
use chrono::Utc;

use crate::{
eval::get_precomputed_assignments,
eval::{
eval_precomputed_assignments::PrecomputedConfiguration, get_precomputed_assignments,
},
ufc::{UniversalFlagConfig, VariationType},
Attributes, Configuration, SdkMetadata,
};
Expand Down Expand Up @@ -89,7 +150,8 @@ mod tests {
&subject_attributes,
false,
now,
);
)
.unwrap_or_else(|_| PrecomputedConfiguration::empty("test"));

assert!(
!precomputed.flags.is_empty(),
Expand Down Expand Up @@ -131,7 +193,8 @@ mod tests {
&subject_attributes,
true,
now,
);
)
.unwrap_or_else(|_| PrecomputedConfiguration::empty("test"));

// Verify we have fewer entries due to early exit
assert!(
Expand Down
8 changes: 4 additions & 4 deletions eppo_core/src/eval/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use chrono::Utc;

use crate::{
configuration_store::ConfigurationStore,
error::EvaluationFailure,
eval::eval_precomputed_assignments::PrecomputedConfiguration,
events::AssignmentEvent,
ufc::{Assignment, AssignmentValue, VariationType},
Expand Down Expand Up @@ -118,11 +119,10 @@ impl Evaluator {
subject_key: &Str,
subject_attributes: &Arc<Attributes>,
early_exit: bool,
) -> PrecomputedConfiguration {
let config = self.get_configuration();

) -> Result<PrecomputedConfiguration, EvaluationError> {
let configuration = self.get_configuration();
get_precomputed_assignments(
config.as_ref().map(AsRef::as_ref),
configuration.as_ref().map(AsRef::as_ref),
&subject_key,
&subject_attributes,
early_exit,
Expand Down
2 changes: 1 addition & 1 deletion eppo_core/src/eval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ pub mod eval_details;

pub use eval_assignment::{get_assignment, get_assignment_details};
pub use eval_bandits::{get_bandit_action, get_bandit_action_details, BanditResult};
pub use eval_precomputed_assignments::get_precomputed_assignments;
pub use eval_precomputed_assignments::{get_precomputed_assignments, PrecomputedConfiguration};
pub use evaluator::{Evaluator, EvaluatorConfig};
4 changes: 2 additions & 2 deletions eppo_core/src/ufc/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ pub(crate) struct UniversalFlagConfigWire {

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "UPPERCASE")]
pub(crate) enum AssignmentFormat {
pub enum AssignmentFormat {
Client,
Precomputed,
Server,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct Environment {
pub struct Environment {
/// Name of the environment.
pub name: Str,
}
Expand Down
67 changes: 44 additions & 23 deletions fastly-edge-assignments/src/handlers/handler_assignments.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use eppo_core::configuration_store::ConfigurationStore;
use eppo_core::eval::{Evaluator, EvaluatorConfig};
use eppo_core::ufc::{Assignment, UniversalFlagConfig, VariationType};
use eppo_core::eval::{Evaluator, EvaluatorConfig, PrecomputedConfiguration};
use eppo_core::ufc::{
Assignment, AssignmentFormat, Environment, UniversalFlagConfig, VariationType,
};
use eppo_core::{Attributes, Configuration, SdkMetadata, Str};
use fastly::http::StatusCode;
use fastly::kv_store::KVStoreError;
Expand Down Expand Up @@ -76,11 +78,34 @@ impl FlagAssignment {
}
}

#[derive(Debug)]
#[derive(Debug, Serialize)]
struct PrecomputedAssignmentsResponse {
created_at: chrono::DateTime<chrono::Utc>,
format: AssignmentFormat,
environment: Environment,
flags: HashMap<String, FlagAssignment>,
}

impl PrecomputedAssignmentsResponse {
fn from_precomputed_assignments(assignments: PrecomputedConfiguration) -> Self {
Self {
created_at: assignments.created_at,
format: assignments.format,
environment: assignments.environment,
flags: assignments
.flags
.into_iter()
.map(|(k, v)| {
v.ok()
.and_then(|assignment| FlagAssignment::try_from_assignment(assignment))
.map(|flag_assignment| (k, flag_assignment))
})
.flatten()
.collect(),
}
}
}

pub fn handle_assignments(mut req: Request) -> Result<Response, Error> {
// Extract the SDK key and generate a token hash matching the pre-defined encoding.
let Some(token) = req
Expand Down Expand Up @@ -169,30 +194,26 @@ pub fn handle_assignments(mut req: Request) -> Result<Response, Error> {
let configuration_store = ConfigurationStore::new();
configuration_store.set_configuration(configuration.clone());

// Create the response
let assignments_response = PrecomputedAssignmentsResponse {
flags: Evaluator::new(EvaluatorConfig {
configuration_store: Arc::new(configuration_store),
sdk_metadata: SdkMetadata {
name: SDK_NAME,
version: SDK_VERSION,
},
})
.get_precomputed_assignments(&subject_key, &subject_attributes, false)
.flags
.into_iter()
.map(|(k, v)| {
v.ok()
.and_then(|assignment| FlagAssignment::try_from_assignment(assignment))
.map(|flag_assignment| (k, flag_assignment))
})
.flatten()
.collect(),
let evaluator = Evaluator::new(EvaluatorConfig {
configuration_store: Arc::new(configuration_store),
sdk_metadata: SdkMetadata {
name: SDK_NAME,
version: SDK_VERSION,
},
});

let precomputed_assignments = if let Ok(assignments) =
evaluator.get_precomputed_assignments(&subject_key, &subject_attributes, false)
{
PrecomputedAssignmentsResponse::from_precomputed_assignments(assignments)
} else {
return Ok(Response::from_status(StatusCode::INTERNAL_SERVER_ERROR)
.with_body_text_plain("Failed to get precomputed assignments"));
};

// Create an HTTP OK response with the assignments
let response =
match Response::from_status(StatusCode::OK).with_body_json(&assignments_response.flags) {
match Response::from_status(StatusCode::OK).with_body_json(&precomputed_assignments) {
Ok(response) => response,
Err(e) => {
eprintln!("Failed to serialize response: {:?}", e);
Expand Down

0 comments on commit b9ccbfe

Please sign in to comment.