summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2023-06-18 21:12:18 +0300
committerPaul Buetow <paul@buetow.org>2023-06-18 21:12:18 +0300
commit71a5b5fc4b1e8a652f1bc8ca7c04eacaa289faf3 (patch)
tree7b1a0d6b4fb01deec30536175df27d5dae1b5fd1
parent831137abdecfcafeb21fb5f3de45156819f35ed4 (diff)
can specify my id
-rw-r--r--internal/config/config.go12
-rw-r--r--internal/quorum/quorum.go2
-rw-r--r--internal/quorum/quorum_test.go28
-rw-r--r--internal/vote/vote.go12
-rw-r--r--internal/vote/vote_test.go29
5 files changed, 52 insertions, 31 deletions
diff --git a/internal/config/config.go b/internal/config/config.go
index fe23cbc..e5cd224 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -15,7 +15,9 @@ type Config struct {
StateDir string
Address string
Nodes []string
- LoopIntervalS int64 `json:"LoopIntervalS,omitempty"`
+ LoopIntervalS int64 `json:"LoopIntervalS,omitempty"`
+ MyID string `json:"MyID,omitempty"`
+ RelaxedMode bool `json:"RelaxedMode,omitempty"`
}
func New(configFile string) (Config, error) {
@@ -41,7 +43,13 @@ func New(configFile string) (Config, error) {
c.LoopIntervalS = 10
}
- log.Println("config: using loop interval of", c.LoopIntervalS)
+ if c.MyID == "" {
+ hostname, err := os.Hostname()
+ if err != nil {
+ return c, err
+ }
+ c.MyID = hostname
+ }
return c, nil
}
diff --git a/internal/quorum/quorum.go b/internal/quorum/quorum.go
index 20fbe8a..cb8f01e 100644
--- a/internal/quorum/quorum.go
+++ b/internal/quorum/quorum.go
@@ -140,5 +140,5 @@ func (quo Quorum) pruneVotes() (vote.Vote, error) {
delete(quo.votes, e)
}
- return vote.New(live)
+ return vote.New(quo.conf, live)
}
diff --git a/internal/quorum/quorum_test.go b/internal/quorum/quorum_test.go
index f5d847e..8f50493 100644
--- a/internal/quorum/quorum_test.go
+++ b/internal/quorum/quorum_test.go
@@ -17,22 +17,22 @@ func TestScore(t *testing.T) {
quo = New(conf)
)
- vote1, _ := vote.New([]string{"foo", "bar"})
+ vote1, _ := vote.New(conf, []string{"foo", "bar"})
vote1.FromID = "foo"
vote1.ExpiresAt = inOneHour
quo.vote(vote1)
- vote2, _ := vote.New([]string{"bar", "baz"})
+ vote2, _ := vote.New(conf, []string{"bar", "baz"})
vote2.FromID = "bar"
vote2.ExpiresAt = inOneHour
quo.vote(vote2)
- vote3_dup, _ := vote.New([]string{"bar", "baz"})
+ vote3_dup, _ := vote.New(conf, []string{"bar", "baz"})
vote3_dup.FromID = "bar"
vote3_dup.ExpiresAt = inOneHour
quo.vote(vote3_dup)
- vote4, _ := vote.New([]string{"foo", "bar", "baz"})
+ vote4, _ := vote.New(conf, []string{"foo", "bar", "baz"})
vote4.FromID = "baz"
vote4.ExpiresAt = inOneHour
quo.vote(vote4)
@@ -48,18 +48,18 @@ func TestScore(t *testing.T) {
}
func TestTieScore(t *testing.T) {
- addVotes := func(quo Quorum) {
- vote1, _ := vote.New([]string{"foo", "bar", "baz"})
+ addVotes := func(conf config.Config, quo Quorum) {
+ vote1, _ := vote.New(conf, []string{"foo", "bar", "baz"})
vote1.FromID = "foo"
vote1.ExpiresAt = inOneHour
quo.vote(vote1)
- vote2, _ := vote.New([]string{"foo", "bar", "baz"})
+ vote2, _ := vote.New(conf, []string{"foo", "bar", "baz"})
vote2.FromID = "bar"
vote2.ExpiresAt = inOneHour
quo.vote(vote2)
- vote3, _ := vote.New([]string{"foo", "bar", "baz"})
+ vote3, _ := vote.New(conf, []string{"foo", "bar", "baz"})
vote3.FromID = "baz"
vote3.ExpiresAt = inOneHour
quo.vote(vote3)
@@ -72,7 +72,7 @@ func TestTieScore(t *testing.T) {
quo = New(conf)
)
- addVotes(quo)
+ addVotes(conf, quo)
scores := quo.score()
if len(scores) != 3 {
@@ -95,7 +95,7 @@ func TestTieScore(t *testing.T) {
quo = New(conf)
)
- addVotes(quo)
+ addVotes(conf, quo)
scores := quo.score()
if len(scores) != 3 {
@@ -118,12 +118,12 @@ func TestExpire(t *testing.T) {
quo = New(conf)
)
- vote1, _ := vote.New([]string{"bar", "baz", "bay"})
+ vote1, _ := vote.New(conf, []string{"bar", "baz", "bay"})
vote1.FromID = "foo"
vote1.ExpiresAt = inOneHour
quo.vote(vote1)
- vote2, _ := vote.New([]string{"foo", "bar", "baz"})
+ vote2, _ := vote.New(conf, []string{"foo", "bar", "baz"})
vote2.FromID = "bar"
vote2.ExpiresAt = time.Now()
quo.vote(vote2)
@@ -152,11 +152,11 @@ func TestLiveNodes(t *testing.T) {
quo = New(conf)
)
- vote1, _ := vote.New([]string{"bar", "baz", "bay"})
+ vote1, _ := vote.New(conf, []string{"bar", "baz", "bay"})
vote1.ExpiresAt = inOneHour
quo.vote(vote1)
- vote2, _ := vote.New([]string{"foo", "bar", "baz"})
+ vote2, _ := vote.New(conf, []string{"foo", "bar", "baz"})
vote2.ExpiresAt = inOneHour
quo.vote(vote2)
diff --git a/internal/vote/vote.go b/internal/vote/vote.go
index 6fa2510..5378655 100644
--- a/internal/vote/vote.go
+++ b/internal/vote/vote.go
@@ -2,8 +2,9 @@ package vote
import (
"encoding/json"
- "os"
"time"
+
+ "codeberg.org/snonux/gorum/internal/config"
)
const Expiry = 11 * time.Second
@@ -14,15 +15,12 @@ type Vote struct {
ExpiresAt time.Time `json:"-"`
}
-func New(ids []string) (Vote, error) {
+func New(conf config.Config, ids []string) (Vote, error) {
var v Vote
- hostname, err := os.Hostname()
- if err != nil {
- return v, err
- }
- v.FromID = hostname
+ v.FromID = conf.MyID
v.IDs = ids
+
return v, nil
}
diff --git a/internal/vote/vote_test.go b/internal/vote/vote_test.go
index cccb1be..b5480b9 100644
--- a/internal/vote/vote_test.go
+++ b/internal/vote/vote_test.go
@@ -1,18 +1,23 @@
package vote
import (
- "os"
"testing"
"time"
+
+ "codeberg.org/snonux/gorum/internal/config"
)
func TestVote(t *testing.T) {
t.Parallel()
- 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)
+ conf := config.Config{
+ MyID: "foo.zone",
+ }
+
+ v, _ := New(conf, []string{"foo", "bar", "baz", "bay"})
+
+ if v.FromID != "foo.zone" {
+ t.Errorf("Expected vote to come from foo.zone but came from %s", v.FromID)
}
if len(v.IDs) != 4 {
@@ -30,7 +35,12 @@ func TestVote(t *testing.T) {
func TestVoteExpiry(t *testing.T) {
t.Parallel()
- v, _ := New([]string{"foo", "bar", "baz", "bay"})
+
+ conf := config.Config{
+ MyID: "foo.zone",
+ }
+
+ v, _ := New(conf, []string{"foo", "bar", "baz", "bay"})
// Set expiry 1h into the future
v.ExpiresAt = time.Now().Add(1 * time.Hour)
@@ -47,7 +57,12 @@ func TestVoteExpiry(t *testing.T) {
func TestMarshalling(t *testing.T) {
t.Parallel()
- v, _ := New([]string{"foo", "bar", "baz", "bay"})
+
+ conf := config.Config{
+ MyID: "foo.zone",
+ }
+
+ v, _ := New(conf, []string{"foo", "bar", "baz", "bay"})
jsonStr, err := v.ToJSON()
if err != nil {
t.Errorf("unable to serialize vote to json: %v", err)