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

feat(routing): Contract based routing integration #6761

Open
wants to merge 54 commits into
base: main
Choose a base branch
from

Conversation

Sarthak1799
Copy link
Contributor

@Sarthak1799 Sarthak1799 commented Dec 5, 2024

Type of Change

  • Bugfix
  • New feature
  • Enhancement
  • Refactoring
  • Dependency updates
  • Documentation
  • CI/CD

Description

  • Built an interface for Contract based routing
  • Integrated Contract based routing with hyperswitch
  • Built new APIs for contract routing config setup
  • Refactored some existing dynamic routing code

Additional Changes

  • This PR modifies the API contract
  • This PR modifies the database schema
  • This PR modifies application configuration/environment variables

Motivation and Context

How did you test it?

  1. Enable Contract routing config
curl --location --request POST 'http://localhost:8080/account/sarthak2/business_profile/pro_YozTgS8HebvBlk0UaeWW/dynamic_routing/contracts/toggle?enable=dynamic_connector_selection' \
--header 'api-key: dev_1aD8YuFd6Ovanf3oOtAYobqI6qw4ZBRSfw6BgYbifWHpaopkisBtm8obNXkFbVJn' \
--header 'Content-Type: application/json' \
--data-raw '{
    "config": {
        "constants": [0.7,0.35],
        "time_scale": "day"
    },
    "label_info": [{
        "label": "stripe",
        "target_count": 10000,
        "incremental_count": 0,
        "target_time": 1780486655,
        "mca_id": "mca_5Mk0Qcum2tnbmKoyqeuL"
    }]
}'

Response -

{
    "id": "routing_FlQ49V71J8ZCXEwPzFy9",
    "profile_id": "pro_YozTgS8HebvBlk0UaeWW",
    "name": "Contract based dynamic routing algorithm",
    "kind": "dynamic",
    "description": "",
    "created_at": 1734009361,
    "modified_at": 1734009361,
    "algorithm_for": "payment"
}
  1. Set Volume split for dynamic routing
curl --location --request POST 'http://localhost:8080/account/sarthak2/business_profile/pro_YozTgS8HebvBlk0UaeWW/dynamic_routing/set_volume_split?split=80' \
--header 'api-key: dev_1aD8YuFd6Ovanf3oOtAYobqI6qw4ZBRSfw6BgYbifWHpaopkisBtm8obNXkFbVJn'
  1. Create a Payment
    Metrics Population post payment -
image

Checklist

  • I formatted the code cargo +nightly fmt --all
  • I addressed lints thrown by cargo clippy
  • I reviewed the submitted code
  • I added unit tests for my changes where possible

@Sarthak1799 Sarthak1799 self-assigned this Dec 5, 2024
@Sarthak1799 Sarthak1799 requested review from a team as code owners December 5, 2024 20:19
Copy link

semanticdiff-com bot commented Dec 5, 2024

@Sarthak1799 Sarthak1799 added A-core Area: Core flows A-routing Area: Routing labels Dec 5, 2024
@Sarthak1799 Sarthak1799 linked an issue Dec 12, 2024 that may be closed by this pull request
2 tasks
Comment on lines +574 to +576
fn get_enabled_features(&mut self) -> &mut DynamicRoutingFeatures {
&mut self.enabled_feature
}
Copy link
Contributor

Choose a reason for hiding this comment

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

this method is supposed to be a get call right as per the name, but we are returning mutable reference. should we have separate method for get and set instead of returning mutable reference.
Can be taken in separate PR

