summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2023-06-19 21:54:09 +0300
committerPaul Buetow <paul@buetow.org>2023-06-19 21:54:09 +0300
commitaf2d61921f76cfc0696d4a8caf457fc91ae472d8 (patch)
tree05a7fa24bd82972465a7d77d6542f703c1b29cde
parentdbf23cb423b15fd94930b042f402dfa8d8a76a17 (diff)
get node number correct
-rw-r--r--internal/config/config.go29
-rw-r--r--internal/config/config_test.go15
-rw-r--r--internal/quorum/quorum.go17
-rw-r--r--internal/quorum/quorum_test.go13
4 files changed, 36 insertions, 38 deletions
diff --git a/internal/config/config.go b/internal/config/config.go
index e5cd224..dcec535 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -12,12 +12,13 @@ import (
)
type Config struct {
- StateDir string
- Address string
- Nodes []string
- LoopIntervalS int64 `json:"LoopIntervalS,omitempty"`
- MyID string `json:"MyID,omitempty"`
- RelaxedMode bool `json:"RelaxedMode,omitempty"`
+ StateDir string
+ Address string
+ Nodes []string
+ LoopIntervalS int64 `json:"LoopIntervalS,omitempty"`
+ MyID string `json:"MyID,omitempty"`
+ RelaxedMode bool `json:"RelaxedMode,omitempty"`
+ nodeNumberCache map[string]int
}
func New(configFile string) (Config, error) {
@@ -54,14 +55,24 @@ func New(configFile string) (Config, error) {
return c, nil
}
-func (c Config) NodeNumber(node string) (int, error) {
+func (c *Config) NodeNumber(node string) int {
+ if c.nodeNumberCache == nil {
+ c.nodeNumberCache = make(map[string]int, len(c.Nodes))
+ }
+ nodeNumber, ok := c.nodeNumberCache[node]
+ if ok {
+ return nodeNumber
+ }
+
for i, node_ := range c.Nodes {
if node == utils.StripPort(node_) {
- return i, nil
+ c.nodeNumberCache[node] = i
+ return i
}
}
- return 0, fmt.Errorf("node %s not found", node)
+ log.Println("config:", fmt.Errorf("node %s not found - it will affect it's score!", node))
+ return 0
}
func (c Config) IsNode(remoteAddr string) bool {
diff --git a/internal/config/config_test.go b/internal/config/config_test.go
index 2fcfefb..7927664 100644
--- a/internal/config/config_test.go
+++ b/internal/config/config_test.go
@@ -10,26 +10,15 @@ func TestNodeNumber(t *testing.T) {
t.Parallel()
conf := Config{Nodes: []string{"localhost:1234", "hamburger:4321"}}
- num, err := conf.NodeNumber("localhost")
- if err != nil {
- t.Errorf(err.Error())
- }
+ num := conf.NodeNumber("localhost")
if num != 0 {
t.Errorf("localhost should be node number 0 but is %d", num)
}
- num, err = conf.NodeNumber("hamburger")
- if err != nil {
- t.Errorf(err.Error())
- }
+ num = conf.NodeNumber("hamburger")
if num != 1 {
t.Errorf("hamburger should be node number 1 but is %d", num)
}
-
- _, err = conf.NodeNumber("doener")
- if err == nil {
- t.Errorf("doener is not a node")
- }
}
func TestIsNode(t *testing.T) {
diff --git a/internal/quorum/quorum.go b/internal/quorum/quorum.go
index 37b351c..25852b2 100644
--- a/internal/quorum/quorum.go
+++ b/internal/quorum/quorum.go
@@ -92,7 +92,8 @@ func (quo Quorum) scores() (scores []Score) {
}
for _, id := range vote.IDs {
score := scoreMap[id]
- scoreMap[id] = score + 100
+ nodeNumber := quo.conf.NodeNumber(id)
+ scoreMap[id] = score + 100 + (len(quo.conf.Nodes) - nodeNumber)
}
}
@@ -101,14 +102,7 @@ func (quo Quorum) scores() (scores []Score) {
}
sort.Slice(scores, func(i, j int) bool {
- if scores[i].Value != scores[j].Value {
- return scores[i].Value > scores[j].Value
- }
-
- // Score tie, use node number.
- i_, _ := quo.conf.NodeNumber(scores[i].ID)
- j_, _ := quo.conf.NodeNumber(scores[j].ID)
- return i_ < j_
+ return scores[i].Value > scores[j].Value
})
return
@@ -145,7 +139,10 @@ func (quo *Quorum) persistScores() error {
}
sb.WriteString(score.ID)
- sb.WriteString(" with score of ")
+ sb.WriteString(" (node #")
+ sb.WriteString(strconv.Itoa(quo.conf.NodeNumber(score.ID)))
+
+ sb.WriteString(") with total score of ")
sb.WriteString(strconv.Itoa(score.Value))
sb.WriteString("\n")
diff --git a/internal/quorum/quorum_test.go b/internal/quorum/quorum_test.go
index 169fd33..a5fcbbf 100644
--- a/internal/quorum/quorum_test.go
+++ b/internal/quorum/quorum_test.go
@@ -42,8 +42,9 @@ func TestScore(t *testing.T) {
t.Errorf("Expected scores to be of length 3: %v", scores)
}
- if scores[0].ID != "bar" || scores[0].Value != 300 {
- t.Errorf("Expected score[0] to be {bar,300}: %v", scores[0])
+ t.Log(scores)
+ if scores[0].ID != "bar" || scores[0].Value != 306 {
+ t.Errorf("Expected score[0] to be {bar,306}: %v", scores[0])
}
}
@@ -78,8 +79,8 @@ func TestTieScore(t *testing.T) {
if len(scores) != 3 {
t.Errorf("Expected scores to be of length 3: %v", scores)
}
- if scores[0].ID != "foo" || scores[0].Value != 300 {
- t.Errorf("Expected score[0] to be {foo,300}: %v", scores[0])
+ if scores[0].ID != "foo" || scores[0].Value != 309 {
+ t.Errorf("Expected score[0] to be {foo,309}: %v", scores[0])
}
winner := scores[0].ID
@@ -101,8 +102,8 @@ func TestTieScore(t *testing.T) {
if len(scores) != 3 {
t.Errorf("Expected scores to be of length 3: %v", scores)
}
- if scores[0].ID != "bar" || scores[0].Value != 300 {
- t.Errorf("Expected score[0] to be {bar,300}: %v", scores[0])
+ if scores[0].ID != "bar" || scores[0].Value != 309 {
+ t.Errorf("Expected score[0] to be {bar,309}: %v", scores[0])
}
winner := scores[0].ID