Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Final pass addressing any outstanding comments #87

Merged
merged 6 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
32 changes: 13 additions & 19 deletions contract-tests/src/client_entity.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use futures::future::FutureExt;
use launchdarkly_server_sdk::{
Context, ContextBuilder, ExecutionOrder, MigratorBuilder, MultiContextBuilder, Reference,
Context, ContextBuilder, MigratorBuilder, MultiContextBuilder, Reference,
};
use std::sync::Arc;
use std::time::Duration;
Expand Down Expand Up @@ -242,22 +242,13 @@ impl ClientEntity {

let mut builder = MigratorBuilder::new(self.client.clone());

let execution_order = match params.read_execution_order.as_str() {
"serial" => ExecutionOrder::Serial,
"random" => ExecutionOrder::Random,
_ => ExecutionOrder::Parallel,
};
let old_endpoint = params.old_endpoint.clone();

let new_endpoint = params.new_endpoint.clone();

builder = builder
.read_execution_order(execution_order)
.read_execution_order(params.read_execution_order)
.track_errors(params.track_errors)
.track_latency(params.track_latency)
.read(
|payload: &Option<String>| {
let old_endpoint = old_endpoint.clone();
let old_endpoint = params.old_endpoint.clone();
async move {
let result = send_payload(&old_endpoint, payload.clone()).await;
match result {
Expand All @@ -268,7 +259,7 @@ impl ClientEntity {
.boxed()
},
|payload| {
let new_endpoint = new_endpoint.clone();
let new_endpoint = params.new_endpoint.clone();
async move {
let result = send_payload(&new_endpoint, payload.clone()).await;
match result {
Expand All @@ -286,7 +277,7 @@ impl ClientEntity {
)
.write(
|payload| {
let old_endpoint = old_endpoint.clone();
let old_endpoint = params.old_endpoint.clone();
async move {
let result = send_payload(&old_endpoint, payload.clone()).await;
match result {
Expand All @@ -297,7 +288,7 @@ impl ClientEntity {
.boxed()
},
|payload| {
let new_endpoint = new_endpoint.clone();
let new_endpoint = params.new_endpoint.clone();
async move {
let result = send_payload(&new_endpoint, payload.clone()).await;
match result {
Expand All @@ -309,13 +300,13 @@ impl ClientEntity {
},
);

let migrator = builder.build().expect("builder failed");
let mut migrator = builder.build().expect("builder failed");
match params.operation {
launchdarkly_server_sdk::Operation::Read => {
let result = migrator
.read(
&params.context,
Copy link
Member Author

Choose a reason for hiding this comment

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

We don't need to consume the context, and the parameter order change makes this consistent with the variation methods.

params.key,
params.context,
params.default_stage,
params.payload,
)
Expand All @@ -333,8 +324,8 @@ impl ClientEntity {
launchdarkly_server_sdk::Operation::Write => {
let result = migrator
.write(
&params.context,
params.key,
params.context,
params.default_stage,
params.payload,
)
Expand All @@ -349,7 +340,10 @@ impl ClientEntity {
MigrationOperationResponse { result: payload },
)))
}
_ => todo!(),
_ => Err(format!(
"Invalid operation requested: {:?}",
params.operation
)),
}
}
command => Err(format!("Invalid command requested: {}", command)),
Expand Down
4 changes: 2 additions & 2 deletions contract-tests/src/command_params.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use launchdarkly_server_sdk::{
AttributeValue, Context, FlagDetail, FlagValue, Operation, Reason, Stage,
AttributeValue, Context, ExecutionOrder, FlagDetail, FlagValue, Operation, Reason, Stage,
};
use serde::{self, Deserialize, Serialize};
use std::collections::HashMap;
Expand Down Expand Up @@ -157,7 +157,7 @@ pub struct MigrationOperationParams {
pub key: String,
pub context: Context,
pub default_stage: Stage,
pub read_execution_order: String,
pub read_execution_order: ExecutionOrder,
pub operation: Operation,
pub old_endpoint: String,
pub new_endpoint: String,
Expand Down
37 changes: 20 additions & 17 deletions launchdarkly-server-sdk/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ impl Client {
context: &Context,
flag_key: &str,
default_stage: Stage,
) -> (Stage, MigrationOpTracker) {
) -> (Stage, Arc<Mutex<MigrationOpTracker>>) {
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 mentioned this on the docs PR, but should we change this to return the Arc<Mutex<>>? Both the kit and the blocks would require doing this at some point anyway so it's kind of a convenience in a sense.

You will also see in this PR I removed the internal mutex in the tracker as well. No need to pay for two lock penalties.

let (detail, flag) =
self.variation_internal(context, flag_key, default_stage, &self.events_default);

Expand All @@ -655,7 +655,10 @@ impl Client {
default_stage,
);

(migration_detail.value.unwrap_or(default_stage), tracker)
(
migration_detail.value.unwrap_or(default_stage),
Arc::new(Mutex::new(tracker)),
)
}

/// Reports that a context has performed an event.
Expand Down Expand Up @@ -1741,7 +1744,7 @@ mod tests {
)
.expect("patch should apply");

let migrator = MigratorBuilder::new(client.clone())
let mut migrator = MigratorBuilder::new(client.clone())
.read(
|_| async move { Ok(serde_json::Value::Null) }.boxed(),
|_| async move { Ok(serde_json::Value::Null) }.boxed(),
Expand All @@ -1761,17 +1764,17 @@ mod tests {
if let Operation::Read = operation {
migrator
.read(
&context,
"stage-flag".into(),
context,
Stage::Off,
serde_json::Value::Null,
)
.await;
} else {
migrator
.write(
&context,
"stage-flag".into(),
context,
Stage::Off,
serde_json::Value::Null,
)
Expand Down Expand Up @@ -1855,7 +1858,7 @@ mod tests {
)
.expect("patch should apply");

let migrator = MigratorBuilder::new(client.clone())
let mut migrator = MigratorBuilder::new(client.clone())
.track_latency(true)
.read(
|_| {
Expand Down Expand Up @@ -1900,17 +1903,17 @@ mod tests {
if let Operation::Read = operation {
migrator
.read(
&context,
"stage-flag".into(),
context,
Stage::Off,
serde_json::Value::Null,
)
.await;
} else {
migrator
.write(
&context,
"stage-flag".into(),
context,
Stage::Off,
serde_json::Value::Null,
)
Expand Down Expand Up @@ -1957,12 +1960,12 @@ mod tests {
)
.expect("patch should apply");

let migrator = MigratorBuilder::new(client.clone())
let mut migrator = MigratorBuilder::new(client.clone())
.track_latency(true)
.read(
|_| async move { Err("fail".into()) }.boxed(),
|_| async move { Err("fail".into()) }.boxed(),
Some(|_, _| true),
Some(|_: &String, _: &String| true),
)
.write(
|_| async move { Err("fail".into()) }.boxed(),
Expand All @@ -1977,8 +1980,8 @@ mod tests {

migrator
.read(
&context,
"stage-flag".into(),
context,
Stage::Off,
serde_json::Value::Null,
)
Expand Down Expand Up @@ -2026,7 +2029,7 @@ mod tests {
)
.expect("patch should apply");

let migrator = MigratorBuilder::new(client.clone())
let mut migrator = MigratorBuilder::new(client.clone())
.track_latency(true)
.read(
|_| async move { Ok(serde_json::Value::Null) }.boxed(),
Expand All @@ -2046,8 +2049,8 @@ mod tests {

migrator
.write(
&context,
"stage-flag".into(),
context,
Stage::Off,
serde_json::Value::Null,
)
Expand Down Expand Up @@ -2117,7 +2120,7 @@ mod tests {
)
.expect("patch should apply");

let migrator = MigratorBuilder::new(client.clone())
let mut migrator = MigratorBuilder::new(client.clone())
.track_latency(true)
.read(
|_| async move { Ok(serde_json::Value::Null) }.boxed(),
Expand Down Expand Up @@ -2155,8 +2158,8 @@ mod tests {

migrator
.write(
&context,
"stage-flag".into(),
context,
Stage::Off,
serde_json::Value::Null,
)
Expand Down Expand Up @@ -2202,7 +2205,7 @@ mod tests {
)
.expect("patch should apply");

let migrator = MigratorBuilder::new(client.clone())
let mut migrator = MigratorBuilder::new(client.clone())
.track_latency(true)
.read(
|_| {
Expand Down Expand Up @@ -2246,8 +2249,8 @@ mod tests {

migrator
.read(
&context,
"stage-flag".into(),
context,
Stage::Off,
serde_json::Value::Null,
)
Expand Down
6 changes: 1 addition & 5 deletions launchdarkly-server-sdk/src/events/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl BaseEvent {
}
}

/// MigrationOpEventData is generated through the migration op tracker provided through the SDK.
/// A MigrationOpEvent is generated through the migration op tracker provided through the SDK.
#[derive(Clone, Debug)]
pub struct MigrationOpEvent {
pub(crate) base: BaseEvent,
Expand Down Expand Up @@ -131,21 +131,17 @@ impl Serialize for MigrationOpEvent {
key: self.key.clone(),
value: self.evaluation.value,
default: self.default_stage,
// QUESTION: In the ruby implementation, this can be nil. Why not here?
Copy link
Member Author

Choose a reason for hiding this comment

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

The ruby one is needlessly checking for nil. 🤦🏼

Copy link
Member

Choose a reason for hiding this comment

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

In client-side SDKs the reason is optional, so maybe it is for bootstrapping from ruby? But it would always be populated.

reason: self.evaluation.reason.clone(),
variation_index: self.evaluation.variation_index,
version: self.version,
};
state.serialize_field("evaluation", &evaluation)?;

// TODO: Add sampling here if it is set and not 1
Copy link
Member Author

Choose a reason for hiding this comment

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

Done above. Forgot to remove this then.


let mut measurements = vec![];
if !self.invoked.is_empty() {
measurements.push(MigrationOpMeasurement::Invoked(&self.invoked));
}

// TODO: There is something here to do with consistency check ratio
Copy link
Member Author

Choose a reason for hiding this comment

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

No there isn't.

if let Some(consistency_check) = self.consistency_check {
measurements.push(MigrationOpMeasurement::ConsistencyCheck(
consistency_check,
Expand Down
2 changes: 1 addition & 1 deletion launchdarkly-server-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub use feature_requester_builders::{
};
pub use launchdarkly_server_sdk_evaluation::{Flag, Segment, Versioned};
pub use migrations::{
ExecutionOrder, MigrationOpTracker, MigratorBuilder, Operation, Origin, Stage,
ExecutionOrder, MigrationOpTracker, Migrator, MigratorBuilder, Operation, Origin, Stage,
};
pub use service_endpoints::ServiceEndpointsBuilder;
pub use stores::persistent_store::{PersistentDataStore, PersistentStoreError};
Expand Down
Loading