Skip to content
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
281 changes: 214 additions & 67 deletions crates/context_aware_config/src/api/context/handlers.rs

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

let version_id = add_config_version(
&state,
tags,
req_change_reason.into(),
transaction_conn,
&workspace_context.schema_name,
)?;
Ok((put_response, version_id))
})?;

Can you add the trigger for the webhook in add_config_version?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No , add_config_version is synchronous and the trigger is async , we cannot call async function inside transaction.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

But you can return future which can be awaited on outside the synchronous block of the transaction

Anyways, on failure of webhook we dont even need to revert the queries so response awaiting outside transaction is safe as well

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

There is a slight problem with this approach - I would be using execute_webhook_call which needs &mut DBConnection, but the transaction still holds the mutable borrow when we create the future. The future can't capture db_conn — the borrow checker won't allow it.
You can't return a future that captures &mut DBConnection from inside a transaction because the connection's lifetime is scoped to the transaction closure.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Explanation:
Modified add_config_version:

pub fn add_config_version<'a, T>(
    state: &'a Data<AppState>,
    tags: Option<Vec<String>>,
    description: Description,
    db_conn: &'a mut DBConnection,
    workspace_context: &'a WorkspaceContext,
    payload: &'a T,
) -> superposition::Result<(i64, Pin<Box<dyn Future<Output = bool> + Send + 'a>>)>
where
    T: Serialize + Send + Sync,
{
    use config_versions::dsl::config_versions;
    let version_id = generate_snowflake_id(state)?;
    let config = generate_cac(db_conn, &workspace_context.schema_name)?;
    let json_config = json!(config);
    let config_hash = blake3::hash(json_config.to_string().as_bytes()).to_string();

    let config_version = ConfigVersion {
        id: version_id,
        config: json_config,
        config_hash,
        tags,
        created_at: Utc::now(),
        description,
    };
    diesel::insert_into(config_versions)
        .values(&config_version)
        .returning(ConfigVersion::as_returning())
        .schema_name(&workspace_context.schema_name)
        .execute(db_conn)?;

    let webhook_future = Box::pin(async move {
        execute_webhook_call(
            payload,
            &Some(version_id.to_string()),
            workspace_context,
            WebhookEvent::ConfigChanged,
            state,
            db_conn,
        )
        .await
    });

    Ok((version_id, webhook_future))
}

Sample Usage:

#[authorized]
#[put("")]
async fn create_handler(
    workspace_context: WorkspaceContext,
    state: Data<AppState>,
    custom_headers: CustomHeaders,
    req: Json<PutRequest>,
    mut db_conn: DbConnection,
    user: User,
) -> superposition::Result<HttpResponse> {
    let tags = parse_config_tags(custom_headers.config_tags)?;
    let description = match req.description.clone() {
        Some(val) => val,
        None => {
            // TODO: get rid of `query_description` function altogether
            let resp = query_description(
                Value::Object(req.context.clone().into_inner().into()),
                &mut db_conn,
                &workspace_context.schema_name,
            );
            match resp {
                Err(AppError::DbError(diesel::result::Error::NotFound)) => {
                    return Err(bad_argument!(
                        "Description is required when context does not exist"
                    ));
                }
                Ok(desc) => desc,
                Err(e) => return Err(e),
            }
        }
    };
    let req_change_reason = req.change_reason.clone();

    validate_change_reason(
        &workspace_context,
        &req_change_reason,
        &mut db_conn,
        &state.master_encryption_key,
    )?;

    let (put_response, version_id, webhook_future) = db_conn
        .transaction::<_, superposition::AppError, _>(|transaction_conn| {
            let put_response = operations::upsert(
                req.into_inner(),
                description,
                transaction_conn,
                true,
                &user,
                &workspace_context,
                false,
                &state.master_encryption_key,
            )
            .map_err(|err: superposition::AppError| {
                log::error!("context put failed with error: {:?}", err);
                err
            })?;

            let (version_id, future_webhook) = add_config_version(
                &state,
                tags,
                req_change_reason.into(),
                transaction_conn,
                &workspace_context,
                &put_response,
            )?;
            Ok((put_response, version_id, future_webhook))
        })?;

    let webhook_status = webhook_future.await;

    let mut http_resp = if webhook_status {
        HttpResponse::Ok()
    } else {
        HttpResponse::build(
            actix_web::http::StatusCode::from_u16(512)
                .unwrap_or(actix_web::http::StatusCode::INTERNAL_SERVER_ERROR),
        )
    };

    http_resp.insert_header((
        AppHeader::XConfigVersion.to_string(),
        version_id.to_string(),
    ));

    #[cfg(feature = "high-performance-mode")]
    put_config_in_redis(version_id, state, &workspace_context.schema_name, &mut conn)
        .await?;

    Ok(http_resp.json(put_response))
}

You will get an error saying :

lifetime may not live long enough
returning this value requires that `'1` must outlive `'2`

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The problem is that the future borrows transaction_conn, which only lives for the duration of the transaction closure — but we're trying to return the future out of the closure and .await it after the transaction completes.
We can't borrow transaction_conn in a future that outlives the transaction.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why do you need db conn with async stuff ?
The only async part is API call, which does not even require db conn

Large diffs are not rendered by default.

97 changes: 48 additions & 49 deletions crates/context_aware_config/src/api/context/operations.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use actix_web::web::Json;
use chrono::Utc;
use diesel::{
Connection, ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl, SelectableHelper,
Connection, ExpressionMethods, OptionalExtension, PgConnection, QueryDsl,
RunQueryDsl, SelectableHelper,
r2d2::{ConnectionManager, PooledConnection},
result::{DatabaseErrorKind::*, Error::DatabaseError},
};
Expand All @@ -11,7 +12,10 @@ use superposition_core::helpers::{calculate_context_weight, hash};
use superposition_macros::{db_error, not_found, unexpected_error};
use superposition_types::{
DBConnection, Overrides, User,
api::context::{Identifier, MoveRequest, PutRequest, UpdateRequest},
api::{
context::{Identifier, MoveRequest, PutRequest, UpdateRequest},
webhook::Action,
},
database::{
models::{Description, cac::Context},
schema::contexts::{self, dsl},
Expand Down Expand Up @@ -143,17 +147,23 @@ pub fn update(
.map_err(|e| db_error!(e))
}

pub struct MoveResult {
pub deleted_context: Context,
pub context: Context,
pub action: Action,
}

#[allow(clippy::too_many_arguments)]
pub fn r#move(
workspace_context: &WorkspaceContext,
old_ctx_id: String,
req: Json<MoveRequest>,
req_description: Description,
conn: &mut PooledConnection<ConnectionManager<PgConnection>>,
conn: &mut DBConnection,
already_under_txn: bool,
user: &User,
master_encryption_key: &Option<EncryptionKey>,
) -> result::Result<Context> {
) -> result::Result<MoveResult> {
use contexts::dsl;
let req = req.into_inner();
let ctx_condition = req.context.to_owned().into_inner();
Expand All @@ -176,6 +186,11 @@ pub fn r#move(
diesel::sql_query("SAVEPOINT update_ctx_savepoint").execute(conn)?;
}

let old_context = dsl::contexts
.filter(dsl::id.eq(&old_ctx_id))
.schema_name(&workspace_context.schema_name)
.get_result::<Context>(conn)?;

let context = diesel::update(dsl::contexts)
.filter(dsl::id.eq(&old_ctx_id))
.set((
Expand Down Expand Up @@ -205,46 +220,33 @@ pub fn r#move(
change_reason,
};

let handle_unique_violation =
|db_conn: &mut DBConnection, already_under_txn: bool| {
if already_under_txn {
let deleted_ctxt = diesel::delete(dsl::contexts)
.filter(dsl::id.eq(&old_ctx_id))
.schema_name(&workspace_context.schema_name)
.get_result(db_conn)?;
let delete_and_update = |conn: &mut DBConnection| {
let deleted_ctxt = diesel::delete(dsl::contexts)
.filter(dsl::id.eq(&old_ctx_id))
.schema_name(&workspace_context.schema_name)
.get_result(conn)?;

let ctx = contruct_new_ctx_with_old_overrides(deleted_ctxt);
update_override_of_existing_ctx(
db_conn,
ctx,
user,
&workspace_context.schema_name,
)
} else {
db_conn.transaction(|conn| {
let deleted_ctxt = diesel::delete(dsl::contexts)
.filter(dsl::id.eq(&old_ctx_id))
.schema_name(&workspace_context.schema_name)
.get_result(conn)?;
let ctx = contruct_new_ctx_with_old_overrides(deleted_ctxt);
update_override_of_existing_ctx(
conn,
ctx,
user,
&workspace_context.schema_name,
)
})
}
};
let ctx = contruct_new_ctx_with_old_overrides(deleted_ctxt);
update_override_of_existing_ctx(conn, ctx, user, &workspace_context.schema_name)
};

match context {
Ok(ctx) => Ok(ctx),
Err(DatabaseError(UniqueViolation, _)) => {
if already_under_txn {
diesel::sql_query("ROLLBACK TO update_ctx_savepoint").execute(conn)?;
}
handle_unique_violation(conn, already_under_txn)
Ok(ctx) => Ok(MoveResult {
deleted_context: old_context,
context: ctx,
action: Action::Create,
}),
Err(DatabaseError(UniqueViolation, _)) => if already_under_txn {
diesel::sql_query("ROLLBACK TO update_ctx_savepoint").execute(conn)?;
delete_and_update(conn)
} else {
conn.transaction(delete_and_update)
}
.map(|ctx| MoveResult {
deleted_context: old_context,
context: ctx,
action: Action::Update,
}),
Err(e) => {
log::error!("failed to move context with db error: {:?}", e);
Err(db_error!(e))
Expand All @@ -257,7 +259,7 @@ pub fn delete(
user: &User,
conn: &mut DBConnection,
schema_name: &SchemaName,
) -> result::Result<()> {
) -> result::Result<Context> {
use contexts::dsl;
diesel::update(dsl::contexts)
.filter(dsl::id.eq(&ctx_id))
Expand All @@ -270,16 +272,13 @@ pub fn delete(
.execute(conn)?;
let deleted_row = diesel::delete(dsl::contexts.filter(dsl::id.eq(&ctx_id)))
.schema_name(schema_name)
.execute(conn);
.get_result(conn)
.optional()?;
match deleted_row {
Ok(0) => Err(not_found!("Context Id `{}` doesn't exists", ctx_id)),
Ok(_) => {
None => Err(not_found!("Context Id `{}` doesn't exists", ctx_id)),
Some(context) => {
log::info!("{ctx_id} context deleted by {}", user.get_email());
Ok(())
}
Err(e) => {
log::error!("context delete query failed with error: {e}");
Err(unexpected_error!("Something went wrong."))
Ok(context)
}
}
}
141 changes: 102 additions & 39 deletions crates/context_aware_config/src/api/default_config/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use actix_web::{
};
use chrono::Utc;
use diesel::{
Connection, ExpressionMethods, QueryDsl, RunQueryDsl, SelectableHelper,
TextExpressionMethods,
Connection, ExpressionMethods, OptionalExtension, QueryDsl, RunQueryDsl,
SelectableHelper, TextExpressionMethods,
};
use jsonschema::ValidationError;
use serde_json::Value;
use service_utils::{
helpers::parse_config_tags,
helpers::{WebhookData, execute_webhook_call, parse_config_tags},
service::types::{
AppHeader, AppState, CustomHeaders, DbConnection, EncryptionKey, SchemaName,
WorkspaceContext,
Expand All @@ -22,19 +22,21 @@ use superposition_macros::{
bad_argument, db_error, not_found, unexpected_error, validation_error,
};
use superposition_types::{
DBConnection, PaginatedResponse, User,
DBConnection, PaginatedResponse, Resource, User,
api::{
default_config::{
DefaultConfigCreateRequest, DefaultConfigFilters, DefaultConfigKey,
DefaultConfigUpdateRequest,
},
functions::{FunctionEnvironment, FunctionExecutionRequest, KeyType},
webhook::Action,
},
custom_query::PaginationParams,
database::{
models::{
Description,
cac::{self as models, Context, DefaultConfig, FunctionType},
others::WebhookEvent,
},
schema::{self, contexts::dsl::contexts, default_configs::dsl},
},
Expand Down Expand Up @@ -165,9 +167,33 @@ async fn create_handler(
})?;

#[cfg(feature = "high-performance-mode")]
put_config_in_redis(version_id, state, &workspace_context.schema_name, &mut conn)
.await?;
let mut http_resp = HttpResponse::Ok();
put_config_in_redis(
version_id,
&state,
&workspace_context.schema_name,
&mut conn,
)
.await?;

let data = WebhookData {
payload: &default_config,
resource: Resource::DefaultConfig,
event: WebhookEvent::ConfigChanged,
config_version_opt: Some(version_id.to_string()),
action: Action::Create,
};

let webhook_status =
execute_webhook_call(data, &workspace_context, &state, &mut conn).await;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

here as well


let mut http_resp = if webhook_status {
HttpResponse::Ok()
} else {
HttpResponse::build(
actix_web::http::StatusCode::from_u16(512)
.unwrap_or(actix_web::http::StatusCode::INTERNAL_SERVER_ERROR),
)
};

http_resp.insert_header((
AppHeader::XConfigVersion.to_string(),
Expand Down Expand Up @@ -297,10 +323,33 @@ async fn update_handler(
})?;

#[cfg(feature = "high-performance-mode")]
put_config_in_redis(version_id, state, &workspace_context.schema_name, &mut conn)
.await?;
put_config_in_redis(
version_id,
&state,
&workspace_context.schema_name,
&mut conn,
)
.await?;

let data = WebhookData {
payload: &db_row,
resource: Resource::DefaultConfig,
event: WebhookEvent::ConfigChanged,
config_version_opt: Some(version_id.to_string()),
action: Action::Update,
};

let webhook_status =
execute_webhook_call(data, &workspace_context, &state, &mut conn).await;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

here as well


let mut http_resp = HttpResponse::Ok();
let mut http_resp = if webhook_status {
HttpResponse::Ok()
} else {
HttpResponse::build(
actix_web::http::StatusCode::from_u16(512)
.unwrap_or(actix_web::http::StatusCode::INTERNAL_SERVER_ERROR),
)
};
http_resp.insert_header((
AppHeader::XConfigVersion.to_string(),
version_id.to_string(),
Expand Down Expand Up @@ -461,14 +510,13 @@ async fn delete_handler(
let tags = parse_config_tags(custom_headers.config_tags)?;

let key: String = path.into_inner().into();
let mut version_id = 0;

let context_ids =
get_key_usage_context_ids(&key, &mut conn, &workspace_context.schema_name)
.map_err(|_| unexpected_error!("Something went wrong"))?;
if context_ids.is_empty() {
let resp: Result<HttpResponse, superposition::AppError> =
conn.transaction::<_, superposition::AppError, _>(|transaction_conn| {
let (version_id, default_config) = conn
.transaction::<_, superposition::AppError, _>(|transaction_conn| {
diesel::update(dsl::default_configs)
.filter(dsl::key.eq(&key))
.set((
Expand All @@ -481,18 +529,19 @@ async fn delete_handler(
let deleted_row =
diesel::delete(dsl::default_configs.filter(dsl::key.eq(&key)))
.schema_name(&workspace_context.schema_name)
.execute(transaction_conn);
.get_result::<DefaultConfig>(transaction_conn)
.optional()?;
match deleted_row {
Ok(0) => {
Err(not_found!("default config key `{}` doesn't exists", key))
None => {
Err(not_found!("default config key `{}` doesn't exists", key))?
}
Ok(_) => {
Some(default_config) => {
let config_version_desc = Description::try_from(format!(
"Context Deleted by {}",
user.get_email()
))
.map_err(|e| unexpected_error!(e))?;
version_id = add_config_version(
let version_id = add_config_version(
&state,
tags,
config_version_desc,
Expand All @@ -503,31 +552,45 @@ async fn delete_handler(
"default config key: {key} deleted by {}",
user.get_email()
);
Ok(HttpResponse::NoContent()
.insert_header((
AppHeader::XConfigVersion.to_string(),
version_id.to_string(),
))
.finish())
}
Err(e) => {
log::error!("default config delete query failed with error: {e}");
Err(unexpected_error!("Something went wrong."))
Ok((version_id, default_config))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

this is a very weird code, version_id is a mutable value which is being updated as well as returned
can only return from here and not have it as a mutable variable as well

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Again , this also existed before. We can do the cleanup though.

}
}
});
})?;

if resp.is_ok() {
#[cfg(feature = "high-performance-mode")]
put_config_in_redis(
version_id,
state,
&workspace_context.schema_name,
&mut conn,
#[cfg(feature = "high-performance-mode")]
put_config_in_redis(
version_id,
&state,
&workspace_context.schema_name,
&mut conn,
)
.await?;

let data = WebhookData {
payload: &default_config,
resource: Resource::DefaultConfig,
event: WebhookEvent::ConfigChanged,
config_version_opt: Some(version_id.to_string()),
action: Action::Delete,
};

let webhook_status =
execute_webhook_call(data, &workspace_context, &state, &mut conn).await;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

here as well


let mut http_resp = if webhook_status {
HttpResponse::Ok()
} else {
HttpResponse::build(
actix_web::http::StatusCode::from_u16(512)
.unwrap_or(actix_web::http::StatusCode::INTERNAL_SERVER_ERROR),
)
.await?;
}
resp
};
http_resp.insert_header((
AppHeader::XConfigVersion.to_string(),
version_id.to_string(),
));

Ok(http_resp.finish())
} else {
Err(bad_argument!(
"Given key already in use in contexts: {}",
Expand Down
Loading
Loading