Skip to content

Commit

Permalink
feat: Soft kill kv (#4582)
Browse files Browse the repository at this point in the history
Co-authored-by: Akshay S <akshay.s@Akshay-Subramanian-D66TQ6D97K.local>
Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed May 20, 2024
1 parent 5e84855 commit 3fa59d4
Show file tree
Hide file tree
Showing 32 changed files with 462 additions and 118 deletions.
1 change: 1 addition & 0 deletions config/development.toml
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ delay_between_retries_in_milliseconds = 500

[kv_config]
ttl = 900 # 15 * 60 seconds
soft_kill = false

[frm]
enabled = true
Expand Down
1 change: 1 addition & 0 deletions config/docker_compose.toml
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ queue_strategy = "Fifo"

[kv_config]
ttl = 900 # 15 * 60 seconds
soft_kill = false

[frm]
enabled = true
Expand Down
11 changes: 11 additions & 0 deletions crates/diesel_models/src/customers.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use common_enums::MerchantStorageScheme;
use common_utils::pii;
use diesel::{AsChangeset, Identifiable, Insertable, Queryable};
use time::PrimitiveDateTime;
Expand All @@ -21,6 +22,13 @@ pub struct CustomerNew {
pub created_at: PrimitiveDateTime,
pub modified_at: PrimitiveDateTime,
pub address_id: Option<String>,
pub updated_by: Option<String>,
}

impl CustomerNew {
pub fn update_storage_scheme(&mut self, storage_scheme: MerchantStorageScheme) {
self.updated_by = Some(storage_scheme.to_string());
}
}

impl From<CustomerNew> for Customer {
Expand All @@ -40,6 +48,7 @@ impl From<CustomerNew> for Customer {
modified_at: customer_new.modified_at,
address_id: customer_new.address_id,
default_payment_method_id: None,
updated_by: customer_new.updated_by,
}
}
}
Expand All @@ -61,6 +70,7 @@ pub struct Customer {
pub modified_at: PrimitiveDateTime,
pub address_id: Option<String>,
pub default_payment_method_id: Option<String>,
pub updated_by: Option<String>,
}

#[derive(
Expand All @@ -84,6 +94,7 @@ pub struct CustomerUpdateInternal {
pub connector_customer: Option<serde_json::Value>,
pub address_id: Option<String>,
pub default_payment_method_id: Option<Option<String>>,
pub updated_by: Option<String>,
}

impl CustomerUpdateInternal {
Expand Down
26 changes: 26 additions & 0 deletions crates/diesel_models/src/mandate.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use common_enums::MerchantStorageScheme;
use common_utils::pii;
use diesel::{AsChangeset, Identifiable, Insertable, Queryable};
use masking::Secret;
Expand Down Expand Up @@ -32,6 +33,7 @@ pub struct Mandate {
pub connector_mandate_ids: Option<pii::SecretSerdeValue>,
pub original_payment_id: Option<String>,
pub merchant_connector_id: Option<String>,
pub updated_by: Option<String>,
}

#[derive(
Expand Down Expand Up @@ -69,6 +71,13 @@ pub struct MandateNew {
pub connector_mandate_ids: Option<pii::SecretSerdeValue>,
pub original_payment_id: Option<String>,
pub merchant_connector_id: Option<String>,
pub updated_by: Option<String>,
}

impl MandateNew {
pub fn update_storage_scheme(&mut self, storage_scheme: MerchantStorageScheme) {
self.updated_by = Some(storage_scheme.to_string());
}
}

#[derive(Debug)]
Expand All @@ -90,6 +99,17 @@ pub enum MandateUpdate {
},
}

impl MandateUpdate {
pub fn convert_to_mandate_update(
self,
storage_scheme: MerchantStorageScheme,
) -> MandateUpdateInternal {
let mut updated_object = MandateUpdateInternal::from(self);
updated_object.updated_by = Some(storage_scheme.to_string());
updated_object
}
}

