Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
a813273
feat: fastly compute service to perform assignments (FF-3552)
leoromanovsky Nov 16, 2024
e985863
convert from reqwest blocking to non-blocking to conform to wasm target
leoromanovsky Nov 5, 2024
c8717ee
remove vscode settings
leoromanovsky Nov 16, 2024
5956e65
makefile for local development
leoromanovsky Nov 16, 2024
a3f37c7
split into multiple handlers
leoromanovsky Nov 17, 2024
48d47aa
its a thing of beauty
leoromanovsky Nov 17, 2024
56e5c96
coming along nicely
leoromanovsky Nov 17, 2024
3a4b644
good
leoromanovsky Nov 17, 2024
59cb251
local test kv-store!
leoromanovsky Nov 17, 2024
a159065
feat: add `get_subject_assignments` method to eppo_core to compute al…
leoromanovsky Nov 17, 2024
6e704ef
simplify unnecessary ownership transfer
leoromanovsky Nov 17, 2024
bccce04
e2e evaluation
leoromanovsky Nov 17, 2024
c338d31
remove space end of filename
leoromanovsky Nov 18, 2024
ff73c47
Revert "feat: add `get_subject_assignments` method to eppo_core to co…
leoromanovsky Nov 18, 2024
48d2ebc
tidy up error logging
leoromanovsky Nov 18, 2024
3b55f6d
Merge branch 'main' into lr/ff-3552/edge-function
leoromanovsky Nov 20, 2024
3219a03
edge function dep on 4.1.1
leoromanovsky Nov 20, 2024
d288696
generate token hash
leoromanovsky Nov 20, 2024
be2d5ac
fastly local
leoromanovsky Nov 20, 2024
9f99865
conform to desired API
leoromanovsky Nov 21, 2024
78fe3b5
add CORS support
leoromanovsky Nov 22, 2024
f156caa
Merge branch 'main' into lr/ff-3552/edge-function
leoromanovsky Nov 22, 2024
f2f27af
version from cargo
leoromanovsky Nov 23, 2024
f1bcf3d
use Datetime
leoromanovsky Nov 23, 2024
4ada237
String
leoromanovsky Nov 23, 2024
8101988
no copy
leoromanovsky Nov 23, 2024
50ed0ec
use VariationType
leoromanovsky Nov 23, 2024
38f71eb
move structs to core
leoromanovsky Nov 23, 2024
266de8a
extra logging
leoromanovsky Nov 23, 2024
c3c90f8
refactor FlagAssignment to model
leoromanovsky Nov 23, 2024
b9ca60a
use Str
leoromanovsky Nov 23, 2024
26300db
build responses in core
leoromanovsky Nov 23, 2024
26c4466
tidy
leoromanovsky Nov 23, 2024
7b4eb91
add format
leoromanovsky Nov 23, 2024
95219d4
str
leoromanovsky Nov 23, 2024
81d1f55
format
leoromanovsky Nov 23, 2024
9622a21
one more format
leoromanovsky Nov 23, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ jobs:
submodules: true
- run: npm ci
- run: rustup update ${{ matrix.toolchain }} && rustup default ${{ matrix.toolchain }}
- run: cargo build --verbose --all-targets
- run: cargo test --verbose
# Add WASM target
- run: rustup target add wasm32-wasi
# Build non-WASM targets
- run: cargo build --verbose --all-targets --workspace --exclude fastly-edge-assignments
# Build WASM target separately
- run: cargo build --verbose -p fastly-edge-assignments --target wasm32-wasi
# Run tests (excluding WASM package)
- run: cargo test --verbose --workspace --exclude fastly-edge-assignments
- run: cargo doc --verbose
9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
[workspace]
resolver = "2"
members = [
"eppo_core",
"rust-sdk",
"python-sdk",
"ruby-sdk/ext/eppo_client",
"eppo_core",
"rust-sdk",
"python-sdk",
"ruby-sdk/ext/eppo_client",
"fastly-edge-assignments",
]

[patch.crates-io]
Expand Down
49 changes: 34 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,28 +1,47 @@
# Make settings - @see https://tech.davis-hansson.com/p/make/
SHELL := bash
.ONESHELL:
.SHELLFLAGS := -eu -o pipefail -c
.DELETE_ON_ERROR:
MAKEFLAGS += --warn-undefined-variables
MAKEFLAGS += --no-builtin-rules

