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

add snapshot while context , default configs updates #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

pratikmishra356
Copy link
Collaborator

@pratikmishra356 pratikmishra356 commented Apr 24, 2024

Problem

Record for config's history after every cac_config changes,
we also encountered problem regarding generating snowflake_id, we were not dropping the acquired mutex lock,
and when generating another snowflake_id , it gets stuck in deadlock kind of situation

Solution

Created config_versions table which will store the config's snapshot after every updates which affect the configs.
Fetch contexts and default_configs and insert into config_Versions table after any updates within the same transaction.
we are explicitly dropping acquired lock after snowflake_id generation
Also accepting tags parameters in every update request so that user can tag every versions

Environment variable changes

NA

Pre-deployment activity

Create db changes

Post-deployment activity

NA

API changes

get_config apis we will now provide priority inside context objects
adding optional tags field in every context, default_config and experiments updates
accepting version queryparam in every get_config apis

Possible Issues in the future

NA

@pratikmishra356 pratikmishra356 requested a review from a team as a code owner April 24, 2024 10:06
@pratikmishra356 pratikmishra356 force-pushed the snapshot-api-changes branch 10 times, most recently from b6410a5 to b7c93df Compare April 29, 2024 06:49
crates/context_aware_config/src/api/config/handlers.rs Outdated Show resolved Hide resolved
@@ -313,7 +356,8 @@ async fn get_filtered_config(
.map_or_else(|_| json!(value), |int_val| json!(int_val)),
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we can now deprecate this handler.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

why?

crates/context_aware_config/src/api/context/handlers.rs Outdated Show resolved Hide resolved
@@ -134,6 +135,8 @@ async fn main() -> Result<()> {
return view! { <App app_envs=routes_ui_envs.clone()/> };
});

let snowflake_generator = Arc::new(Mutex::new(SnowflakeIdGenerator::new(1, 1)));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why add Arc? as per rust docs

Shared references in Rust disallow mutation by default, and Arc is no exception: you cannot generally obtain a mutable reference to something inside an Arc. If you need to mutate through an Arc, use Mutex, RwLock, or one of the Atomic types.

If we aren't mutating the snowflakeIdGenerator, just use Arc otherwise there is no need to add it

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@Datron arc is used to share the same mutex instance
you can check the example for mutex implementation
/// use std::sync::{Arc, Mutex}; /// use std::thread; /// /// let mutex = Arc::new(Mutex::new(0)); /// let c_mutex = Arc::clone(&mutex); /// /// thread::spawn(move || { /// let mut lock = c_mutex.try_lock(); /// if let Ok(ref mut mutex) = lock { /// **mutex = 10; /// } else { /// println!("try_lock failed"); /// } /// }).join().expect("thread::spawn failed"); /// assert_eq!(*mutex.lock().unwrap(), 10);

crates/context_aware_config/src/api/config/handlers.rs Outdated Show resolved Hide resolved
crates/context_aware_config/src/api/config/handlers.rs Outdated Show resolved Hide resolved
crates/context_aware_config/src/api/config/handlers.rs Outdated Show resolved Hide resolved
crates/context_aware_config/src/api/config/mod.rs Outdated Show resolved Hide resolved
crates/context_aware_config/src/api/context/handlers.rs Outdated Show resolved Hide resolved
crates/context_aware_config/src/api/context/handlers.rs Outdated Show resolved Hide resolved
@Datron
Copy link
Collaborator

Datron commented Apr 30, 2024

Can we squash all commits into one?

Copy link
Collaborator

@Datron Datron left a comment

Choose a reason for hiding this comment

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

What happens when someone tries to update tags? Is that possible?

crates/context_aware_config/src/api/config/handlers.rs Outdated Show resolved Hide resolved
@@ -177,12 +221,13 @@ async fn get(
query_params_map.insert(
key,
value
.parse::<i32>()
.parse::<i64>()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why bump up to i64?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@Datron expecting version_id in query params, which is snowflake_id and it is i64

config -> Json,
config_hash -> Text,
- tags -> Nullable<Array<Nullable<Varchar>>>,
+ tags -> Nullable<Array<Varchar>>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would like to avoid this patch file. Can we make the type in DB in such a way that we can avoid this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We can concatenate tags with comma and store it as string , but then the DB filter query will not be optimised
https://diesel.rs/guides/configuring-diesel-cli.html
https://diesel.rs/guides/migration_guide.html

@pratikmishra356
Copy link
Collaborator Author

pratikmishra356 commented May 20, 2024

What happens when someone tries to update tags? Is that possible?

@Datron currently this is not supported , a different api will be needed
also we need to have list versions api with filter on tags support
these changes can be added later on , this pr already has lot of changes

crates/context_aware_config/src/api/config/handlers.rs Outdated Show resolved Hide resolved
crates/context_aware_config/src/api/context/handlers.rs Outdated Show resolved Hide resolved
@@ -5,11 +5,18 @@ use serde_json::{Map, Value};
pub struct PutReq {
pub context: Map<String, Value>,
pub r#override: Map<String, Value>,
pub tags: Option<Vec<String>>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we accept tags as headers? These don't really belong here, and the name is also misleading should be snapshot_tags

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

headers can be used when its value is consistent across similar requests or let say it is a global level identifier for requests .
although tags is consistent across all write requests , I think we should not add this in header because probably tags can be changed based on the different operations (c, u, d) on different entities (default_configs, contexts)

also we have other entities and request where this header is of no use ( like functions, types, dimensions, get apis ) so for me it probably seems more close to a create request than a global request identifier
will change tags -> version_tags

@knutties @Datron your thoughts ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

tags is a actually a metadata here, and not exactly the part of the request, are we using this to tag the actual resource we are touching in these request, the answer is no, these tags in fact are used to tag the versions created after the actual defined job for the request is completed.

Copy link
Collaborator

Choose a reason for hiding this comment

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

We are repeating everywhere too, all request types are changed, can't we do something to abstract this out.

.do_update()
.set(&default_config)
.execute(transaction_conn);
add_config_version(&state, req.tags, transaction_conn)?;
Copy link
Collaborator

Choose a reason for hiding this comment

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

@pratikmishra356 , do we have to fail a request because we weren't able to create a snapshot.
What I think, the user never intended to make a snapshot, he just wanted to create a default_config/context/....., creating a snapshot should be our separate task, I feel we really shouldn't fail the endpoints when we fail to create a snapshot

@knutties @Datron , what are you thoughts on this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

since currently we dont have any process/event based tracking mechanisms to do these operations in async , we might lose track of updates on configs thats why we followed this behavior ,
we assume every updates on default_config and contexts is crucial which results in changing the configs , and since now we are relying only on config_version table for serving configs ( i.e no more db fetch from multiple tables ) it is important to have this both operation in sync

@pratikmishra356 pratikmishra356 force-pushed the snapshot-api-changes branch 2 times, most recently from 0a00a77 to f006a38 Compare May 22, 2024 11:40
@pratikmishra356 pratikmishra356 force-pushed the snapshot-api-changes branch 2 times, most recently from 0305021 to 6b61691 Compare May 24, 2024 06:20
@Datron Datron added the priority Things that need immediate attention label May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority Things that need immediate attention
Projects
None yet
4 participants