#[derive(Clone, Eq, PartialEq, Copy, Debug, Default, serde::Serialize, serde::Deserialize)]
pub struct SingleUseMandate {
pub amount: i64,
Expand All @@ -113,6 +133,7 @@ pub struct MandateUpdateInternal {
connector_mandate_id: Option<String>,
payment_method_id: Option<String>,
original_payment_id: Option<String>,
updated_by: Option<String>,
}

impl From<MandateUpdate> for MandateUpdateInternal {
Expand All @@ -125,6 +146,7 @@ impl From<MandateUpdate> for MandateUpdateInternal {
connector_mandate_id: None,
payment_method_id: None,
original_payment_id: None,
updated_by: None,
},
MandateUpdate::CaptureAmountUpdate { amount_captured } => Self {
mandate_status: None,
Expand All @@ -133,6 +155,7 @@ impl From<MandateUpdate> for MandateUpdateInternal {
connector_mandate_id: None,
payment_method_id: None,
original_payment_id: None,
updated_by: None,
},
MandateUpdate::ConnectorReferenceUpdate {
connector_mandate_ids,
Expand Down Expand Up @@ -165,6 +188,7 @@ impl MandateUpdateInternal {
connector_mandate_id,
payment_method_id,
original_payment_id,
updated_by,
} = self;

Mandate {
Expand All @@ -174,6 +198,7 @@ impl MandateUpdateInternal {
connector_mandate_id: connector_mandate_id.map_or(source.connector_mandate_id, Some),
payment_method_id: payment_method_id.unwrap_or(source.payment_method_id),
original_payment_id: original_payment_id.map_or(source.original_payment_id, Some),
updated_by: updated_by.map_or(source.updated_by, Some),
..source
}
}
Expand Down Expand Up @@ -208,6 +233,7 @@ impl From<&MandateNew> for Mandate {
connector_mandate_ids: mandate_new.connector_mandate_ids.clone(),
original_payment_id: mandate_new.original_payment_id.clone(),
merchant_connector_id: mandate_new.merchant_connector_id.clone(),
updated_by: mandate_new.updated_by.clone(),
}
}
}
31 changes: 31 additions & 0 deletions crates/diesel_models/src/payment_method.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use common_enums::MerchantStorageScheme;
use common_utils::pii;
use diesel::{AsChangeset, Identifiable, Insertable, Queryable};
use masking::Secret;
Expand Down Expand Up @@ -41,6 +42,7 @@ pub struct PaymentMethod {
pub network_transaction_id: Option<String>,
pub client_secret: Option<String>,
pub payment_method_billing_address: Option<Encryption>,
pub updated_by: Option<String>,
}

#[derive(
Expand Down Expand Up @@ -77,6 +79,13 @@ pub struct PaymentMethodNew {
pub network_transaction_id: Option<String>,
pub client_secret: Option<String>,
pub payment_method_billing_address: Option<Encryption>,
pub updated_by: Option<String>,
}

impl PaymentMethodNew {
pub fn update_storage_scheme(&mut self, storage_scheme: MerchantStorageScheme) {
self.updated_by = Some(storage_scheme.to_string());
}
}

#[derive(Debug, Eq, PartialEq, Deserialize, Serialize)]
Expand Down Expand Up @@ -116,6 +125,17 @@ pub enum PaymentMethodUpdate {
},
}

impl PaymentMethodUpdate {
pub fn convert_to_payment_method_update(
self,
storage_scheme: MerchantStorageScheme,
) -> PaymentMethodUpdateInternal {
let mut update_internal: PaymentMethodUpdateInternal = self.into();
update_internal.updated_by = Some(storage_scheme.to_string());
update_internal
}
}

#[derive(
Clone, Debug, Default, AsChangeset, router_derive::DebugAsDisplay, Serialize, Deserialize,
)]
Expand All @@ -129,6 +149,7 @@ pub struct PaymentMethodUpdateInternal {
locker_id: Option<String>,
payment_method: Option<storage_enums::PaymentMethod>,
connector_mandate_details: Option<serde_json::Value>,
updated_by: Option<String>,
payment_method_type: Option<storage_enums::PaymentMethodType>,
payment_method_issuer: Option<String>,
}
Expand All @@ -148,6 +169,7 @@ impl PaymentMethodUpdateInternal {
network_transaction_id,
status,
connector_mandate_details,
updated_by,
..
} = self;

Expand All @@ -160,6 +182,7 @@ impl PaymentMethodUpdateInternal {
status: status.unwrap_or(source.status),
connector_mandate_details: connector_mandate_details
.map_or(source.connector_mandate_details, Some),
updated_by: updated_by.map_or(source.updated_by, Some),
..source
}
}
Expand All @@ -177,6 +200,7 @@ impl From<PaymentMethodUpdate> for PaymentMethodUpdateInternal {
locker_id: None,
payment_method: None,
connector_mandate_details: None,
updated_by: None,
payment_method_issuer: None,
payment_method_type: None,
},
Expand All @@ -191,6 +215,7 @@ impl From<PaymentMethodUpdate> for PaymentMethodUpdateInternal {
locker_id: None,
payment_method: None,
connector_mandate_details: None,
updated_by: None,
payment_method_issuer: None,
payment_method_type: None,
},
Expand All @@ -203,6 +228,7 @@ impl From<PaymentMethodUpdate> for PaymentMethodUpdateInternal {
locker_id: None,
payment_method: None,
connector_mandate_details: None,
updated_by: None,
payment_method_issuer: None,
payment_method_type: None,
},
Expand All @@ -218,6 +244,7 @@ impl From<PaymentMethodUpdate> for PaymentMethodUpdateInternal {
locker_id: None,
payment_method: None,
connector_mandate_details: None,
updated_by: None,
payment_method_issuer: None,
payment_method_type: None,
},
Expand All @@ -230,6 +257,7 @@ impl From<PaymentMethodUpdate> for PaymentMethodUpdateInternal {
locker_id: None,
payment_method: None,
connector_mandate_details: None,
updated_by: None,
payment_method_issuer: None,
payment_method_type: None,
},
Expand All @@ -249,6 +277,7 @@ impl From<PaymentMethodUpdate> for PaymentMethodUpdateInternal {
locker_id,
payment_method,
connector_mandate_details: None,
updated_by: None,
payment_method_issuer,
payment_method_type,
},
Expand All @@ -263,6 +292,7 @@ impl From<PaymentMethodUpdate> for PaymentMethodUpdateInternal {
payment_method: None,
connector_mandate_details,
network_transaction_id: None,
updated_by: None,
payment_method_issuer: None,
payment_method_type: None,
},
Expand Down Expand Up @@ -302,6 +332,7 @@ impl From<&PaymentMethodNew> for PaymentMethod {
status: payment_method_new.status,
network_transaction_id: payment_method_new.network_transaction_id.clone(),
client_secret: payment_method_new.client_secret.clone(),
updated_by: payment_method_new.updated_by.clone(),
payment_method_billing_address: payment_method_new
.payment_method_billing_address
.clone(),
Expand Down
6 changes: 6 additions & 0 deletions crates/diesel_models/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ diesel::table! {
address_id -> Nullable<Varchar>,
#[max_length = 64]
default_payment_method_id -> Nullable<Varchar>,
#[max_length = 64]
updated_by -> Nullable<Varchar>,
}
}

