From 3c9ab24959321b04e529df02742a66d4e11edd84 Mon Sep 17 00:00:00 2001 From: Aleksey Kashapov Date: Wed, 31 Jan 2024 17:36:40 +0300 Subject: [PATCH] TD-846: Adds provider fd overrides support (#116) * TD-846: Adds provider fd overrides support * Simplify FD override options 'merge' * Removes obsolete comment * Fixes and merges testcase for pinned and FD overridden routes --- apps/hellgate/test/hg_ct_domain.hrl | 22 ++++++++-- .../test/hg_route_rules_tests_SUITE.erl | 22 +++++++--- apps/routing/src/hg_route.erl | 2 - apps/routing/src/hg_routing.erl | 42 +++++++++++++++---- 4 files changed, 67 insertions(+), 21 deletions(-) diff --git a/apps/hellgate/test/hg_ct_domain.hrl b/apps/hellgate/test/hg_ct_domain.hrl index 334424a..8357ddf 100644 --- a/apps/hellgate/test/hg_ct_domain.hrl +++ b/apps/hellgate/test/hg_ct_domain.hrl @@ -43,6 +43,7 @@ payment_token = ?token_srv(Prv), tokenization_method = Method }). +-define(fd_overrides(Enabled), #domain_RouteFaultDetectorOverrides{enabled = Enabled}). -define(cashrng(Lower, Upper), #domain_CashRange{lower = Lower, upper = Upper}). @@ -152,16 +153,28 @@ ruleset = RuleSetRef }). --define(terminal_obj(Ref, PRef), #domain_TerminalObject{ +-define(terminal_obj(Ref, PRef), ?terminal_obj(Ref, PRef, undefined)). + +-define(terminal_obj(Ref, PRef, FdOverrides), #domain_TerminalObject{ ref = Ref, data = #domain_Terminal{ name = <<"Payment Terminal">>, description = <<"Best terminal">>, - provider_ref = PRef + provider_ref = PRef, + route_fd_overrides = FdOverrides } }). --define(provider(ProvisionTermSet), #domain_Provider{ +-define(provider_obj(Ref, ProvisionTermSet), ?provider_obj(Ref, ProvisionTermSet, undefined)). + +-define(provider_obj(Ref, ProvisionTermSet, FdOverrides), #domain_ProviderObject{ + ref = Ref, + data = ?provider(ProvisionTermSet, FdOverrides) +}). + +-define(provider(ProvisionTermSet), ?provider(ProvisionTermSet, undefined)). + +-define(provider(ProvisionTermSet, FdOverrides), #domain_Provider{ name = <<"Biba">>, description = <<"Payment terminal provider">>, proxy = #domain_Proxy{ @@ -172,7 +185,8 @@ }, abs_account = <<"0987654321">>, accounts = hg_ct_fixture:construct_provider_account_set([?cur(<<"RUB">>)]), - terms = ProvisionTermSet + terms = ProvisionTermSet, + route_fd_overrides = FdOverrides }). -define(payment_terms, #domain_PaymentsProvisionTerms{ diff --git a/apps/hellgate/test/hg_route_rules_tests_SUITE.erl b/apps/hellgate/test/hg_route_rules_tests_SUITE.erl index 542356f..51604f9 100644 --- a/apps/hellgate/test/hg_route_rules_tests_SUITE.erl +++ b/apps/hellgate/test/hg_route_rules_tests_SUITE.erl @@ -86,6 +86,7 @@ init_per_suite(C) -> scoper, bender_client, dmt_client, + party_client, hg_proto, hellgate, {cowboy, CowboySpec} @@ -795,9 +796,9 @@ gather_pinned_route(_C) -> }, ?assert_set_equal( [ - hg_route:new(?prv(1), ?trm(1), 0, 0, Ctx), - hg_route:new(?prv(2), ?trm(2), 0, 0, Pin), - hg_route:new(?prv(3), ?trm(3), 0, 0, Pin) + hg_route:new(?prv(1), ?trm(1), 0, 0, Ctx, ?fd_overrides(undefined)), + hg_route:new(?prv(2), ?trm(2), 0, 0, Pin, ?fd_overrides(true)), + hg_route:new(?prv(3), ?trm(3), 0, 0, Pin, ?fd_overrides(false)) ], Routes ). @@ -883,9 +884,18 @@ routing_with_risk_score_fixture(Domain, AddRiskScore) -> construct_domain_fixture() -> #{ - {terminal, ?trm(1)} => {terminal, ?terminal_obj(?trm(1), ?prv(1))}, - {terminal, ?trm(2)} => {terminal, ?terminal_obj(?trm(2), ?prv(2))}, - {terminal, ?trm(3)} => {terminal, ?terminal_obj(?trm(3), ?prv(3))}, + {provider, ?prv(1)} => {provider, ?provider_obj(?prv(1), #domain_ProvisionTermSet{}, undefined)}, + {provider, ?prv(2)} => {provider, ?provider_obj(?prv(2), #domain_ProvisionTermSet{}, ?fd_overrides(undefined))}, + {provider, ?prv(3)} => {provider, ?provider_obj(?prv(3), #domain_ProvisionTermSet{}, ?fd_overrides(true))}, + {provider, ?prv(4)} => {provider, ?provider_obj(?prv(4), #domain_ProvisionTermSet{})}, + {provider, ?prv(5)} => {provider, ?provider_obj(?prv(5), #domain_ProvisionTermSet{})}, + {provider, ?prv(6)} => {provider, ?provider_obj(?prv(6), #domain_ProvisionTermSet{})}, + {provider, ?prv(7)} => {provider, ?provider_obj(?prv(7), #domain_ProvisionTermSet{})}, + {provider, ?prv(11)} => {provider, ?provider_obj(?prv(11), #domain_ProvisionTermSet{})}, + {provider, ?prv(12)} => {provider, ?provider_obj(?prv(12), #domain_ProvisionTermSet{})}, + {terminal, ?trm(1)} => {terminal, ?terminal_obj(?trm(1), ?prv(1), undefined)}, + {terminal, ?trm(2)} => {terminal, ?terminal_obj(?trm(2), ?prv(2), ?fd_overrides(true))}, + {terminal, ?trm(3)} => {terminal, ?terminal_obj(?trm(3), ?prv(3), ?fd_overrides(false))}, {terminal, ?trm(4)} => {terminal, ?terminal_obj(?trm(4), ?prv(4))}, {terminal, ?trm(5)} => {terminal, ?terminal_obj(?trm(5), ?prv(5))}, {terminal, ?trm(6)} => {terminal, ?terminal_obj(?trm(6), ?prv(6))}, diff --git a/apps/routing/src/hg_route.erl b/apps/routing/src/hg_route.erl index 582bdb6..cb9e37a 100644 --- a/apps/routing/src/hg_route.erl +++ b/apps/routing/src/hg_route.erl @@ -79,8 +79,6 @@ new(ProviderRef, TerminalRef, Weight, Priority, Pin) -> new(ProviderRef, TerminalRef, Weight, Priority, Pin, #domain_RouteFaultDetectorOverrides{}). -spec new(provider_ref(), terminal_ref(), integer(), integer(), pin(), fd_overrides() | undefined) -> t(). -new(ProviderRef, TerminalRef, Weight, Priority, Pin, undefined) -> - new(ProviderRef, TerminalRef, Weight, Priority, Pin, #domain_RouteFaultDetectorOverrides{}); new(ProviderRef, TerminalRef, Weight, Priority, Pin, FdOverrides) -> #route{ provider_ref = ProviderRef, diff --git a/apps/routing/src/hg_routing.erl b/apps/routing/src/hg_routing.erl index 549bcc8..dcebb56 100644 --- a/apps/routing/src/hg_routing.erl +++ b/apps/routing/src/hg_routing.erl @@ -30,6 +30,8 @@ -define(rejected(Reason), {rejected, Reason}). +-define(fd_overrides(Enabled), #domain_RouteFaultDetectorOverrides{enabled = Enabled}). + -type fd_service_stats() :: fd_proto_fault_detector_thrift:'ServiceStatistics'(). -type terminal_priority_rating() :: integer(). @@ -204,13 +206,7 @@ collect_routes(Predestination, Candidates, VS, Revision, Ctx) -> weight = Weight, pin = Pin } = Candidate, - % Looks like overhead, we got Terminal only for provider_ref. Maybe - % we can remove provider_ref from hg_route:t(). - % https://github.com/rbkmoney/hellgate/pull/583#discussion_r682745123 - #domain_Terminal{ - provider_ref = ProviderRef, - route_fd_overrides = FdOverrides - } = hg_domain:get(Revision, {terminal, TerminalRef}), + {ProviderRef, FdOverrides} = get_provider_fd_overrides(Revision, TerminalRef), GatheredPinInfo = gather_pin_info(Pin, Ctx), try true = acceptable_terminal(Predestination, ProviderRef, TerminalRef, VS, Revision), @@ -227,6 +223,25 @@ collect_routes(Predestination, Candidates, VS, Revision, Ctx) -> Candidates ). +get_provider_fd_overrides(Revision, TerminalRef) -> + % Looks like overhead, we got Terminal only for provider_ref. Maybe + % we can remove provider_ref from hg_route:t(). + % https://github.com/rbkmoney/hellgate/pull/583#discussion_r682745123 + #domain_Terminal{provider_ref = ProviderRef, route_fd_overrides = TrmFdOverrides} = + hg_domain:get(Revision, {terminal, TerminalRef}), + #domain_Provider{route_fd_overrides = PrvFdOverrides} = + hg_domain:get(Revision, {provider, ProviderRef}), + %% TODO Consider moving this logic to party-management before (or after) + %% internal route structure refactoring. + {ProviderRef, merge_fd_overrides(PrvFdOverrides, TrmFdOverrides)}. + +merge_fd_overrides(_A, B = ?fd_overrides(Enabled)) when Enabled =/= undefined -> + B; +merge_fd_overrides(A = ?fd_overrides(Enabled), _B) when Enabled =/= undefined -> + A; +merge_fd_overrides(_A, _B) -> + ?fd_overrides(undefined). + gather_pin_info(undefined, _Ctx) -> #{}; gather_pin_info(#domain_RoutingPin{features = Features}, Ctx) -> @@ -488,7 +503,7 @@ get_provider_status(Route, FDStats) -> ConversionStatus = get_provider_conversion_status(FdOverrides, ConversionServiceID, FDStats), {AvailabilityStatus, ConversionStatus}. -get_adapter_availability_status(#domain_RouteFaultDetectorOverrides{enabled = true}, _FDID, _Stats) -> +get_adapter_availability_status(?fd_overrides(true), _FDID, _Stats) -> %% ignore fd statistic if set override {alive, 0.0}; get_adapter_availability_status(_, FDID, Stats) -> @@ -503,7 +518,7 @@ get_adapter_availability_status(_, FDID, Stats) -> {alive, 0.0} end. -get_provider_conversion_status(#domain_RouteFaultDetectorOverrides{enabled = true}, _FDID, _Stats) -> +get_provider_conversion_status(?fd_overrides(true), _FDID, _Stats) -> %% ignore fd statistic if set override {normal, 0.0}; get_provider_conversion_status(_, FDID, Stats) -> @@ -922,4 +937,13 @@ prefer_weight_over_conversion_test() -> FailRatedRoutes = lists:zip(Routes, ProviderStatuses), {Route2, _Meta} = choose_rated_route(FailRatedRoutes). +-spec merge_fd_overrides_test_() -> _. +merge_fd_overrides_test_() -> + [ + ?_assertEqual(?fd_overrides(undefined), merge_fd_overrides(undefined, ?fd_overrides(undefined))), + ?_assertEqual(?fd_overrides(true), merge_fd_overrides(?fd_overrides(true), undefined)), + ?_assertEqual(?fd_overrides(true), merge_fd_overrides(?fd_overrides(true), ?fd_overrides(undefined))), + ?_assertEqual(?fd_overrides(false), merge_fd_overrides(?fd_overrides(true), ?fd_overrides(false))) + ]. + -endif.