Skip to content

Commit ef25bdc

Browse files
authored
OPS-465: Fix pinned routes weight (#132)
* OPS-465: Fix pinned routes weight * Get rid of useless test
1 parent 1d6d7a6 commit ef25bdc

File tree

2 files changed

+74
-51
lines changed

2 files changed

+74
-51
lines changed

apps/hellgate/test/hg_route_rules_tests_SUITE.erl

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
-export([routes_selected_for_low_risk_score/1]).
3131
-export([terminal_priority_for_shop/1]).
3232
-export([gather_pinned_route/1]).
33-
-export([choose_pinned_route/1]).
3433
-export([choose_route_w_override/1]).
3534

3635
-define(PROVIDER_MIN_ALLOWED, ?cash(1000, <<"RUB">>)).
@@ -73,7 +72,6 @@ groups() ->
7372
terminal_priority_for_shop,
7473

7574
gather_pinned_route,
76-
choose_pinned_route,
7775
choose_route_w_override
7876
]}
7977
].
@@ -796,34 +794,6 @@ gather_pinned_route(_C) ->
796794
Routes
797795
).
798796

799-
-spec choose_pinned_route(config()) -> test_return().
800-
choose_pinned_route(_C) ->
801-
Currency = ?cur(<<"RUB">>),
802-
PaymentTool = {payment_terminal, #domain_PaymentTerminal{payment_service = ?pmt_srv(<<"euroset-ref">>)}},
803-
Pin1 = #{
804-
currency => Currency,
805-
payment_tool => PaymentTool,
806-
client_ip => undefined
807-
},
808-
Pin2 = #{
809-
currency => Currency,
810-
payment_tool => PaymentTool
811-
},
812-
Pin3 = #{
813-
currency => Currency,
814-
payment_tool => PaymentTool
815-
},
816-
Route1 = hg_route:new(?prv(1), ?trm(1), 0, 0, Pin1),
817-
Route2 = hg_route:new(?prv(1), ?trm(1), 0, 0, Pin2),
818-
Route3 = hg_route:new(?prv(1), ?trm(1), 0, 0, Pin3),
819-
Routes = [
820-
Route1,
821-
Route2,
822-
Route3
823-
],
824-
[ChosenRoute | _] = lists:sort(Routes),
825-
{ChosenRoute, _} = hg_routing:choose_route(Routes).
826-
827797
-spec choose_route_w_override(config()) -> test_return().
828798
choose_route_w_override(_C) ->
829799
%% without overrides

apps/routing/src/hg_routing.erl

Lines changed: 74 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -347,23 +347,42 @@ find_best_routes([First | Rest]) ->
347347
select_better_route({LeftScore, _} = Left, {RightScore, _} = Right) ->
348348
LeftPin = LeftScore#domain_PaymentRouteScores.route_pin,
349349
RightPin = RightScore#domain_PaymentRouteScores.route_pin,
350-
case {LeftPin, RightPin} of
351-
_ when LeftPin /= ?ZERO, RightPin /= ?ZERO ->
352-
select_better_pinned_route(Left, Right);
353-
_ ->
354-
select_better_regular_route(Left, Right)
355-
end.
350+
Res =
351+
case {LeftPin, RightPin} of
352+
_ when LeftPin /= ?ZERO, RightPin /= ?ZERO, LeftPin == RightPin ->
353+
select_better_pinned_route(Left, Right);
354+
_ ->
355+
select_better_regular_route(Left, Right)
356+
end,
357+
Res.
356358

357-
select_better_pinned_route({LeftScore, _Route1} = Left, {RightScore, _Route2} = Right) ->
358-
case max(LeftScore, RightScore) of
359-
LeftScore ->
359+
select_better_pinned_route({LeftScore0, _Route1} = Left, {RightScore0, _Route2} = Right) ->
360+
LeftScore1 = LeftScore0#domain_PaymentRouteScores{
361+
random_condition = 0
362+
},
363+
RightScore1 = RightScore0#domain_PaymentRouteScores{
364+
random_condition = 0
365+
},
366+
case max(LeftScore1, RightScore1) of
367+
LeftScore1 ->
360368
Left;
361-
RightScore ->
369+
RightScore1 ->
362370
Right
363371
end.
364372