# Log levels
DEBUG := $(shell printf "\e[2D\e[35m")
INFO := $(shell printf "\e[2D\e[36m🔵 ")
OK := $(shell printf "\e[2D\e[32m🟢 ")
WARN := $(shell printf "\e[2D\e[33m🟡 ")
ERROR := $(shell printf "\e[2D\e[31m🔴 ")
END := $(shell printf "\e[0m")
WASM_TARGET=wasm32-wasi
FASTLY_PACKAGE=fastly-edge-assignments
BUILD_DIR=target/$(WASM_TARGET)/release
WASM_FILE=$(BUILD_DIR)/$(FASTLY_PACKAGE).wasm

.PHONY: default
default: help

## help - Print help message.
# Help target for easy documentation
.PHONY: help
help: Makefile
@echo "usage: make <target>"
@sed -n 's/^##//p' $<
help:
@echo "Available targets:"
@echo " all - Default target (build workspace)"
@echo " workspace-build - Build the entire workspace excluding the Fastly package"
@echo " workspace-test - Test the entire workspace excluding the Fastly package"
@echo " fastly-edge-assignments-build - Build only the Fastly package for WASM"
@echo " fastly-edge-assignments-test - Test only the Fastly package"
@echo " clean - Clean all build artifacts"

.PHONY: test
test: ${testDataDir}
npm test

# Build the entire workspace excluding the `fastly-edge-assignments` package
.PHONY: workspace-build
workspace-build:
cargo build --workspace --exclude $(FASTLY_PACKAGE)

# Run tests for the entire workspace excluding the `fastly-edge-assignments` package
.PHONY: workspace-test
workspace-test:
cargo test --workspace --exclude $(FASTLY_PACKAGE)

# Build only the `fastly-edge-assignments` package for WASM
.PHONY: fastly-edge-assignments-build
fastly-edge-assignments-build:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these changes helpful? I got tired of putting the various parameters in the right place.

rustup target add $(WASM_TARGET)
cargo build --release --target $(WASM_TARGET) --package $(FASTLY_PACKAGE)

# Test only the `fastly-edge-assignments` package
.PHONY: fastly-edge-assignments-test
fastly-edge-assignments-test:
cargo test --target $(WASM_TARGET) --package $(FASTLY_PACKAGE)
3 changes: 2 additions & 1 deletion eppo_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ log = { version = "0.4.21", features = ["kv", "kv_serde"] }
md5 = "0.7.0"
rand = "0.8.5"
regex = "1.10.4"
reqwest = { version = "0.12.4", features = ["blocking", "json"] }
reqwest = { version = "0.12.4", features = ["json"] }
semver = { version = "1.0.22", features = ["serde"] }
serde = { version = "1.0.198", features = ["derive", "rc"] }
serde_json = "1.0.116"
thiserror = "1.0.60"
tokio = { version = "1.34.0", features = ["rt", "time"] }
url = "2.5.0"

