Show server status

This commit is contained in:
Smaug123
2022-10-27 12:55:38 +01:00
parent 8441e75fb2
commit 110a04c4f2
2 changed files with 26 additions and 5 deletions

View File

@@ -164,9 +164,15 @@ type private ServerSpecialisation =
| Candidate of CandidateState
type ServerStatus =
| Leader
| Leader of int<Term>
| Follower
| Candidate
| Candidate of int<Term>
override this.ToString () =
match this with
| Leader term -> sprintf "Leader in term %i" term
| Candidate term -> sprintf "Candidate in term %i" term
| Follower -> "Follower"
type private ServerAction<'a> =
| BeginElection
@@ -462,8 +468,8 @@ type Server<'a>
member this.State =
match currentType with
| ServerSpecialisation.Leader _ -> ServerStatus.Leader
| ServerSpecialisation.Candidate _ -> ServerStatus.Candidate
| ServerSpecialisation.Leader _ -> ServerStatus.Leader persistentState.CurrentTerm
| ServerSpecialisation.Candidate _ -> ServerStatus.Candidate persistentState.CurrentTerm
| ServerSpecialisation.Follower -> ServerStatus.Follower
type Cluster<'a> =
@@ -479,6 +485,10 @@ type Cluster<'a> =
this.Servers.[i / 1<ServerId>].TriggerTimeout ()
this.Servers.[i / 1<ServerId>].Sync ()
member this.State (i : int<ServerId>) : ServerStatus = this.Servers.[i / 1<ServerId>].State
member this.ClusterSize : int = this.Servers.Length
type Network<'a> =
internal
{

View File

@@ -16,6 +16,10 @@ module Program =
if not wroteAnything then
printfn "<No messages in network>"
let printClusterState<'a> (cluster : Cluster<'a>) : unit =
for i in 0 .. cluster.ClusterSize - 1 do
printfn "Server %i: %O" i (cluster.State (i * 1<ServerId>))
let getMessage (clusterSize : int) (s : string) : (int<ServerId> * int) option =
match s.Split ',' with
| [| serverId ; messageId |] ->
@@ -59,7 +63,13 @@ module Program =
let rec getAction (clusterSize : int) =
printf "Enter action. Trigger [t]imeout <server id>, or allow [m]essage <server id, message id>: "
let s = Console.ReadLine().ToUpperInvariant ()
let s =
let rec go () =
let s = Console.ReadLine().ToUpperInvariant ()
if String.IsNullOrEmpty s then go () else s
go ()
match s.[0] with
| 'T' ->
@@ -81,6 +91,7 @@ module Program =
while true do
printNetworkState network
printClusterState cluster
let action = getAction clusterSize