365-
select_better_regular_route(Left, Right) ->
366-
max(Left, Right).
373+
select_better_regular_route({LeftScore0, LRoute}, {RightScore0, RRoute}) ->
374+
LeftScore1 = LeftScore0#domain_PaymentRouteScores{
375+
route_pin = 0
376+
},
377+
RightScore1 = RightScore0#domain_PaymentRouteScores{
378+
route_pin = 0
379+
},
380+
case max({LeftScore1, LRoute}, {RightScore1, RRoute}) of
381+
{LeftScore1, LRoute} ->
382+
{LeftScore0, LRoute};
383+
{RightScore1, RRoute} ->
384+
{RightScore0, RRoute}
385+
end.
367386

368387
select_better_route_ideal(Left, Right) ->
369388
IdealLeft = set_ideal_score(Left),
@@ -525,20 +544,18 @@ score_route_ext({Route, ProviderStatus}) ->
525544
score_route(Route) ->
526545
PriorityRate = hg_route:priority(Route),
527546
Pin = hg_route:pin(Route),
528-
{RandomCondition, PinHash} =
529-
case Pin of
530-
#{} when map_size(Pin) == 0 ->
531-
{hg_route:weight(Route), ?ZERO};
532-
_ ->
533-
{?ZERO, erlang:phash2(Pin)}
534-
end,
535547
#domain_PaymentRouteScores{
536548
terminal_priority_rating = PriorityRate,
537-
route_pin = PinHash,
538-
random_condition = RandomCondition,
549+
route_pin = get_pin_hash(Pin),
550+
random_condition = hg_route:weight(Route),
539551
blacklist_condition = 0
540552
}.
541553

554+
get_pin_hash(Pin) when map_size(Pin) == 0 ->
555+
?ZERO;
556+
get_pin_hash(Pin) ->
557+
erlang:phash2(Pin).
558+
542559
get_availability_score({alive, FailRate}) -> {1, 1.0 - FailRate};
543560
get_availability_score({dead, FailRate}) -> {0, 1.0 - FailRate}.
544561

@@ -868,6 +885,42 @@ pin_random_test() ->
868885
lists:seq(0, 1000)
869886
).
870887

888+
-spec pin_weight_test() -> _.
889+
pin_weight_test() ->
890+
Pin0 = #{
891+
email => <<"example@mail.com">>
892+
},
893+
Pin1 = #{
894+
email => <<"example2@mail.com">>
895+
},
896+
Scores = {{alive, 0.0}, {normal, 0.0}},
897+
Route1 = {hg_route:new(?prv(1), ?trm(1), 50, 1, Pin0), Scores},
898+
Route2 = {hg_route:new(?prv(2), ?trm(2), 50, 1, Pin1), Scores},
899+
{_, DiffTimes} = lists:foldl(
900+
fun(_I, {Acc, Iter}) ->
901+
BalancedRoutes = balance_routes([Route1, Route2]),
902+
ScoredRoutes = score_routes(BalancedRoutes),
903+
{{_, ChosenScoredRoute}, _IdealRoute} = find_best_routes(ScoredRoutes),
904+
case Acc of
905+
undefined ->
906+
{ChosenScoredRoute, Iter};
907+
_ ->
908+
ChosenTerminal = hg_route:terminal_ref(ChosenScoredRoute),
909+
case hg_route:terminal_ref(Acc) of
910+
ChosenTerminal ->
911+
{Acc, Iter};
912+
_ ->
913+
{Acc, Iter + 1}
914+
end
915+
end
916+
end,
917+
{undefined, 0},
918+
lists:seq(0, 1000)
919+
),
920+
?assertNotEqual(0, DiffTimes),
921+
?assertEqual(true, DiffTimes > 300),
922+
?assertEqual(true, DiffTimes < 700).
923+
871924
-spec balance_routes_test_() -> [testcase()].
872925
balance_routes_test_() ->
873926
Status = {{alive, 0.0}, {normal, 0.0}},

0 commit comments

Comments
 (0)