This repository was archived by the owner on Sep 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathutil.ml
More file actions
68 lines (59 loc) · 2 KB
/
Copy pathutil.ml
File metadata and controls
68 lines (59 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
open Common
open Rpcs
open Io
open State
type eventsig = State.t -> Global.t -> State.t option * rpc Io.output list * Global.t
type mode_mini = F | C | L
type term_checker =
| Invalid
| Same
| Higher
(* check term of incoming packet relative to local term *)
let check_terms incoming_term (state:State.t) =
if incoming_term > state.term then Higher
else if incoming_term < state.term then Invalid
else Same
let cancel_timers (state:State.t) =
match state.mode with
| Follower _ -> [CancelTimeout Heartbeat]
| Candidate _ -> [CancelTimeout Election]
| Leader _ -> [CancelTimeout Leadership]
let construct_heartbeat (state:State.t) =
let (min,max) = state.config.election_timeout in
let timeout = Numbergen.uniform min max in
SetTimeout (to_span timeout,Heartbeat)
let reconstruct_heartbeat (state:State.t) =
let (min,max) = state.config.election_timeout in
let timeout = Numbergen.uniform min max in
ResetTimeout (to_span timeout,Heartbeat)
let step_down term incoming_mode (state:State.t) (global:Global.t) =
(* TODO: fix me *)
match term=state.term with
| true ->
let global = (
match state.mode, incoming_mode with
| Candidate _ , L ->
global
|> Global.update `ELE_DOWN
|> Global.update (`FOLLOW term)
| _ -> global) in
(Some {state with mode=State.follower},
(construct_heartbeat state)::(cancel_timers state), global)
| false ->
let global = (
match state.mode with
| Follower _ -> global
| Leader _ -> global
| Candidate c -> Global.update `ELE_DOWN global) in
(Some {state with term=term; mode=State.follower},
(construct_heartbeat state)::(cancel_timers state),
Global.update (`FOLLOW term) global)
let rec get_commit_index curr indexes =
let nodes = (List.length indexes) +1 in
List.map (fun (id,next,matched) -> matched) indexes
|> List.filter (fun m -> m>curr)
|> List.length
|> fun n ->
if (n+1)*2 > nodes
then get_commit_index (curr+1) indexes
else curr