diff options
| author | Paul Buetow <paul@buetow.org> | 2023-06-04 23:16:52 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2023-06-04 23:16:52 +0300 |
| commit | 5ae52a553444725a190be6381a851d13e3409da0 (patch) | |
| tree | 0b712fb08a1a4adf57767f8615fd68b4aedfb272 | |
| parent | 2e6796726329f211c70785f2fa29a4cded399412 (diff) | |
first ID in message is from node
| -rw-r--r-- | internal/client/client.go | 1 | ||||
| -rw-r--r-- | internal/quorum/quorum.go | 3 | ||||
| -rw-r--r-- | internal/quorum/quorum_test.go | 18 | ||||
| -rw-r--r-- | internal/server/tcpserver.go | 2 | ||||
| -rw-r--r-- | internal/vote/vote.go | 12 | ||||
| -rw-r--r-- | internal/vote/vote_test.go | 10 |
6 files changed, 26 insertions, 20 deletions
diff --git a/internal/client/client.go b/internal/client/client.go index 25d99e0..b444d35 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -25,6 +25,7 @@ func Start(ctx context.Context, conf config.Config, liveNodesCh <-chan []string) for { select { case liveNodes := <-liveNodesCh: + log.Printf("Notifying live nodes %v to all partner nodes", liveNodes) for _, ch := range fanOut { select { case <-ch: diff --git a/internal/quorum/quorum.go b/internal/quorum/quorum.go index b299460..d7fb866 100644 --- a/internal/quorum/quorum.go +++ b/internal/quorum/quorum.go @@ -41,6 +41,7 @@ func (quo Quorum) Start(ctx context.Context) <-chan []string { liveNodesCh <- quo.deleteExpiredVotes() case vote := <-quo.voteCh: quo.vote(vote) + quo.score() liveNodesCh <- quo.deleteExpiredVotes() case <-ctx.Done(): return @@ -58,7 +59,7 @@ func (quo Quorum) Vote(v vote.Vote) { func (quo Quorum) vote(v vote.Vote) { log.Printf("Adding vote %v", v) - quo.votes[v.From] = v + quo.votes[v.FromID] = v } func (quo Quorum) score() (scores []Score) { diff --git a/internal/quorum/quorum_test.go b/internal/quorum/quorum_test.go index 9953c11..ac5cbc6 100644 --- a/internal/quorum/quorum_test.go +++ b/internal/quorum/quorum_test.go @@ -12,19 +12,19 @@ func TestScore(t *testing.T) { conf := config.Config{Nodes: []string{"foo:1234", "bar:4321", "baz:3444"}} quo := New(conf) - vote1 := vote.New(conf, "foo:334234", "foo bar\n") + vote1 := vote.New(conf, "foo foo bar\n") vote1.ExpiresAt = time.Now().Add(1 * time.Hour) quo.vote(vote1) - vote2 := vote.New(conf, "bar:334234", "bar baz\n") + vote2 := vote.New(conf, "bar bar baz\n") vote2.ExpiresAt = time.Now().Add(1 * time.Hour) quo.vote(vote2) - vote3_dup := vote.New(conf, "bar:33234", "bar baz\n") + vote3_dup := vote.New(conf, "bar bar baz\n") vote3_dup.ExpiresAt = time.Now().Add(1 * time.Hour) quo.vote(vote3_dup) - vote4 := vote.New(conf, "baz:334234", "foo bar baz\n") + vote4 := vote.New(conf, "baz foo bar baz\n") vote4.ExpiresAt = time.Now().Add(1 * time.Hour) quo.vote(vote4) @@ -40,15 +40,15 @@ func TestScore(t *testing.T) { func TestTieScore(t *testing.T) { addVotes := func(conf config.Config, quo Quorum) { - vote1 := vote.New(conf, "foo:334234", "foo bar baz\n") + vote1 := vote.New(conf, "foo foo bar baz\n") vote1.ExpiresAt = time.Now().Add(1 * time.Hour) quo.vote(vote1) - vote2 := vote.New(conf, "bar:334234", "foo bar baz\n") + vote2 := vote.New(conf, "bar foo bar baz\n") vote2.ExpiresAt = time.Now().Add(1 * time.Hour) quo.vote(vote2) - vote3 := vote.New(conf, "baz:334234", "foo bar baz\n") + vote3 := vote.New(conf, "baz foo bar baz\n") vote3.ExpiresAt = time.Now().Add(1 * time.Hour) quo.vote(vote3) } @@ -100,11 +100,11 @@ func TestExpire(t *testing.T) { conf := config.Config{Nodes: []string{"foo:1234", "bay:4321"}} quo := New(conf) - vote1 := vote.New(conf, "foo:334234", " foo bar baz bay\n") + vote1 := vote.New(conf, " foo bar baz bay\n") vote1.ExpiresAt = time.Now().Add(1 * time.Hour) quo.vote(vote1) - vote2 := vote.New(conf, "bar:334234", " foo bar baz bay\n") + vote2 := vote.New(conf, " bay foo bar baz\n") vote2.ExpiresAt = time.Now() quo.vote(vote2) diff --git a/internal/server/tcpserver.go b/internal/server/tcpserver.go index 9ebbb60..30baa4a 100644 --- a/internal/server/tcpserver.go +++ b/internal/server/tcpserver.go @@ -60,7 +60,7 @@ func handleConnection(ctx context.Context, conf config.Config, } log.Printf("Received message from %s: %s", remoteAddr, message) - ch <- vote.New(conf, remoteAddr, message) + ch <- vote.New(conf, message) conn.Write([]byte(message)) } diff --git a/internal/vote/vote.go b/internal/vote/vote.go index 75fa67a..4fff0fd 100644 --- a/internal/vote/vote.go +++ b/internal/vote/vote.go @@ -6,28 +6,32 @@ import ( "time" "codeberg.org/snonux/gorum/internal/config" - "codeberg.org/snonux/gorum/internal/utils" ) const Expiry = 20 * time.Second type Vote struct { - From string + FromID string IDs []string ExpiresAt time.Time } -func New(conf config.Config, from, message string) Vote { +func New(conf config.Config, message string) Vote { + var fromID string var ids []string for _, id := range strings.Split(strings.TrimSpace(message), " ") { if !conf.IsNode(id) { log.Printf("%s is not a node, excluding from the vote", id) continue } + if fromID == "" { + fromID = id + continue + } ids = append(ids, id) } - return Vote{utils.StripPort(from), ids, time.Now().Add(Expiry)} + return Vote{fromID, ids, time.Now().Add(Expiry)} } func (v Vote) Expired() bool { diff --git a/internal/vote/vote_test.go b/internal/vote/vote_test.go index af8b741..24af55f 100644 --- a/internal/vote/vote_test.go +++ b/internal/vote/vote_test.go @@ -8,11 +8,11 @@ import ( ) func TestVote(t *testing.T) { - conf := config.Config{Nodes: []string{"foo:1234", "bay:4321"}} - v := New(conf, "earth:334234", " foo bar baz bay\n") + conf := config.Config{Nodes: []string{"earth:1234", "foo:1234", "bay:4321"}} + v := New(conf, "earth foo bar baz bay\n") - if v.From != "earth" { - t.Errorf("Expected vote to come from earth but came from %s", v.From) + if v.FromID != "earth" { + t.Errorf("Expected vote to come from earth but came from %s", v.FromID) } if len(v.IDs) != 2 { @@ -30,7 +30,7 @@ func TestVote(t *testing.T) { func TestVoteExpiry(t *testing.T) { conf := config.Config{Nodes: []string{"foo:1234", "bay:4321"}} - v := New(conf, "earth:334234", " foo bar baz bay\n") + v := New(conf, "earth foo bar baz bay\n") // Set expiry 1h into the future v.ExpiresAt = time.Now().Add(1 * time.Hour) |
