Skip to content
Merged
Show file tree
Hide file tree
Changes from 32 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)
1 change: 1 addition & 0 deletions eppo_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub mod configuration_store;
pub mod eval;
pub mod events;
pub mod poller_thread;
pub mod precomputed_assignments;
#[cfg(feature = "pyo3")]
pub mod pyo3;
pub mod sharder;
Expand Down
86 changes: 86 additions & 0 deletions eppo_core/src/precomputed_assignments.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use crate::ufc::{Assignment, AssignmentFormat, Environment, VariationType};
use crate::{Attributes, Configuration, Str};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;

// Request
#[derive(Debug, Deserialize)]
pub struct PrecomputedAssignmentsServiceRequestBody {
Comment on lines +7 to +9
Copy link
Collaborator

Choose a reason for hiding this comment

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

major: eppo_core shall be unconcerned with network request format

pub subject_key: String,
pub subject_attributes: Arc<Attributes>,
// TODO: Add bandit actions
// #[serde(rename = "banditActions")]
// #[serde(skip_serializing_if = "Option::is_none")]
// bandit_actions: Option<HashMap<String, serde_json::Value>>,
}

// Response
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FlagAssignment {
pub allocation_key: Str,
pub variation_key: Str,
pub variation_type: VariationType,
pub variation_value: serde_json::Value,
/// Additional user-defined logging fields for capturing extra information related to the
/// assignment.
#[serde(flatten)]
pub extra_logging: HashMap<String, String>,
pub do_log: bool,
}

impl FlagAssignment {
pub fn try_from_assignment(assignment: Assignment) -> Option<Self> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

minor: prefer implementing From and TryFrom traits

// WARNING! There is a problem here. The event is only populated for splits
Copy link
Member Author

Choose a reason for hiding this comment

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

@rasendubi @sameerank I believe this is a problem as described here: https://github.com/Eppo-exp/eppo-multiplatform/blob/main/eppo_core/src/ufc/compiled_flag_config.rs#L231

During compilation, an optimization step to reduce the configuration, event is only populated for splits with do_log == True.

A possible solution is to produce a configuration that is "not optimized" for use in this function so that we can access do_log

Copy link
Collaborator

Choose a reason for hiding this comment

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

Another option is to make fields from event optional. There's no reason to include keys and extra logging if doLog is false.

Copy link
Member Author

Choose a reason for hiding this comment

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

I still think its a problem because we want the precomputed server to return evaluations for all flags and do_log is true or false depending on if the user wants them log, but they still need to be present. The flag will be present in the response which changes the behavior of the SDK / application instead of now the flag will not be present and the default value will be used. Am I understanding that correctly?

// that have `do_log` set to true in the wire format. This means that
// all the ones present here are logged, but any splits that are not
// logged are not present here.
//
// This is a problem for us because we want to be able to return
// precomputed assignments for any split, logged or not, since we
// want to be able to return them for all flags.
//
// We need to fix this.
assignment.event.as_ref().map(|event| Self {
allocation_key: event.base.allocation.clone(),
variation_key: event.base.variation.clone(),
variation_type: assignment.value.variation_type(),
variation_value: assignment.value.variation_value(),
extra_logging: event
.base
.extra_logging
.iter()
.map(|(k, v)| (k.clone(), v.clone()))
.collect(),
Comment on lines +50 to +55
Copy link
Collaborator

Choose a reason for hiding this comment

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

same as:

extra_logging: event.base.extra_logging.clone(),

do_log: true,
})
}
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PrecomputedAssignmentsServiceResponse {
created_at: chrono::DateTime<chrono::Utc>,
format: AssignmentFormat,
environment: Environment,
flags: HashMap<String, FlagAssignment>,
}

impl PrecomputedAssignmentsServiceResponse {
pub fn from_configuration(
configuration: Arc<Configuration>,
flags: HashMap<String, FlagAssignment>,
) -> Self {
Self {
created_at: chrono::Utc::now(),
format: AssignmentFormat::Precomputed,
environment: {
Environment {
name: configuration.flags.compiled.environment.name.clone(),
}
},
flags,
}
}
}
48 changes: 48 additions & 0 deletions eppo_core/src/ufc/assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use serde::{Deserialize, Serialize};

use crate::{events::AssignmentEvent, Str};

use crate::ufc::VariationType;

/// Result of assignment evaluation.
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -231,6 +233,52 @@ impl AssignmentValue {
_ => None,
}
}

