diff options
| author | Paul Buetow <paul@buetow.org> | 2023-06-13 00:33:14 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2023-06-13 00:33:14 +0300 |
| commit | 3096e71218f58588b8be2d11280b2b99383e1461 (patch) | |
| tree | 92b950b6e220025e244fb50154c7dfa23f6501b1 | |
| parent | f9aaa6b3838c336428ed1f2df30f3731e590c043 (diff) | |
refacto quorum
| -rw-r--r-- | cmd/gorum/main.go | 2 | ||||
| -rw-r--r-- | internal/quorum/quorum.go | 17 | ||||
| -rw-r--r-- | internal/quorum/quorum_test.go | 79 | ||||
| -rw-r--r-- | internal/vote/vote_test.go | 22 |
4 files changed, 60 insertions, 60 deletions
diff --git a/cmd/gorum/main.go b/cmd/gorum/main.go index 0573f3a..da6574c 100644 --- a/cmd/gorum/main.go +++ b/cmd/gorum/main.go @@ -29,7 +29,7 @@ func main() { defer cancel() if err := internal.Run(ctx, *configFile); err != nil { - panic(err) + log.Fatal(err) } <-ctx.Done() diff --git a/internal/quorum/quorum.go b/internal/quorum/quorum.go index febc19e..e3f0eeb 100644 --- a/internal/quorum/quorum.go +++ b/internal/quorum/quorum.go @@ -42,13 +42,13 @@ func (quo Quorum) Start(ctx context.Context) <-chan vote.Vote { for { select { case <-time.After(vote.Expiry): - if newVote, changed := quo.makeVote(); changed { + if newVote, changed := quo.makeMyVote(); changed { quo.score() ch <- newVote } case vote := <-quo.voteCh: quo.vote(vote) - if newVote, changed := quo.makeVote(); changed { + if newVote, changed := quo.makeMyVote(); changed { ch <- newVote } quo.score() @@ -103,14 +103,19 @@ func (quo Quorum) score() (scores []Score) { return } -func (quo *Quorum) makeVote() (vote.Vote, bool) { +func (quo *Quorum) makeMyVote() (vote.Vote, bool) { newVote, err := quo.pruneVotes() if err != nil { log.Println("quorum:", err) - return newVote, false + return quo.myVote, false } - defer func() { quo.myVote = newVote }() - return newVote, !quo.myVote.Equal(newVote) + + if quo.myVote.Equal(newVote) { + return quo.myVote, false + } + + quo.myVote = newVote + return quo.myVote, true } func (quo Quorum) pruneVotes() (vote.Vote, error) { diff --git a/internal/quorum/quorum_test.go b/internal/quorum/quorum_test.go index 11372ce..ad78938 100644 --- a/internal/quorum/quorum_test.go +++ b/internal/quorum/quorum_test.go @@ -8,26 +8,32 @@ import ( "codeberg.org/snonux/gorum/internal/vote" ) +var inOneHour = time.Now().Add(1 * time.Hour) + func TestScore(t *testing.T) { var ( conf = config.Config{Nodes: []string{"foo:1234", "bar:4321", "baz:3444"}} quo = New(conf) ) - vote1 := vote.New(conf, "foo foo bar\n") - vote1.ExpiresAt = time.Now().Add(1 * time.Hour) + vote1, _ := vote.New([]string{"foo", "bar"}) + vote1.FromID = "foo" + vote1.ExpiresAt = inOneHour quo.vote(vote1) - vote2 := vote.New(conf, "bar bar baz\n") - vote2.ExpiresAt = time.Now().Add(1 * time.Hour) + vote2, _ := vote.New([]string{"bar", "baz"}) + vote2.FromID = "bar" + vote2.ExpiresAt = inOneHour quo.vote(vote2) - vote3_dup := vote.New(conf, "bar bar baz\n") - vote3_dup.ExpiresAt = time.Now().Add(1 * time.Hour) + vote3_dup, _ := vote.New([]string{"bar", "baz"}) + vote3_dup.FromID = "bar" + vote3_dup.ExpiresAt = inOneHour quo.vote(vote3_dup) - vote4 := vote.New(conf, "baz foo bar baz\n") - vote4.ExpiresAt = time.Now().Add(1 * time.Hour) + vote4, _ := vote.New([]string{"foo", "bar", "baz"}) + vote4.FromID = "baz" + vote4.ExpiresAt = inOneHour quo.vote(vote4) scores := quo.score() @@ -41,17 +47,20 @@ func TestScore(t *testing.T) { } func TestTieScore(t *testing.T) { - addVotes := func(conf config.Config, quo Quorum) { - vote1 := vote.New(conf, "foo foo bar baz\n") - vote1.ExpiresAt = time.Now().Add(1 * time.Hour) + addVotes := func(quo Quorum) { + vote1, _ := vote.New([]string{"foo", "bar", "baz"}) + vote1.FromID = "foo" + vote1.ExpiresAt = inOneHour quo.vote(vote1) - vote2 := vote.New(conf, "bar foo bar baz\n") - vote2.ExpiresAt = time.Now().Add(1 * time.Hour) + vote2, _ := vote.New([]string{"foo", "bar", "baz"}) + vote2.FromID = "bar" + vote2.ExpiresAt = inOneHour quo.vote(vote2) - vote3 := vote.New(conf, "baz foo bar baz\n") - vote3.ExpiresAt = time.Now().Add(1 * time.Hour) + vote3, _ := vote.New([]string{"foo", "bar", "baz"}) + vote3.FromID = "baz" + vote3.ExpiresAt = inOneHour quo.vote(vote3) } @@ -62,7 +71,7 @@ func TestTieScore(t *testing.T) { quo = New(conf) ) - addVotes(conf, quo) + addVotes(quo) scores := quo.score() if len(scores) != 3 { @@ -85,7 +94,7 @@ func TestTieScore(t *testing.T) { quo = New(conf) ) - addVotes(conf, quo) + addVotes(quo) scores := quo.score() if len(scores) != 3 { @@ -104,15 +113,17 @@ func TestTieScore(t *testing.T) { func TestExpire(t *testing.T) { var ( - conf = config.Config{Nodes: []string{"foo:1234", "bay:4321"}} + conf = config.Config{Nodes: []string{"foo:1234", "bar:4321", "bay:2212"}} quo = New(conf) ) - vote1 := vote.New(conf, " foo bar baz bay\n") - vote1.ExpiresAt = time.Now().Add(1 * time.Hour) + vote1, _ := vote.New([]string{"bar", "baz", "bay"}) + vote1.FromID = "foo" + vote1.ExpiresAt = inOneHour quo.vote(vote1) - vote2 := vote.New(conf, " bay foo bar baz\n") + vote2, _ := vote.New([]string{"foo", "bar", "baz"}) + vote2.FromID = "bar" vote2.ExpiresAt = time.Now() quo.vote(vote2) @@ -120,17 +131,17 @@ func TestExpire(t *testing.T) { t.Errorf("Expected to have two votes before expiry: %v", quo) } - liveNodes := quo.pruneVotes() + newVote, _ := quo.pruneVotes() if len(quo.votes) != 1 { t.Errorf("Expected to have one vote after expiry: %v", quo) } - if len(liveNodes) != 1 { + if len(newVote.IDs) != 1 { t.Errorf("Expected to have one live node after expiry: %v", quo) } - if liveNodes[0] != "foo" { - t.Errorf("Expected 'foo' to be the live node, but got : %v", liveNodes[0]) + if newVote.IDs[0] != "foo" { + t.Errorf("Expected 'foo' to be the live node, but got : %v", newVote.IDs[0]) } } @@ -140,21 +151,19 @@ func TestLiveNodes(t *testing.T) { quo = New(conf) ) - vote1 := vote.New(conf, " foo bar baz bay\n") - vote1.ExpiresAt = time.Now().Add(1 * time.Hour) + vote1, _ := vote.New([]string{"bar", "baz", "bay"}) + vote1.ExpiresAt = inOneHour quo.vote(vote1) - vote2 := vote.New(conf, " bay foo bar baz\n") - vote2.ExpiresAt = time.Now().Add(1 * time.Hour) + vote2, _ := vote.New([]string{"foo", "bar", "baz"}) + vote2.ExpiresAt = inOneHour quo.vote(vote2) - if liveNodes, changed := quo.liveNodes(); !changed { - t.Errorf("Expected live node list to be changed: %v", liveNodes) + if newVote, changed := quo.makeMyVote(); !changed { + t.Errorf("Expected live node list to be changed: %v", newVote) } - t.Log(quo.prevLiveNodes) - if liveNodes, changed := quo.liveNodes(); changed { - t.Log(quo.prevLiveNodes) - t.Errorf("Expected live node list not to be changed: %v", liveNodes) + if newVote, changed := quo.makeMyVote(); changed { + t.Errorf("Expected live node list not to be changed: %v", newVote) } } diff --git a/internal/vote/vote_test.go b/internal/vote/vote_test.go index 04f0623..02c4af0 100644 --- a/internal/vote/vote_test.go +++ b/internal/vote/vote_test.go @@ -7,15 +7,8 @@ import ( ) func TestVote(t *testing.T) { - v, err := New([]string{"foo", "bar", "baz", "bay"}) - if err != nil { - t.Errorf(err.Error()) - } - - hostname, err := os.Hostname() - if err != nil { - t.Errorf(err.Error()) - } + v, _ := New([]string{"foo", "bar", "baz", "bay"}) + hostname, _ := os.Hostname() if v.FromID != hostname { t.Errorf("Expected vote to come from earth but came from %s", v.FromID) @@ -35,10 +28,7 @@ func TestVote(t *testing.T) { } func TestVoteExpiry(t *testing.T) { - v, err := New([]string{"foo", "bar", "baz", "bay"}) - if err != nil { - t.Errorf(err.Error()) - } + v, _ := New([]string{"foo", "bar", "baz", "bay"}) // Set expiry 1h into the future v.ExpiresAt = time.Now().Add(1 * time.Hour) @@ -54,11 +44,7 @@ func TestVoteExpiry(t *testing.T) { } func TestMarshalling(t *testing.T) { - v, err := New([]string{"foo", "bar", "baz", "bay"}) - if err != nil { - t.Errorf(err.Error()) - } - + v, _ := New([]string{"foo", "bar", "baz", "bay"}) bytes, err := v.ToJSON() if err != nil { t.Errorf("unable to serialize vote to json: %v", err) |
