Expose ClusterName instead of Banner/StatePath

This commit is contained in:
kaiyou
2020-05-15 10:17:11 +02:00
committed by Leo Antunes
parent b9df078538
commit adc7a807df
6 changed files with 22 additions and 26 deletions

View File

@ -19,21 +19,21 @@ const KeyLen = 32
// Cluster represents a running cluster configuration
type Cluster struct {
name string
ml *memberlist.Memberlist
mlConfig *memberlist.Config
localNode *common.Node
LocalName string
statePath string
state *state
events chan memberlist.NodeEvent
}
// New is used to create a new Cluster instance
// The returned instance is ready to be updated with the local node settings then joined
func New(init bool, clusterKey []byte, bindAddr string, bindPort int, statePath string, useIPAsName bool) (*Cluster, error) {
func New(name string, init bool, clusterKey []byte, bindAddr string, bindPort int, useIPAsName bool) (*Cluster, error) {
state := &state{}
if !init {
loadState(state, statePath)
loadState(state, name)
}
clusterKey, err := computeClusterKey(state, clusterKey)
@ -57,14 +57,14 @@ func New(init bool, clusterKey []byte, bindAddr string, bindPort int, statePath
}
cluster := Cluster{
name: name,
ml: ml,
mlConfig: mlConfig,
LocalName: ml.LocalNode().Name,
// The big channel buffer is a work-around for https://github.com/hashicorp/memberlist/issues/23
// More than this many simultaneous events will deadlock cluster.members()
events: make(chan memberlist.NodeEvent, 100),
state: state,
statePath: statePath,
events: make(chan memberlist.NodeEvent, 100),
state: state,
}
return &cluster, nil
}
@ -96,7 +96,7 @@ func (c *Cluster) Join(addrs []string) error {
// Leave saves the current state before leaving, then leaves the cluster
func (c *Cluster) Leave() {
c.state.save(c.statePath)
c.state.save(c.name)
c.ml.Leave(10 * time.Second)
c.ml.Shutdown() //nolint: errcheck
}
@ -146,7 +146,7 @@ func (c *Cluster) Members() <-chan []common.Node {
}
c.state.Nodes = nodes
changes <- nodes
c.state.save(c.statePath)
c.state.save(c.name)
}
}()
return changes

View File

@ -2,6 +2,7 @@ package cluster
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
@ -16,12 +17,10 @@ type state struct {
Nodes []common.Node
}
const defaultStatePath = "/var/lib/wesher/state.json"
var defaultStatePath = "/var/lib/wesher/%s.json"
func (s *state) save(statePath string) error {
if statePath == "" {
statePath = defaultStatePath
}
func (s *state) save(clusterName string) error {
statePath := fmt.Sprintf(defaultStatePath, clusterName)
if err := os.MkdirAll(path.Dir(statePath), 0700); err != nil {
return err
}
@ -34,10 +33,8 @@ func (s *state) save(statePath string) error {
return ioutil.WriteFile(statePath, stateOut, 0600)
}
func loadState(cs *state, statePath string) {
if statePath == "" {
statePath = defaultStatePath
}
func loadState(cs *state, clusterName string) {
statePath := fmt.Sprintf(defaultStatePath, clusterName)
content, err := ioutil.ReadFile(statePath)
if err != nil {
if !os.IsNotExist(err) {

View File

@ -9,7 +9,7 @@ import (
)
func Test_state_save_soad(t *testing.T) {
statePath := "/tmp/wesher.json"
defaultStatePath = "/tmp/%s.json"
key := "abcdefghijklmnopqrstuvwxyzABCDEF"
node := common.Node{
Name: "node",
@ -23,11 +23,11 @@ func Test_state_save_soad(t *testing.T) {
},
}
if err := cluster.state.save(statePath); err != nil {
if err := cluster.state.save("test"); err != nil {
t.Error(err)
}
loaded := &state{}
loadState(loaded, statePath)
loadState(loaded, "test")
if !reflect.DeepEqual(cluster.state, loaded) {
t.Errorf("cluster state save then reload mistmatch: %s / %s", cluster.state, loaded)