/// Returns the type of the variation as a string.
///
/// # Returns
/// - A string representing the type of the variation ("STRING", "INTEGER", "NUMERIC", "BOOLEAN", or "JSON").
///
/// # Examples
/// ```
/// # use eppo_core::ufc::AssignmentValue;
/// # use eppo_core::ufc::VariationType;
/// let value = AssignmentValue::String("example".into());
/// assert_eq!(value.variation_type(), VariationType::String);
/// ```
pub fn variation_type(&self) -> VariationType {
match self {
AssignmentValue::String(_) => VariationType::String,
AssignmentValue::Integer(_) => VariationType::Integer,
AssignmentValue::Numeric(_) => VariationType::Numeric,
AssignmentValue::Boolean(_) => VariationType::Boolean,
AssignmentValue::Json(_) => VariationType::Json,
}
}

/// Returns the raw value of the variation.
///
/// # Returns
/// - A JSON Value containing the variation value.
///
/// # Examples
/// ```
/// # use eppo_core::ufc::AssignmentValue;
/// # use serde_json::json;
/// let value = AssignmentValue::String("example".into());
/// assert_eq!(value.variation_value(), json!("example"));
/// ```
pub fn variation_value(&self) -> serde_json::Value {
match self {
AssignmentValue::String(s) => serde_json::Value::String(s.to_string()),
AssignmentValue::Integer(i) => serde_json::Value::Number((*i).into()),
AssignmentValue::Numeric(n) => serde_json::Value::Number(
serde_json::Number::from_f64(*n).unwrap_or_else(|| serde_json::Number::from(0)),
),
AssignmentValue::Boolean(b) => serde_json::Value::Bool(*b),
AssignmentValue::Json(j) => j.as_ref().clone(),
}
}
}

#[cfg(feature = "pyo3")]
Expand Down
9 changes: 9 additions & 0 deletions eppo_core/src/ufc/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub type Timestamp = chrono::DateTime<chrono::Utc>;
pub(crate) struct UniversalFlagConfigWire {
/// When configuration was last updated.
pub created_at: Timestamp,
pub format: AssignmentFormat,
Copy link
Member Author

Choose a reason for hiding this comment

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

heads up @sameerank I added a new key to UFC parsing. I see it is being returned now always so I feel it is safe to make it required.

Copy link
Collaborator

Choose a reason for hiding this comment

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

minor: ideally, this field should be optional because there could exist configurations without it (remember that users may be storing and passing configurations out of band now)

Copy link
Member Author

Choose a reason for hiding this comment

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

#76

/// Environment this configuration belongs to.
pub environment: Environment,
/// Flags configuration.
Expand All @@ -31,6 +32,14 @@ pub(crate) struct UniversalFlagConfigWire {
pub bandits: HashMap<String, Vec<BanditVariationWire>>,
}

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

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct Environment {
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/
16 changes: 16 additions & 0 deletions fastly-edge-assignments/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[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]
base64-url = "2.0.0"
chrono = "0.4.19"
eppo_core = { version = "=4.1.1", path = "../eppo_core" }
fastly = "0.11.0"
serde_json = "1.0.132"
serde = "1.0.192"
sha2 = "0.10.0"
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
17 changes: 17 additions & 0 deletions fastly-edge-assignments/fastly.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# This file describes a Fastly Compute package. To learn more visit:
# https://www.fastly.com/documentation/reference/compute/fastly-toml

authors = ["[email protected]"]
name = "Eppo Assignments on Fastly Compute@Edge"
description = "Edge compute service for pre-computed Eppo flag assignments"
language = "rust"
manifest_version = 3

[scripts]
build = "cargo build --bin fastly-edge-assignments --release --target wasm32-wasi --color always"

[local_server]
[local_server.kv_stores]
[[local_server.kv_stores.edge-assignment-kv-store]]
key = "ufc-by-sdk-key-token-hash-V--77TScV5Etm78nIMTSOdiroOh1__NsupwUwsetEVM"
file = "../sdk-test-data/ufc/flags-v1.json"
Copy link
Member Author

Choose a reason for hiding this comment

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

this instructs the fastly serve process to populate the local in-memory KV store with one key, a UFC configuration at a precomputed hash. See the unit test in this repo for the associate SDK key to use.

3 changes: 3 additions & 0 deletions fastly-edge-assignments/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[toolchain]
channel = "stable"
targets = [ "wasm32-wasi" ]
Loading
Loading