# pyo3 dependencies
Expand Down
26 changes: 14 additions & 12 deletions eppo_core/src/configuration_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const BANDIT_ENDPOINT: &'static str = "/flag-config/v1/bandits";
/// A client that fetches Eppo configuration from the server.
pub struct ConfigurationFetcher {
// Client holds a connection pool internally, so we're reusing the client between requests.
client: reqwest::blocking::Client,
client: reqwest::Client,
config: ConfigurationFetcherConfig,
/// If we receive a 401 Unauthorized error during a request, it means the API key is not
/// valid. We cache this error so we don't issue additional requests to the server.
Expand All @@ -29,7 +29,7 @@ pub struct ConfigurationFetcher {

impl ConfigurationFetcher {
pub fn new(config: ConfigurationFetcherConfig) -> ConfigurationFetcher {
let client = reqwest::blocking::Client::new();
let client = reqwest::Client::new();

ConfigurationFetcher {
client,
Expand All @@ -38,24 +38,24 @@ impl ConfigurationFetcher {
}
}

pub fn fetch_configuration(&mut self) -> Result<Configuration> {
pub async fn fetch_configuration(&mut self) -> Result<Configuration> {
if self.unauthorized {
return Err(Error::Unauthorized);
}

let ufc = self.fetch_ufc_configuration()?;
let ufc = self.fetch_ufc_configuration().await?;

let bandits = if ufc.compiled.flag_to_bandit_associations.is_empty() {
// We don't need bandits configuration if there are no bandits.
None
} else {
Some(self.fetch_bandits_configuration()?)
Some(self.fetch_bandits_configuration().await?)
};

Ok(Configuration::from_server_response(ufc, bandits))
}

fn fetch_ufc_configuration(&mut self) -> Result<UniversalFlagConfig> {
async fn fetch_ufc_configuration(&mut self) -> Result<UniversalFlagConfig> {
let url = Url::parse_with_params(
&format!("{}{}", self.config.base_url, UFC_ENDPOINT),
&[
Expand All @@ -68,7 +68,7 @@ impl ConfigurationFetcher {
.map_err(|err| Error::InvalidBaseUrl(err))?;

log::debug!(target: "eppo", "fetching UFC flags configuration");
let response = self.client.get(url).send()?;
let response = self.client.get(url).send().await?;

let response = response.error_for_status().map_err(|err| {
if err.status() == Some(StatusCode::UNAUTHORIZED) {
Expand All @@ -82,15 +82,17 @@ impl ConfigurationFetcher {
}
})?;

let configuration =
UniversalFlagConfig::from_json(self.config.sdk_metadata, response.bytes()?.into())?;
let configuration = UniversalFlagConfig::from_json(
self.config.sdk_metadata,
response.bytes().await?.into(),
)?;

log::debug!(target: "eppo", "successfully fetched UFC flags configuration");

Ok(configuration)
}

fn fetch_bandits_configuration(&mut self) -> Result<BanditResponse> {
async fn fetch_bandits_configuration(&mut self) -> Result<BanditResponse> {
let url = Url::parse_with_params(
&format!("{}{}", self.config.base_url, BANDIT_ENDPOINT),
&[
Expand All @@ -103,7 +105,7 @@ impl ConfigurationFetcher {
.map_err(|err| Error::InvalidBaseUrl(err))?;

log::debug!(target: "eppo", "fetching UFC bandits configuration");
let response = self.client.get(url).send()?;
let response = self.client.get(url).send().await?;

let response = response.error_for_status().map_err(|err| {
if err.status() == Some(StatusCode::UNAUTHORIZED) {
Expand All @@ -117,7 +119,7 @@ impl ConfigurationFetcher {
}
})?;

let configuration = response.json()?;
let configuration = response.json().await?;

log::debug!(target: "eppo", "successfully fetched UFC bandits configuration");

Expand Down
78 changes: 74 additions & 4 deletions eppo_core/src/eval/eval_assignment.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::{collections::HashMap, sync::Arc};

use chrono::{DateTime, Utc};

Expand Down Expand Up @@ -91,6 +91,56 @@ pub fn get_assignment_details(
(result_with_details, event)
}

pub fn get_subject_assignments(
configuration: Option<&Configuration>,
subject_key: &Str,
subject_attributes: &Arc<Attributes>,
now: DateTime<Utc>,
) -> HashMap<String, Result<Option<Assignment>, EvaluationError>> {
get_assignments_with_visitor(
configuration,
&mut NoopEvalVisitor,
subject_key,
subject_attributes,
now,
)
}

/**
* Evaluate all flags for the given subject and return a map of flag keys to assignments.
*
* If an error occurs while evaluating a flag,
* it is returned for the resulting flag key.
*/
pub(super) fn get_assignments_with_visitor<V: EvalAssignmentVisitor>(
configuration: Option<&Configuration>,
visitor: &mut V,
subject_key: &Str,
subject_attributes: &Arc<Attributes>,
now: DateTime<Utc>,
) -> HashMap<String, Result<Option<Assignment>, EvaluationError>> {
configuration.map_or_else(HashMap::new, |config| {
config
.flags
.compiled
.flags
.keys()
.map(|flag_key| {
let result = get_assignment_with_visitor(
Some(&config),
visitor,
flag_key,
subject_key,
subject_attributes,
None,
now,
);
(flag_key.clone(), result)
})
.collect()
})
}

// Exposed for use in bandit evaluation.
pub(super) fn get_assignment_with_visitor<V: EvalAssignmentVisitor>(
configuration: Option<&Configuration>,
Expand Down Expand Up @@ -317,7 +367,7 @@ mod tests {
eval_details::{
AllocationEvaluationCode, AllocationEvaluationDetails, FlagEvaluationCode,
},
get_assignment, get_assignment_details,
get_assignment, get_assignment_details, get_subject_assignments,
},
ufc::{RuleWire, UniversalFlagConfig, ValueWire, VariationType},
Attributes, Configuration, SdkMetadata, Str,
Expand Down Expand Up @@ -451,6 +501,8 @@ mod tests {

for subject in test_file.subjects {
print!("test subject {:?} ... ", subject.subject_key);

// Verify that get_assignment returns the desired result.
let result = get_assignment(
Some(&config),
&test_file.flag,
Expand All @@ -461,15 +513,33 @@ mod tests {
)
.unwrap_or(None);

let result_assingment = result
let result_assignment = result
.as_ref()
.map(|assignment| &assignment.value)
.unwrap_or(&default_assignment);
let expected_assignment = to_value(subject.assignment)
.into_assignment_value(test_file.variation_type)
.unwrap();

assert_eq!(result_assingment, &expected_assignment);
assert_eq!(result_assignment, &expected_assignment);

// Verify that get_subject_assignments returns the same result.
let subject_assignments = get_subject_assignments(
Some(&config),
&subject.subject_key,
&subject.subject_attributes,
now,
);
let subject_assignment_for_flag = subject_assignments
.get(&test_file.flag)
.and_then(|result| result.as_ref().ok())
.and_then(|opt| opt.as_ref())
.map(|a| &a.value)
.unwrap_or(&default_assignment);

// Compare against the original expected assignment instead of result_assignment
assert_eq!(subject_assignment_for_flag, &expected_assignment);

println!("ok");
}
}
Expand Down
16 changes: 15 additions & 1 deletion eppo_core/src/eval/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
use super::{
eval_details::{EvaluationDetails, EvaluationResultWithDetails},
get_assignment, get_assignment_details, get_bandit_action, get_bandit_action_details,
BanditResult,
get_subject_assignments, BanditResult,
};

pub struct EvaluatorConfig {
Expand Down Expand Up @@ -49,6 +49,20 @@ impl Evaluator {
)
}

pub fn get_subject_assignments(
&self,
subject_key: &Str,
subject_attributes: &Arc<Attributes>,
) -> HashMap<String, Result<Option<Assignment>, EvaluationError>> {
let config = self.get_configuration();
get_subject_assignments(
config.as_ref().map(AsRef::as_ref),
subject_key,
subject_attributes,
Utc::now(),
)
}

pub fn get_assignment_details(
&self,
flag_key: &str,
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 @@ -7,6 +7,6 @@ mod evaluator;

pub mod eval_details;

pub use eval_assignment::{get_assignment, get_assignment_details};
pub use eval_assignment::{get_assignment, get_assignment_details, get_subject_assignments};
pub use eval_bandits::{get_bandit_action, get_bandit_action_details, BanditResult};
pub use evaluator::{Evaluator, EvaluatorConfig};
6 changes: 5 additions & 1 deletion eppo_core/src/poller_thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,13 @@ impl PollerThread {
std::thread::Builder::new()
.name("eppo-poller".to_owned())
.spawn(move || {
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
loop {
log::debug!(target: "eppo", "fetching new configuration");
let result = fetcher.fetch_configuration();
let result = runtime.block_on(fetcher.fetch_configuration());
match result {
Ok(configuration) => {
store.set_configuration(Arc::new(configuration));
Expand Down
2 changes: 2 additions & 0 deletions fastly-edge-assignments/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build]
target = "wasm32-wasi"
2 changes: 2 additions & 0 deletions fastly-edge-assignments/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pkg/
bin/
14 changes: 14 additions & 0 deletions fastly-edge-assignments/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "fastly-edge-assignments"
version = "0.1.0"
edition = "2021"
# Remove this line if you want to be able to publish this crate as open source on crates.io.
# Otherwise, `publish = false` prevents an accidental `cargo publish` from revealing private source.
publish = false

[dependencies]
chrono = "0.4.19"
eppo_core = { version = "=4.1.0", path = "../eppo_core" }
fastly = "0.11.0"
serde_json = "1.0.132"
serde = "1.0.192"
3 changes: 3 additions & 0 deletions fastly-edge-assignments/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Eppo Assignments on Fastly Compute@Edge

TODO: Add a description
Loading
Loading