Comment on lines +613 to +615
if let Some(contract_based_routing) = new.contract_based_routing {
self.contract_based_routing = Some(contract_based_routing)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

this update method currently updates all the fields. its better to have separate update methods for each fields to improve visibility
Again can be taken up in separate PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is updating the algorithm right?

crates/api_models/src/routing.rs Outdated Show resolved Hide resolved
crates/api_models/src/routing.rs Outdated Show resolved Hide resolved
crates/router/src/routes/routing.rs Outdated Show resolved Hide resolved
crates/router/src/core/payments/routing.rs Outdated Show resolved Hide resolved
crates/router/src/core/payments/routing.rs Outdated Show resolved Hide resolved
crates/router/src/core/payments/routing.rs Outdated Show resolved Hide resolved
crates/router/src/core/payments/routing.rs Show resolved Hide resolved
.transpose()
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("unable to deserialize DynamicRoutingAlgorithmRef from JSON")?
.unwrap_or_default();
Copy link
Contributor

Choose a reason for hiding this comment

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

why not just do if let on business_profile.dynamic_routing_algorithm instead of doing unwrap_or_default

Copy link
Contributor Author

Choose a reason for hiding this comment

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

let expressions in the same condition are unstable. We'd have to have two nested loops then.
I'll try to find a way better way of doing this

crates/router/src/core/payments/routing.rs Outdated Show resolved Hide resolved
crates/router/src/core/payments/routing.rs Outdated Show resolved Hide resolved
Comment on lines 1332 to 1336
connector_list = dynamic_routing_algo_ref
.contract_based_routing
.as_ref()
.async_map(|algorithm| {
perform_contract_based_routing(
Copy link
Contributor

Choose a reason for hiding this comment

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

also do we want to go forward with performing contract based routing even if success based routing failed? because I see that you are only logging the error after success based failed and doing unwrap_or to original connector list

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Success routing can only fail here if,
It's set up but call fails, in this case contract won't be activated so the call won't happen.
It's not set up, in which case call won't happen and contract call would take place next.

crates/router/src/core/payments/routing.rs Outdated Show resolved Hide resolved
crates/router/src/core/payments/routing.rs Outdated Show resolved Hide resolved
Comment on lines 1493 to 1504
for info in info_vec {
let mca = db
.find_by_merchant_connector_account_merchant_id_merchant_connector_id(
key_manager_state,
merchant_account.get_id(),
&info.mca_id,
&key_store,
)
.await
.change_context(errors::ApiErrorResponse::MerchantConnectorAccountNotFound {
id: info.mca_id.get_string_repr().to_owned(),
})?;
Copy link
Contributor

Choose a reason for hiding this comment

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

can we do tokio join?

crates/router/src/core/routing.rs Outdated Show resolved Hide resolved
@@ -1419,6 +1463,292 @@ pub async fn success_based_routing_update_configs(
Ok(service_api::ApplicationResponse::Json(new_record))
}

#[cfg(all(feature = "v1", feature = "dynamic_routing"))]
pub async fn contract_based_dynamic_routing_setup(
Copy link
Contributor

Choose a reason for hiding this comment

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

why didn't you make use of existing toggle_specific_dynamic_routing function to activate

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because CR cannot be set to default, we can use that for routing types that can be always be toggled to some default config.

crates/router/src/core/routing/helpers.rs Outdated Show resolved Hide resolved
crates/router/src/core/routing/helpers.rs Outdated Show resolved Hide resolved
crates/router/src/core/routing/helpers.rs Outdated Show resolved Hide resolved
first_contract_based_connector.to_string(),
);

core_metrics::DYNAMIC_CONTRACT_BASED_ROUTING.add(
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this metric be pushed only if payment_status_attribute is charged?

crates/router/src/core/routing/helpers.rs Outdated Show resolved Hide resolved
crates/router/src/core/routing/helpers.rs Outdated Show resolved Hide resolved
Chethan-rao
Chethan-rao previously approved these changes Jan 20, 2025
tsdk02
tsdk02 previously approved these changes Jan 20, 2025
crates/router/src/core/payments/routing.rs Outdated Show resolved Hide resolved
state: &SessionState,
routable_connectors: Vec<api_routing::RoutableConnectorChoice>,
profile_id: &common_utils::id_type::ProfileId,
_dynamic_routing_config_params_interpolator: routing::helpers::DynamicRoutingConfigParamsInterpolator,
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we remove this, if redundant ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Had kept this as we had decided to use the params in the upcoming PRs,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-core Area: Core flows A-routing Area: Routing M-api-contract-changes Metadata: This PR involves API contract changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE] Contract Routing Integration
4 participants