Unexport state from the cluster package
This commit is contained in:
parent
9ddcbb7914
commit
d85fb84398
|
@ -23,14 +23,14 @@ type Cluster struct {
|
||||||
ml *memberlist.Memberlist
|
ml *memberlist.Memberlist
|
||||||
mlConfig *memberlist.Config
|
mlConfig *memberlist.Config
|
||||||
localNode common.Node
|
localNode common.Node
|
||||||
state *State
|
state *state
|
||||||
events chan memberlist.NodeEvent
|
events chan memberlist.NodeEvent
|
||||||
}
|
}
|
||||||
|
|
||||||
// New is used to create a new Cluster instance
|
// New is used to create a new Cluster instance
|
||||||
// The returned instance is ready to be updated with the local node settings then joined
|
// 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, useIPAsName bool) (*Cluster, error) {
|
func New(init bool, clusterKey []byte, bindAddr string, bindPort int, useIPAsName bool) (*Cluster, error) {
|
||||||
state := &State{}
|
state := &state{}
|
||||||
if !init {
|
if !init {
|
||||||
loadState(state)
|
loadState(state)
|
||||||
}
|
}
|
||||||
|
@ -151,7 +151,7 @@ func (c *Cluster) setupDelegate() {
|
||||||
c.mlConfig.Events = &memberlist.ChannelEventDelegate{Ch: c.events}
|
c.mlConfig.Events = &memberlist.ChannelEventDelegate{Ch: c.events}
|
||||||
}
|
}
|
||||||
|
|
||||||
func computeClusterKey(state *State, clusterKey []byte) ([]byte, error) {
|
func computeClusterKey(state *state, clusterKey []byte) ([]byte, error) {
|
||||||
if len(clusterKey) == 0 {
|
if len(clusterKey) == 0 {
|
||||||
clusterKey = state.ClusterKey
|
clusterKey = state.ClusterKey
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// State keeps track of information needed to rejoin the cluster
|
// State keeps track of information needed to rejoin the cluster
|
||||||
type State struct {
|
type state struct {
|
||||||
ClusterKey []byte
|
ClusterKey []byte
|
||||||
Nodes []common.Node
|
Nodes []common.Node
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,7 @@ func (c *Cluster) saveState() error {
|
||||||
return ioutil.WriteFile(statePath, stateOut, 0600)
|
return ioutil.WriteFile(statePath, stateOut, 0600)
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadState(cs *State) {
|
func loadState(cs *state) {
|
||||||
content, err := ioutil.ReadFile(statePath)
|
content, err := ioutil.ReadFile(statePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !os.IsNotExist(err) {
|
if !os.IsNotExist(err) {
|
||||||
|
@ -41,7 +41,7 @@ func loadState(cs *State) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// avoid partially unmarshalled content by using a temp var
|
// avoid partially unmarshalled content by using a temp var
|
||||||
csTmp := &State{}
|
csTmp := &state{}
|
||||||
if err := json.Unmarshal(content, csTmp); err != nil {
|
if err := json.Unmarshal(content, csTmp); err != nil {
|
||||||
logrus.Warnf("could not decode state: %s", err)
|
logrus.Warnf("could not decode state: %s", err)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
"github.com/costela/wesher/common"
|
"github.com/costela/wesher/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_State_Save_Load(t *testing.T) {
|
func Test_state_save_soad(t *testing.T) {
|
||||||
key := "abcdefghijklmnopqrstuvwxyzABCDEF"
|
key := "abcdefghijklmnopqrstuvwxyzABCDEF"
|
||||||
node := common.Node{
|
node := common.Node{
|
||||||
Name: "node",
|
Name: "node",
|
||||||
|
@ -16,14 +16,14 @@ func Test_State_Save_Load(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
cluster := Cluster{
|
cluster := Cluster{
|
||||||
state: &State{
|
state: &state{
|
||||||
ClusterKey: []byte(key),
|
ClusterKey: []byte(key),
|
||||||
Nodes: []common.Node{node},
|
Nodes: []common.Node{node},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
cluster.saveState()
|
cluster.saveState()
|
||||||
loaded := &State{}
|
loaded := &state{}
|
||||||
loadState(loaded)
|
loadState(loaded)
|
||||||
|
|
||||||
if !reflect.DeepEqual(cluster.state, loaded) {
|
if !reflect.DeepEqual(cluster.state, loaded) {
|
||||||
|
|
Loading…
Reference in New Issue