Expand Down Expand Up @@ -590,6 +592,8 @@ diesel::table! {
original_payment_id -> Nullable<Varchar>,
#[max_length = 32]
merchant_connector_id -> Nullable<Varchar>,
#[max_length = 64]
updated_by -> Nullable<Varchar>,
}
}

Expand Down Expand Up @@ -933,6 +937,8 @@ diesel::table! {
#[max_length = 128]
client_secret -> Nullable<Varchar>,
payment_method_billing_address -> Nullable<Bytea>,
#[max_length = 64]
updated_by -> Nullable<Varchar>,
}
}

Expand Down
5 changes: 4 additions & 1 deletion crates/router/src/configs/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ impl Default for super::settings::DrainerSettings {
#[cfg(feature = "kv_store")]
impl Default for super::settings::KvConfig {
fn default() -> Self {
Self { ttl: 900 }
Self {
ttl: 900,
soft_kill: Some(false),
}
}
}

Expand Down
15 changes: 14 additions & 1 deletion crates/router/src/configs/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use external_services::{
},
};
use hyperswitch_interfaces::secrets_interface::secret_state::{
SecretState, SecretStateContainer, SecuredSecret,
RawSecret, SecretState, SecretStateContainer, SecuredSecret,
};
use masking::Secret;
use redis_interface::RedisSettings;
Expand Down Expand Up @@ -138,6 +138,7 @@ pub struct Frm {
#[derive(Debug, Deserialize, Clone)]
pub struct KvConfig {
pub ttl: u32,
pub soft_kill: Option<bool>,
}

#[derive(Debug, Deserialize, Clone, Default)]
Expand Down Expand Up @@ -759,6 +760,18 @@ impl Settings<SecuredSecret> {
}
}

impl Settings<RawSecret> {
#[cfg(feature = "kv_store")]
pub fn is_kv_soft_kill_mode(&self) -> bool {
self.kv_config.soft_kill.unwrap_or(false)
}

#[cfg(not(feature = "kv_store"))]
pub fn is_kv_soft_kill_mode(&self) -> bool {
false
}
}

#[cfg(feature = "payouts")]
#[derive(Debug, Deserialize, Clone, Default)]
pub struct Payouts {
Expand Down
7 changes: 7 additions & 0 deletions crates/router/src/core/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,13 @@ pub async fn kv_for_merchant(
Ok(merchant_account)
}
(true, MerchantStorageScheme::PostgresOnly) => {
if state.conf.as_ref().is_kv_soft_kill_mode() {
Err(errors::ApiErrorResponse::InvalidRequestData {
message: "Kv cannot be enabled when application is in soft_kill_mode"
.to_owned(),
})?
}

db.update_merchant(
merchant_account,
storage::MerchantAccountUpdate::StorageSchemeUpdate {
Expand Down
1 change: 1 addition & 0 deletions crates/router/src/core/customers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ pub async fn create_customer(
created_at: common_utils::date_time::now(),
modified_at: common_utils::date_time::now(),
default_payment_method_id: None,
updated_by: None,
})
}
.await
Expand Down
1 change: 1 addition & 0 deletions crates/router/src/core/payment_methods/cards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ pub async fn create_payment_method(
last_modified: current_time,
last_used_at: current_time,
payment_method_billing_address,
updated_by: None,
},
storage_scheme,
)
Expand Down
1 change: 1 addition & 0 deletions crates/router/src/core/payments/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1608,6 +1608,7 @@ pub async fn create_customer_if_not_exist<'a, F: Clone, R>(
connector_customer: None,
address_id: None,
default_payment_method_id: None,
updated_by: None,
},
)
}
Expand Down

0 comments on commit 3fa59d4

Please sign in to comment.