Skip to content

Commit 4f8c969

Browse files
authored
Add ties to the set of selections to use (#126)
Will randomly select from the top MaxResults set of ID clusters, and any tied with the sparsest set within the MaxResults. This means when there is a contiguous set of IDs (e.g. when doing root_confirm with a fully broken root), any set of IDs will be chosen, not just sets within the first MaxResults. This ensures a better spread of selections across the tree, so that the mismatched_segments will more accurately reflect the overall state of the tree
1 parent f4376d4 commit 4f8c969

1 file changed

Lines changed: 43 additions & 9 deletions

File tree

src/aae_exchange.erl

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -970,12 +970,14 @@ intersect_ids(IDs0, IDs1) ->
970970
%% will wastefully correct the same data - so randomly chose one of the better
971971
%% lists
972972
select_ids(IDList, MaxOutput, StateName, ExchangeID, LogLevels)
973-
when length(IDList) > MaxOutput ->
973+
when length(IDList) > MaxOutput ->
974974
IDList0 = lists:sort(IDList),
975-
aae_util:log("EX005",
976-
[ExchangeID, length(IDList0), StateName],
977-
logs(),
978-
LogLevels),
975+
aae_util:log(
976+
"EX005",
977+
[ExchangeID, length(IDList0), StateName],
978+
logs(),
979+
LogLevels
980+
),
979981
IDList1 =
980982
lists:sublist(IDList0, 1 + length(IDList0) - MaxOutput),
981983
IDList2 =
@@ -986,8 +988,21 @@ select_ids(IDList, MaxOutput, StateName, ExchangeID, LogLevels)
986988
end,
987989
{_EndIdx, SpaceIdxL} =
988990
lists:foldl(FoldFun, {1, []}, lists:zip(IDList1, IDList2)),
989-
Selections =
990-
lists:sublist(lists:sort(SpaceIdxL), MaxOutput),
991+
Selections =
992+
case length(SpaceIdxL) of
993+
L when L > MaxOutput ->
994+
{MaxResults, TailResults} =
995+
lists:split(MaxOutput, lists:sort(SpaceIdxL)),
996+
{LastChosenScore, _LastChosenIdx} = lists:last(MaxResults),
997+
TiedSelections =
998+
lists:takewhile(
999+
fun({S, _I}) -> S == LastChosenScore end,
1000+
TailResults
1001+
),
1002+
MaxResults ++ TiedSelections;
1003+
_ ->
1004+
lists:sort(SpaceIdxL)
1005+
end,
9911006
{_ChosenSpace, ChosenIdx} =
9921007
lists:nth(leveled_rand:uniform(length(Selections)), Selections),
9931008
lists:sublist(IDList0, ChosenIdx, MaxOutput);
@@ -1002,8 +1017,8 @@ jitter_pause(Timeout) ->
10021017
leveled_rand:uniform(Timeout) + Timeout div 2.
10031018

10041019

1005-
-spec reset({pos_integer(), pos_integer()})
1006-
-> {non_neg_integer(), pos_integer()}.
1020+
-spec reset(
1021+
{pos_integer(), pos_integer()}) -> {non_neg_integer(), pos_integer()}.
10071022
%% @doc
10081023
%% Rest the count back to 0
10091024
reset({Target, Target}) -> {0, Target}.
@@ -1111,6 +1126,25 @@ select_best_id_rand_test() ->
11111126
?assertMatch({true, true, true},
11121127
lists:foldl(F, {false, false, false}, lists:seq(1, 1000))).
11131128

1129+
1130+
select_id_withties_test() ->
1131+
L0 = lists:seq(1, 1000),
1132+
RL = select_ids(L0, 32, root_confirm, "t0.0", undefined),
1133+
?assertMatch(32, length(RL)),
1134+
Select100 =
1135+
lists:map(
1136+
fun(_I) ->
1137+
hd(select_ids(L0, 32, root_confirm, "t0.1", undefined))
1138+
end,
1139+
lists:seq(1, 100)
1140+
),
1141+
% Expect to see heads evenly distributed
1142+
% - check we see both above and below 500
1143+
Above500 = lists:filter(fun(H) -> H > 500 end, Select100),
1144+
Below500 = lists:filter(fun(H) -> H < 500 end, Select100),
1145+
?assert(length(Above500) > 0),
1146+
?assert(length(Below500) > 0).
1147+
11141148
compare_clocks_test() ->
11151149
KV1 = {<<"B1">>, <<"K1">>, [{a, 1}]},
11161150
KV2 = {<<"B1">>, <<"K2">>, [{b, 1}]},

0 commit comments

Comments
 (0)