-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This reverts commit d7e3030.
- Loading branch information
Alex Valiushko
committed
May 25, 2023
1 parent
ac84b6f
commit 9936265
Showing
6 changed files
with
342 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
%% This Source Code Form is subject to the terms of the Mozilla Public | ||
%% | ||
%% Copyright (c) 2017-2022 VMware, Inc. or its affiliates. All rights reserved. | ||
%% | ||
-module(ra_voter). | ||
|
||
-export([ | ||
new_nonvoter/1, | ||
status/1, | ||
status/2, | ||
maybe_promote/3 | ||
]). | ||
|
||
-define(DEFAULT_MAX_ROUNDS, 4). | ||
|
||
new_nonvoter(State) -> | ||
Target = maps:get(commit_index, State), | ||
{no, #{round => 0, target => Target , ts => os:system_time(millisecond)}}. | ||
|
||
maybe_promote(PeerID, | ||
#{commit_index := CI, cluster := Cluster} = _State, | ||
Effects) -> | ||
#{PeerID := #{match_index := MI, voter := OldStatus} = _Peer} = Cluster, | ||
case evaluate_voter(OldStatus, MI, CI) of | ||
OldStatus -> | ||
Effects; | ||
Change -> | ||
[{next_event, | ||
{command, {'$ra_join', | ||
#{ts => os:system_time(millisecond)}, | ||
PeerID, | ||
noreply}}} | | ||
Effects] | ||
end. | ||
|
||
evaluate_voter({no, #{round := Round, target := Target , ts := RoundStart}}, MI, CI) | ||
when MI >= Target -> | ||
AtenPollInt = application:get_env(aten, poll_interval, 1000), | ||
Now = os:system_time(millisecond), | ||
case (Now - RoundStart) =< AtenPollInt of | ||
true -> | ||
yes; | ||
false when Round > ?DEFAULT_MAX_ROUNDS -> | ||
{no, permanent}; | ||
false -> | ||
{no, #{round => Round+1, target => CI, ts => Now}} | ||
end; | ||
evaluate_voter(Permanent, _, _) -> | ||
Permanent. | ||
|
||
status(#{cluster := Cluster} = State) -> | ||
Id = ra_server:id(State), | ||
status(Cluster, Id). | ||
|
||
status(Cluster, PeerId) -> | ||
case maps:get(PeerId, Cluster, undefined) of | ||
undefined -> | ||
throw(not_a_cluster_member); | ||
Peer -> | ||
maps:get(voter, Peer, yes) | ||
end. |
Oops, something went wrong.