Skip to content

Commit e258ff9

Browse files
authored
Add ties to the set of selections to use (#127)
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 5d107ee commit e258ff9

1 file changed

Lines changed: 37 additions & 5 deletions

File tree

src/aae_exchange.erl

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,7 @@ intersect_ids(IDs0, IDs1) ->
993993
%% will wastefully correct the same data - so randomly chose one of the better
994994
%% lists
995995
select_ids(IDList, MaxOutput, StateName, ExchangeID, LogLevels)
996-
when length(IDList) > MaxOutput ->
996+
when length(IDList) > MaxOutput ->
997997
IDList0 = lists:sort(IDList),
998998
aae_util:log(
999999
ex005,
@@ -1010,8 +1010,21 @@ select_ids(IDList, MaxOutput, StateName, ExchangeID, LogLevels)
10101010
end,
10111011
{_EndIdx, SpaceIdxL} =
10121012
lists:foldl(FoldFun, {1, []}, lists:zip(IDList1, IDList2)),
1013-
Selections =
1014-
lists:sublist(lists:sort(SpaceIdxL), MaxOutput),
1013+
Selections =
1014+
case length(SpaceIdxL) of
1015+
L when L > MaxOutput ->
1016+
{MaxResults, TailResults} =
1017+
lists:split(MaxOutput, lists:sort(SpaceIdxL)),
1018+
{LastChosenScore, _LastChosenIdx} = lists:last(MaxResults),
1019+
TiedSelections =
1020+
lists:takewhile(
1021+
fun({S, _I}) -> S == LastChosenScore end,
1022+
TailResults
1023+
),
1024+
MaxResults ++ TiedSelections;
1025+
_ ->
1026+
lists:sort(SpaceIdxL)
1027+
end,
10151028
{_ChosenSpace, ChosenIdx} =
10161029
lists:nth(rand:uniform(length(Selections)), Selections),
10171030
lists:sublist(IDList0, ChosenIdx, MaxOutput);
@@ -1026,8 +1039,8 @@ jitter_pause(Timeout) ->
10261039
rand:uniform(Timeout) + Timeout div 2.
10271040

10281041

1029-
-spec reset({pos_integer(), pos_integer()})
1030-
-> {non_neg_integer(), pos_integer()}.
1042+
-spec reset(
1043+
{pos_integer(), pos_integer()}) -> {non_neg_integer(), pos_integer()}.
10311044
%% @doc
10321045
%% Rest the count back to 0
10331046
reset({Target, Target}) -> {0, Target}.
@@ -1092,6 +1105,25 @@ select_best_id_rand_test() ->
10921105
?assertMatch({true, true, true},
10931106
lists:foldl(F, {false, false, false}, lists:seq(1, 1000))).
10941107

1108+
1109+
select_id_withties_test() ->
1110+
L0 = lists:seq(1, 1000),
1111+
RL = select_ids(L0, 32, root_confirm, "t0.0", undefined),
1112+
?assertMatch(32, length(RL)),
1113+
Select100 =
1114+
lists:map(
1115+
fun(_I) ->
1116+
hd(select_ids(L0, 32, root_confirm, "t0.1", undefined))
1117+
end,
1118+
lists:seq(1, 100)
1119+
),
1120+
% Expect to see heads evenly distributed
1121+
% - check we see both above and below 500
1122+
Above500 = lists:filter(fun(H) -> H > 500 end, Select100),
1123+
Below500 = lists:filter(fun(H) -> H < 500 end, Select100),
1124+
?assert(length(Above500) > 0),
1125+
?assert(length(Below500) > 0).
1126+
10951127
compare_clocks_test() ->
10961128
KV1 = {<<"B1">>, <<"K1">>, [{a, 1}]},
10971129
KV2 = {<<"B1">>, <<"K2">>, [{b, 1}]},

0 commit comments

Comments
 (0)